hydregion
hydregion

Reputation: 69

Get squares to move across the screen

I am trying to create a program that will open a new window, display 50 squares, and make them move in a random direction off screen. This renders them, but they don't move.

package game;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;


public class Game extends Canvas implements Runnable{

   private static final long serialVersionUID = 1550691097823471818L; 

  public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;

  private Thread thread;
  private boolean running = false;

  private Random r;
  private Handler handler;

   public Game(){
       new Window(WIDTH, HEIGHT, "Test", this);

       handler = new Handler();
       r = new Random();

       for(int i = 0; i < 50; i++){
         handler.addObject(new Player(r.nextInt(WIDTH), r.nextInt(HEIGHT), ID.Player));  
       }


   }

   public synchronized void start(){
       thread = new Thread(this);
       thread.start();
       running = true;
   } 

   public synchronized void stop(){
       try{
           thread.join();
           running = false;
       }catch(Exception e){
           e.printStackTrace();
       } 
   } 

   public void run(){
       long lastTime = System.nanoTime();
       double amountOfTicks = 60.0;
       double ns = 1000000000 / amountOfTicks;
       double delta = 0;
       long timer = System.currentTimeMillis();
       int frames = 0;
       while(running){
           long now = System.nanoTime();
           delta += (now - lastTime) / ns;
           lastTime = now;
           while (delta >= 1){
               tick();
               delta--;

           }
           if(running)
               render();
           frames++;

           if(System.currentTimeMillis() - timer > 10000){
               timer += 10000;
               System.out.println("FPS: " + frames);
               frames = 0;
           }
       }
       stop();
   }

   private void tick(){
       handler.tick();
   }

   private void render(){
       BufferStrategy bs = this.getBufferStrategy();
       if(bs == null){
           this.createBufferStrategy(3);
           return;
       }

       Graphics g = bs.getDrawGraphics();
       while(1==1){
       g.setColor(Color.black);
       g.fillRect(0, 0, WIDTH, HEIGHT);

       handler.render(g);

       g.dispose();
       bs.show();
       }
   }

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




}

here is the player class

package game;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;



public class Player extends GameObject {

    Random r = new Random();

    public Player(int x, int y, ID id){
        super(x, y, id);

        velX = r.nextInt(5) + 1;
        velY = r.nextInt(5) + 1;

    }


    public void tick(){    
    x += velX;
    y += velY;
    }


    public void render(Graphics g){ 
        g.setColor(Color.white);
        g.fillRect(x, y, 32, 32);
    }
}

Handler class

package game;

import java.awt.Graphics;
import java.util.LinkedList;

public class Handler {

LinkedList<GameObject> object = new LinkedList<GameObject>();

public void tick(){
    for(int i = 0; i < object.size(); i++){
        GameObject tempObject = object.get(i);

        tempObject.tick();
    }

}

public void render(Graphics g){
    for(int i = 0; i < object.size(); i++){
        GameObject tempObject = object.get(i);

        tempObject.render(g);
    }
}

public void addObject(GameObject object){
    this.object.add(object);
}

public void removeObject(GameObject object){
    this.object.remove(object);
}

}

Upvotes: 0

Views: 141

Answers (2)

Ok, I figured it out, in the handler render method, you never call the tick method. So change the method to:

public void render(Graphics g){
for(int i = 0; i < object.size(); i++){
    GameObject tempObject = object.get(i);
    tempObject.tick();
    tempObject.render(g);
}
}

I tested it, and it worked, it's actually a really good program.

Upvotes: 2

Int the tick method in the Player class is slightly wrong, it's supposed to be

public void tick() {
x = x + velX;
y = y + velY;
}

the += or -= is adding/subtracting bytes.

Upvotes: 0

Related Questions