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
}
1st prototype has
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