Reputation:
Alright, so I got two classes here, the main and the keylistener class which i use to pass keyboard inputs to the main class.
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
private final static int WIDTH = 240;
private final static int HEIGHT = WIDTH / 12 * 9;
private final static int SCALE = 2;
private String TITLE = "Game";
private boolean running = false;
private Thread thread;
Random r = new Random();
ArrayList<Point> snakePart = new ArrayList<Point>();
Point head;
KeyHandle keyhandle;
public static void main(String[] args) {
new Game();
}
public Game() {
init();
JFrame frame = new JFrame(TITLE);
Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
setMinimumSize(size);
setMaximumSize(size);
setPreferredSize(size);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.pack();
frame.setVisible(true);
start();
}
public void init() {
head = new Point(0,0);
addKeyListener(new KeyHandle());
}
@Override
public void run() {
final double ticks = 60.0;
long initTime = System.nanoTime();
double ns = 1000000000 / ticks;
double delta = 0;
long timer = System.currentTimeMillis();
double updates = 0;
int frames = 0;
while (running) {
long nowTime = System.nanoTime();
delta += (nowTime - initTime) / ns;
initTime = nowTime;
if (delta >= 1) {
tick();
updates++;
delta--;
}
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("Ticks: " + updates + " Frames: " + frames);
updates = 0;
frames = 0;
}
}
stop();
}
public void tick() {
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.white);
g.drawRect(head.x, head.y, 10, 10);
bs.show();
g.dispose();
}
private synchronized void start() {
if (running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop() {
if (!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(1);
}
}
and then i've got the keylistener class which looks like this
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyHandle implements KeyListener {
Game game;
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP){
} else if(e.getKeyCode() == KeyEvent.VK_DOWN){
game.head.y += 10;
} else if(e.getKeyCode() == KeyEvent.VK_LEFT){
} else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
} else if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP){
} else if(e.getKeyCode() == KeyEvent.VK_DOWN){
} else if(e.getKeyCode() == KeyEvent.VK_LEFT){
} else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
} else if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
Now the problem appears when I try to edit the "head" y and x values as seen in the second class.
if(e.getKeyCode() == KeyEvent.VK_DOWN){
game.head.y += 10;
What am I doing wrong here and is there a better way to pass and set x and y values more efficiently.
Upvotes: 1
Views: 46
Reputation: 70
Your KeyHandle class contains a reference to a Game but no intantiation of game. Instead of creating new Game()
in your main method and dropping the instance on the floor, allocate that instance to the reference contained in KeyHandle.
keyhandle.game = new Game();
Upvotes: 1