Back to home

Below is the code for the Game class, added as a separate driver to make a game window pop up. You can also download the .jar file to play the game yourself with the link below.

.jar download link, you need to log into your smuhsd.org account to view this file.

package game;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import static javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Game {
public static void main(String[] args) {
new Game();
}

public Game() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Philip Spring Final");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public static class TestPane extends JPanel {

Mario mario = new Mario();

protected static final int SPRITE_HEIGHT = 30;
protected static final int SPRITE_WIDTH = 30;
private int yPos; // The vertical position...
private int xPos; // The horizontal position...
private Timer engine;
JLabel damage1 = new JLabel("Most Recent Attack Result: ");
// modified later
JLabel damage2 = new JLabel("none, try pressing space!");
JLabel enemy = new JLabel("enemy");
JLabel player = new JLabel(mario.getName());
JLabel i1 = new JLabel("LEFT ARROW KEY to move left, RIGHT ARROW KEY to move right.");
JLabel i2 = new JLabel("SPACE to attack, ENTER for other actions.");
JLabel inv1 = new JLabel("Character Details:");
int height = 100;
JLabel inv2 = new JLabel(mario.toString());

public TestPane() {

xPos = (getPreferredSize().width - SPRITE_WIDTH) / 2;
yPos = getPreferredSize().height - SPRITE_HEIGHT;

InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
am.put("right", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (xPos <= 370) {
xPos = xPos + 5;
}
inv2.setText(mario.toString());
}
});

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
am.put("left", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (xPos >= 0) {
xPos = xPos - 5;
}
inv2.setText(mario.toString());
}
});

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "attack");
am.put("attack", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
try {
if (xPos >= 300) {
String hit = mario.attack(mario.getItemWithMaxAttack().substring(39).split("AKA")[0].trim());
damage2.setText(hit);
} else {
damage2.setText(mario.getName() + " missed the enemy! Try moving closer to it.");
}
inv2.setText(mario.toString());
} catch (Exception err) {
damage2.setText(mario.getName() + " does not have a weapon! Give them one by pressing enter.");
}
}
});

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "inv");
am.put("inv", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
String action = JOptionPane.showInputDialog("What would you like to do?\nTo add item to your inventory, type \"item\".\nTo clear your inventory, type \"clear\".\nTo change your name, type \"name\".\nTo change your base attack power, type \"base\".");
if (!(action.equals("item") || action.equals("name") || action.equals("base") || action.equals("clear"))) {
JOptionPane.showMessageDialog(null, "Invalid Input.");
inv2.setText(mario.toString());
} else {
switch(action) {
case "item":
double atk = Double.parseDouble(JOptionPane.showInputDialog("Give your item an attack power as a double or integer value."));
String alias = JOptionPane.showInputDialog("Enter aliases for your item separated by commas, with no spaces between the commas.\nExample: memes,dreams,john cena,cool thing");
mario.addItem(new InventoryItem(atk, false, alias.split(",")));
height += 15;
break;
case "clear":
mario.clearInventory();
height = 95;
break;
case "name":
String name = JOptionPane.showInputDialog("Type in a name, or type \"random\" to be assigned a random substring of your name as your new name.");
if (!(name.equals("random")))
mario.setName(name);
else
mario.giveMeAnotherName();
player.setText(mario.getName());
break;
case "base":
int baseatk = Integer.parseInt(JOptionPane.showInputDialog("What do you want your new base attack value to be? This determines the upper bound of your attack damage."));
mario.setAttack(baseatk);
break;
}
inv2.setText(mario.toString());
}
}
});

engine = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});
engine.start();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// Set the current color to red (setColor)
g.setColor(Color.RED);

// Obtain the rectanglar bounds of the text
FontMetrics metrics = g.getFontMetrics();
Rectangle2D strRect = metrics.getStringBounds("Java 2D!", g);

// Calculate the center of the screen
int centerPanelX = 100;
int centerPanelY = 100;

// Calculate the starting point for the centered string
int strX = centerPanelX - (int)(strRect.getWidth() / 2);
int strY = centerPanelY + (int)(strRect.getHeight() / 2);

// Draw x and y axis (drawLine)
g.drawLine(200, 0, 200, 400);
g.drawLine(0, 200, 400, 200);

// Draw the floor (drawRect)
g.setColor(Color.GREEN);
g.drawRect(0, 380, 400, 20);
g.fillRect(0, 380, 400, 20);

// Draw the sky (drawRect)
g.setColor(Color.CYAN);
g.drawRect(0, 0, 400, 380);
g.fillRect(0, 0, 400, 380);

// Draw enemy
g.setColor(Color.GRAY);
g.drawRect(330, 320, 60, 60);
g.fillRect(330, 320, 60, 60);

enemy.setBounds(340, 255, 100, 100);
super.add(enemy);

// draw ball
g.setColor(Color.RED);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawOval(xPos, yPos - 15, SPRITE_WIDTH, SPRITE_HEIGHT);
g2d.fillOval(xPos, yPos - 15, SPRITE_WIDTH, SPRITE_HEIGHT);
g2d.dispose();

i1.setBounds(5, 5, 500, 20);
i2.setBounds(5, 20, 500, 20);
damage1.setBounds(5, 45, 200, 20);
damage2.setBounds(5, 60, 1000, 20);
super.add(i1);
super.add(i2);
super.add(damage1);
super.add(damage2);

inv1.setBounds(5, 85, 200, 20);
inv2.setBounds(5, 100, 1000, height);
super.add(inv1);
super.add(inv2);

player.setBounds(xPos, yPos - 50, 50, 50);
super.add(player);
}
}
}