Hello, i'm making a simple game where there's a 10x10 grid and you can move around a selection box and you can move around. Iv'e been trying to make the green selection box to move around but it won't move! I only have right movement in the code but it won't work so far. Here are the classes that have to do greatly with each other.
import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.image.BufferStrategy; import java.util.Random; public class Game extends Canvas implements Runnable{ private static final long serialVersionUID = 1L; private boolean running = false; private Thread thread; public static int WIDTH, HEIGHT; Handler handler; Graphics g; Random rand = new Random(); Selection select; public void init(){ handler = new Handler(); select = new Selection(0, 0); this.addKeyListener(new KeyInput(this)); this.requestFocus(); for (int i = 0; i < 100; i++){ int R = rand.nextInt(21); int G = rand.nextInt(21); int B = rand.nextInt(255); Color randomColor = new Color(R,G,B); handler.addObject(new Square(rand.nextInt(10) * 61, rand.nextInt(10) * 61, ObjectId.Square, randomColor)); } } public void run(){ init(); this.requestFocus(); long lastTime = System.nanoTime(); double amountOfTicks = 60.0; double ns = 1000000000 / amountOfTicks; double delta = 0; long timer = System.currentTimeMillis(); int updates = 0; int frames = 0; while(running){ long now = System.nanoTime(); delta += (now - lastTime) / ns; lastTime = now; while(delta >= 1){ tick(); updates++; delta--; } render(); frames++; if(System.currentTimeMillis() - timer > 1000){ timer += 1000; System.out.println("FPS: " + frames + " TICKS: " + updates); frames = 0; updates = 0; } } } public void tick(){ handler.tick(); } public void render(){ BufferStrategy bs = this.getBufferStrategy(); if (bs == null){ this.createBufferStrategy(3); return; } Graphics g = bs.getDrawGraphics(); ///////////////////////////////// //Draw Here g.setColor(Color.BLACK); g.fillRect(0, 0, getWidth(), getHeight()); handler.render(g); select.render(g); for(int x = 0; x < 600; x+=61){ for (int y = 0; y < 600; y +=61){ g.setColor(Color.white); g.drawRect(x, y, 61, 61); } } ///////////////////////////////// g.dispose(); bs.show(); } public synchronized void start(){ if (running){ return; } running = true; thread = new Thread(this); thread.start(); } public synchronized void stop(){ } public static void main(String[] args){ new Window(600, 600, "Squares", new Game()); } //this is not working for moving the selection box r public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D){ select.setX(61); } } public void KeyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D){ select.setX(getX()); } } }
Selection class
import java.awt.Color; import java.awt.Graphics; public class Selection { Graphics g; private double x, y; public Selection(double x, double y){ this.x = x; this.y = y; } public void render(Graphics g){ g.setColor(Color.GREEN); g.fillRect((int)x, (int)y, 61, 61); } public void tick(){ } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } }
KeyInput Class
import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class KeyInput extends KeyAdapter{ Game game; public KeyInput(Game game){ this.game = game; } public void KeyPressed(KeyEvent e){ game.keyPressed(e); } public void KeyReleased(KeyEvent e){ game.KeyReleased(e); } }
I also tried an approach where you click with the move and it snaps to a tile grid but that didn't work either. Help is MUCH appreciated. I also messed with it a lot so don't be surprised if it's pretty messy