//Avoid asteroid game v. 2 - by Katie Davis and Zoe Wood May 2015 for PCS 5th grade //Simple game with approximate collisions with falling asteroids - use the mouse //to move the main character and avoid the asteroids //hit the 'r' key to reset the game - when the creature is hit, the asteroids stop and //the creature changes color //Look for multiple - TODOs that allow students to modify the game // //Stars picture from http://khongthe.com/wallpapers/space/bright-cold-stars-of-space-244676.jpg //but any image can be used as long as it is loaded in and the image is in the same directory int x1, y1, x2, y2, x3, y3, x4, y4, x5, y5; int speed1, speed2, speed3, speed4, speed5; final int picWidth = 432, picHeight = 768; int collideWidth; boolean collide; color collideColor; PImage backPic; // TODO: Fill in drawCreature to draw your own creature! // Can you figure out how to change the color of your create when you // collide with the balls? // Hint: look for the line "if (collide)" as an example void drawCreature(int tx, int ty) { strokeWeight(1); pushMatrix(); //TODO when replacing with your creature you will likely need to change these - delete rotate //and adjust the translate and scale to fit your creature translate(tx, ty); scale(.3); translate(-200, -200); if (collide) { fill(collideColor); } else { fill(245, 144, 12); } beginShape(); vertex(111, 230); vertex(115, 230); vertex(117, 210); vertex(417, 119); vertex(416, 118); endShape(CLOSE); fill(19, 100, 14); ellipse(250, 250, 400, 400); //DO NOT DELETE popMatrix(); } //TODO - change this if you want to draw something other then an asteroid - just be sure to //draw centered at the variables tx, ty void drawAsteroid(int tx, int ty) { noStroke(); fill(128, 128, 128); ellipse(tx, ty, 40, 40); fill(168, 168, 168); ellipse(tx, ty, 30, 30); fill(188, 188, 188); ellipse(tx, ty, 20, 20); } void setup() { size(picWidth, picHeight); //TODO, you can change which image you want to use by editing the file name below! backPic = loadImage("face4.jpg"); //TODO change this if you change the shape of the asteroids collideWidth = 70; y1 = y2 = y3 = y4 = y5 = 0; x1 = int(random(picWidth)); x2 = int(random(picWidth)); x3 = int(random(picWidth)); x4 = int(random(picWidth)); x5 = int(random(picWidth)); //TODO - you can edit the speeds here as well if you want to change how fast the asteroids go speed1 = int(random(1, 8)); speed2 = int(random(1, 8)); speed3 = int(random(1, 8)); speed4 = int(random(1, 8)); speed5 = int(random(1, 8)); mouseX = mouseY = 200; collide = false; } void updatePoints() { y1 += speed1; y2 += speed2; y3 += speed3; y4 += speed4; y5 += speed5; if (y1 > picHeight) { y1 = 0; x1 = int(random(picWidth)); speed1 = int(random(1, 8)); } if (y2 > picHeight) { y2 = 0; x2 = int(random(picWidth)); speed2 = int(random(1, 8)); } if (y3 > picHeight) { y3 = 0; x3 = int(random(picWidth)); speed3 = int(random(1, 8)); } if (y4 > picHeight) { y4 = 0; x4 = int(random(picWidth)); speed4 = int(random(1, 8)); } if (y5 > picHeight) { y5 = 0; x5 = int(random(picWidth)); speed5 = int(random(1, 8)); } } boolean checkCollisions() { int offSetY, offSetX; offSetX = collideWidth-5; offSetY = collideWidth; if (x1 < mouseX + offSetX && x1 > mouseX && y1 < mouseY + offSetY && y1 > mouseY) { collide = true; return true; } if (x2 < mouseX + offSetX && x2 > mouseX && y2 < mouseY + offSetY && y2 > mouseY) { collide = true; return true; } if (x3 < mouseX + offSetX && x3 > mouseX && y3 < mouseY + offSetY && y3 > mouseY) { collide = true; return true; } if (x4 < mouseX + offSetX && x4 > mouseX && y4 < mouseY + offSetY && y4 > mouseY) { collide = true; return true; } if (x5 < mouseX + offSetX && x5 > mouseX && y5 < mouseY + offSetY && y5 > mouseY) { collide = true; return true; } return false; } void draw() { background(128); image(backPic, 0, 0); //draw the asteroids updatePoints(); drawAsteroid(x1, y1); drawAsteroid(x2, y2); drawAsteroid(x3, y3); drawAsteroid(x4, y4); drawAsteroid(x5, y5); if (checkCollisions()) { speed1 = speed2 = speed3 = speed4 = speed5 = 0; //TODO - change the collide color if you want collideColor = color(255, 255, 0); } drawCreature(mouseX, mouseY); //TODO - if you want - you can change the win condition - how long do you have to survive?? fill(0); text("Time: " + frameCount, 20, 20); if (frameCount > 200) { text("You Won!!!", picWidth/2, picHeight/2); speed1 = speed2 = speed3 = speed4 = speed5 = 0; } } void keyPressed() { //when you press the 'a' key the witch will move to the left if (key == 'r') { ResetGame(); } //TODO - you can add other keys that do different things if you want } void ResetGame() { y1 = y2 = y3 = y4 = y5 = 0; collide = false; x1 = int(random(picWidth)); x2 = int(random(picWidth)); x3 = int(random(picWidth)); x4 = int(random(picWidth)); x5 = int(random(picWidth)); speed1 = int(random(1, 8)); speed2 = int(random(1, 8)); speed3 = int(random(1, 8)); speed4 = int(random(1, 8)); speed5 = int(random(1, 8)); }