Let’s Make a Video Game! Part 1

Time to work on the model. Create a class in your source package called “AlienModel”, implement Entity and generate the methods.  

We can see at this point that we have a bunch of “gets” and no “sets”.  Let’s make some set Methods in our interface because our models should be able to move around.

So we need variables to cover position, isAlive and size.  This will be a bit of typing because we’ve got to create all the getters and setters and instance variables. When you finish it should look like this:

Entity

public interface Entity {

 

   public boolean isAlive();

 

   public void setAlive(boolean isAlive);

 

   public int getWidth();

 

   public void setWidth(int width);

 

   public int getHeight();

 

   public void setHeight(int height);

 

   public int getXPos();

 

   public void setXPos(int x);

 

   public int getYPos();

 

   public void setYPos(int y);

}

 

AlienModel

public class AlienModel implements Entity {

 

   private int x;

   private int y;

   private int h;

   private int w;

   private boolean isAlive;

 

   public AlienModel() {

       x = 0;

       y = 0;

       h = 20;

       w = 20;

       isAlive = true;

   }

 

   @Override

   public boolean isAlive() {

       return isAlive;

   }

 

   @Override

   public void setAlive(boolean isAlive) {

       this.isAlive = isAlive;

   }

 

   @Override

   public int getWidth() {

       return w;

   }

 

   @Override

   public void setWidth(int width) {

       this.w = width;

   }

 

   @Override

   public int getHeight() {

       return h;

   }

 

   @Override

   public void setHeight(int height) {

       this.h = height;

   }

 

   @Override

   public int getXPos() {

       return x;

   }

 

   @Override

   public void setXPos(int x) {

       this.x = x;

   }

 

   @Override

   public int getYPos() {

       return y;

   }

 

   @Override

   public void setYPos(int y) {

       this.y = y;

   }

}

 

Time to bring it all together.  Open your Integration test file.  Since our test method is going to start getting clutter with all the stuff we need to configure to get the aliens in the window, let’s break it apart a bit.  Move the JFrame configuration into its own method:

 

   private JFrame configureWindow() {

       JFrame frame = new JFrame();

       frame.addWindowListener(new WindowAdapter() {

 

           @Override

           public void windowClosing(WindowEvent e) {

               isRunning = false;

           }

       });

 

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

       return frame;

   }

 

Then delete all the JFrame config details from your test method and just refer to the one you just created:

   @Test

   public void runGame() throws InterruptedException {

       isRunning = true;

       JFrame frame = configureWindow();

 

       while (isRunning) {

           TimeUnit.MILLISECONDS.sleep(50000);

       }

   }

 

We need something to draw our graphics on, so we’ll create an awt Canvas and plug it into the JFrame — let’s give this procedure its own method.

 

       private Canvas configureView(JFrame frame) {

       Canvas canvas = new Canvas();

       canvas.setBounds(0, 0, 800, 600);

 

       JPanel contentPanel = (JPanel) frame.getContentPane();

 

       contentPanel.setPreferredSize(new Dimension(800, 600));

       contentPanel.setLayout(null);

       contentPanel.add(canvas);

 

       contentPanel.setPreferredSize(new Dimension(800, 600));

 

       frame.pack();

 

       frame.setVisible(true);

       frame.setResizable(false);

 

       return canvas;

   }

 

Our Test method should look like this:

   @Test

   public void runGame() throws InterruptedException {

       isRunning = true;

 

       JFrame frame = configureWindow();

       Canvas canvas = configureView(frame);

 

       while (isRunning) {

           TimeUnit.MILLISECONDS.sleep(50000);

       }

   }

 

We forgot to set the initial position of the AlienView to be in synch with the model, so let’s go back and do that. Open AlienView and in the constructor, type in the following:

 

this.graphic.setLocation(model.getXPos(), model.getYPos());

 

so you’ll have this:

 

   public AlienView(Entity model) {

       this.model = model;

       this.graphic = new Rectangle(model.getWidth(), model.getHeight());

       this.graphic.setLocation(model.getXPos(), model.getYPos());

   }

 

We need to generate a collection of models that we can traverse every frame and draw. Let’s add our Aliens and put them in their positions.  We’ll give this operation its own method too so that it’s easy to read.

 

   private List<Entity> createEntities() {

       List<Entity> entities = new ArrayList<>();

       for (int row = 0; row < 5; row++) {

           for (int x = 0; x < 12; x++) {

               Entity alien = new AlienModel();

               alien.setXPos(100 + (x * 50));

               alien.setYPos(100 + row * 50);

               entities.add(alien);

           }

       }

 

       return entities;

   }

 

This code does a double iteration, for every row position we iterate over every column position and create an alien for that row and column.  This will distribute the positions so that they don’t all spawn on top of each other.

Author: Bruce Brown

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *