Let’s Make a Video Game! Part 1

Now let’s add them to the canvas:

 

   @Test

   public void runGame() throws InterruptedException {

       isRunning = true;

 

       JFrame frame = configureWindow();

       Canvas canvas = configureView(frame);

       List<Entity> entities = createEntities();

 

       canvas.setIgnoreRepaint(true);

       canvas.createBufferStrategy(2);

       canvas.requestFocus();

 

       List<EntityView> views = new ArrayList<>(entities.size());

 

       for (Entity model : entities) {

           views.add(new AlienView(model));

       }

 

       BufferStrategy strategy = canvas.getBufferStrategy();

 

       while (isRunning) {

           Graphics2D g = (Graphics2D) strategy.getDrawGraphics();

           g.setColor(Color.black);

           g.fillRect(0, 0, 800, 600);

 

           for (EntityView view : views) {

               view.update();

               view.draw(g);

           }

           g.dispose();

           strategy.show();

           

           TimeUnit.MILLISECONDS.sleep(1000);

       }

   }
We create a graphic “view” for each model and then we paint them all to the screen in the loop every 1000 milliseconds.

And there we see our alien place holders all set up.

 

Next time, we’ll add Timing, Inputs, player action, game logic and animation!

 

The source for this can be found at:

https://github.com/Drakoskai/Derp-Invaders-One

 

Author: Bruce Brown

Share This Post On

Submit a Comment

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