When I run the game and try to use the Arrow keys to control the movement of the selection box, nothing happens. I'm pretty sure the game is redrawing itself properly because the ticker text is updating. Aswell, I tried doing:
selectVal++;
in the update function in TankShooter.Java, which made the box redraw and change position everytime.
Here's all my code.
-- Game.Java
package org.game.engine;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
public abstract class Game implements KeyListener, MouseListener, MouseMotionListener, MouseWheelListener {
protected boolean over;
protected String title = "My Game";
protected int width=400, height=300;
protected int delay = 30;
public void init() {}
abstract public void update();
abstract public void draw(Graphics2D g);
public boolean isOver() { return over; }
public String getTitle() { return title; }
public int getWidth() { return width; }
public int getHeight() { return height; }
public int getDelay() { return delay; }
public void resize(int width, int height) {}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseWheelMoved(MouseWheelEvent e) {
}
}
-- GameApplication.Java
package org.game.engine;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class GameApplication {
public static void start(final Game game) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame(game.getTitle());
frame.setSize(game.getWidth(), game.getHeight());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GameCanvas canvas = new GameCanvas(game);
canvas.setGame(game);
frame.add(canvas);
frame.setVisible(true);
canvas.requestFocus();
new GameLoop(game, canvas).start();
}
});
}
}
-- GameCanvas.Java
package org.game.engine;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JComponent;
@SuppressWarnings("serial")
public class GameCanvas extends JComponent implements ComponentListener {
private Game game;
public GameCanvas() {
}
public GameCanvas(Game game) {
this.game = game;
addKeyListener(game);
addMouseListener(game);
addMouseMotionListener(game);
addMouseWheelListener(game);
requestFocus();
addComponentListener(this);
}
public void setGame(Game game) {
this.game = game;
addKeyListener(game);
addMouseListener(game);
addMouseMotionListener(game);
requestFocus();
addComponentListener(this);
}
@Override
public void paintComponent(Graphics g) {
game.draw((Graphics2D)g);
}
@Override
public void componentResized(ComponentEvent ce) {
game.resize(ce.getComponent().getWidth(), ce.getComponent().getHeight());
}
@Override
public void componentMoved(ComponentEvent ce) {
}
@Override
public void componentShown(ComponentEvent ce) {
//game.resize(ce.getComponent().getWidth(), ce.getComponent().getHeight());
}
@Override
public void componentHidden(ComponentEvent ce) {
}
}
-- GameLoop
package org.game.engine;
import java.util.logging.Level;
import java.util.logging.Logger;
public class GameLoop extends Thread {
private final Game game;
private final GameCanvas canvas;
private boolean stopped;
private boolean paused;
public GameLoop(Game game, GameCanvas canvas) {
this.game = game;
this.canvas = canvas;
this.stopped = false;
this.paused = false;
}
public void pauseGame() {
this.paused = true;
}
public void resumeGame() {
this.paused = false;
}
public void stopGame() {
stopped = true;
}
@Override
public void run() {
game.init();
while (!game.isOver() && !stopped) {
if (!paused) {
game.update();
canvas.repaint();
}
try {
Thread.sleep(game.getDelay());
} catch (InterruptedException ex) {
Logger.getLogger(GameLoop.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
-- TankShooter.Java
package org.game.tankshooter;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.game.engine.Game;
import org.game.engine.GameApplication;
public class TankShooter extends Game {
public static void main(String[] args) {
GameApplication.start(new TankShooter());
}
//Variable Declarations
int x;
int y;
int selModX;
int selModY;
int selectVal;
int ticker = 0;
BufferedImage GUI_BottomGUI;
BufferedImage GUI_SelectGUI;
public TankShooter() {
title = "Tank Shooter";
width = 950;
height = 650;
selModX = 0;
selModY = 0;
//Graphics Imports
try {
GUI_BottomGUI = ImageIO.read(new File("Resources/Images/BottomGUI.png"));
GUI_SelectGUI = ImageIO.read(new File("Resources/Images/SelectGUI.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == ((KeyEvent.VK_UP) | (KeyEvent.VK_DOWN)) && (selectVal == 1) | (selectVal == 3)) {
selectVal += 1;
}
if (e.getKeyCode() == ((KeyEvent.VK_DOWN) | (KeyEvent.VK_UP)) && (selectVal == 2) | (selectVal == 4)) {
selectVal -= 1;
}
if (e.getKeyCode() == ((KeyEvent.VK_RIGHT) | (KeyEvent.VK_LEFT)) && (selectVal == 1) | (selectVal == 3)) {
selectVal += 2;
}
if (e.getKeyCode() == ((KeyEvent.VK_RIGHT) | (KeyEvent.VK_LEFT)) && (selectVal == 2) | (selectVal == 4)) {
selectVal -= 2;
}
}
@Override
public void update() {
ticker++;
if (selectVal == 1) {
selModX = 0;
selModY = 0;
}
else if (selectVal == 2) {
selModX = 0;
selModY = GUI_SelectGUI.getHeight();
}
else if (selectVal == 3) {
selModX = GUI_SelectGUI.getWidth();
selModY = 0;
}
else if (selectVal == 4) {
selModX = GUI_SelectGUI.getWidth();
selModY = GUI_SelectGUI.getHeight();
}
//selectVal++;
if (selectVal > 4) {
selectVal = 0;
}
}
@Override
public void draw(Graphics2D g) {
g.drawImage(GUI_BottomGUI, 0, 462, null);
g.drawImage(GUI_SelectGUI, (13+selModX), (475+selModY), null);
g.drawString(String.valueOf(selectVal), 5, 10);
g.drawString(String.valueOf(ticker), 5, 20);
}
@Override
public void init() {
}
}