Monday, November 4, 2013

Beginning a Simple Pong Game

We're taking a brief break from our game code to start a project from scratch -- a good thing to do every now and then.

I used Photoshop to create 3 game pieces which I saved as PNG's to preserve the transparency:

  • Left Paddle
  • Right Paddle
  • Ball

I began a new Greenfoot scenario which I called MyPong -- of course you can call yours anything you want. I created a new subclass of World and called it "Board".  \

Next I created three subclasses of Actor and assigned the images (above):

  • Paddle1
  • Paddle2
  • Ball

My Board class looks like this:

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Board here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Board extends World
{

    /**
     * Constructor for objects of class Board.
     * 
     */
    public Board()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(640, 480, 1); 
        Ball ball = new Ball();
        addObject(ball, 320, 240);
        Paddle1 paddle1 = new Paddle1();
        addObject(paddle1, 10, 240);
        Paddle2 paddle2 = new Paddle2();
        addObject(paddle2, 630, 240);
    }
}
I then "Compiled" my game to verify that everything looked right.

No comments:

Post a Comment