Open a new Processing sketch, paste in the code below and save it as Defenderz. On moodle you will find a background gif file. Download the file to your Defenderz directory. The code below will give you a horiztonal scrolling background image which wraps around.

PImage background;
int x=0; //global variable background location

void setup() {
size(800, 400);
background = loadImage("spaceBackground.jpg");
background.resize(width,height);
}

void draw() {
image(background, x, 0); //draw background twice adjacent
image(background, x+background.width, 0);
x -= 4;
if(x == -background.width)
x=0; //wrap around
}

Defenderz Game

1st prototype has

  • alien (moving left, and perhaps up and down).
    • color ALIEN1 = color(0, 255, 0);
    • color ALIEN2 = color(50, 100, 0);
  • A defender
    • Moved up and down by player
    • Can crash into alien
    • Detect by alien colour (area just in front of the defender)
    • color ALIEN1 = color(0, 255, 0);
    • color ALIEN2 = color(50, 100, 0);

What Classes do we require?

What constructors & methods for each?

Add the defender class (do not use the same name as the sketch! - nested class error)

//draw a defender
fill(255, 0, 0);
rect(x, y, 50, 20);
triangle(x+50, y, x+50, y+20, x+60, y+10);
fill(0, 0, 100);
rect(x, y-10, 20, 10);

Add a defender instance with user interaction (key press move up and down) and test it

Add an Alien class

//draw an alien
fill(ALIEN1);
ellipse(x, y, 30, 20);
fill(ALIEN2);
ellipse(x, y, 50, 15);

Add an instance of an Alien and test it

Add a crash Boolean method to Defender. We could use a distance measure as we have previously (asserting that a crash has taken place if the two objects are within a certain distance) but here we'll use a different technique instead.

A crash has taken place if we detect the alien's colours just in front of the defender ship. To do this we need to check a range of pixels (e.g. yellow are in image) and test whether ALIEN1 colour or ALIEN2 colour is present - how?

FOR LOOP - repeat a block of actions a set number of times
color test = get(x, y);

FOR LOOP - repeat a block of actions a set number of times
color test = get(x, y);
will get the colour of the pixel at x,y, what value should x be - the defender is 60 pixels wide, so defender's x + 65 would be just in front.

if(test == ALIEN1)
Ensure you have drawn the alien on the screen (in draw event) before checking for a crash!!

Complete the 1st prototype of the game
Add 3 aliens (2 more)
Use a gameMode variable to stop the game if a crash occurs
Allow the alien to move up or down in some sequence (as well as left)
If the alien goes off the screen a new alien should appear on the right

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.