introduction to programming class 12
TRANSCRIPT
Introduction to ProgrammingClass 12 – A Complete Game
Paul Brebner
Game design – car and traffic lights
Design
• Green car• Drive it around• Goal is to hit Green Traffic lights• Once hit, green lights go RED• Don’t hit RED lights again or gameover• Don’t hit edge of screen of gameover• Score for each green light hit• Speed increases for each green light hit• Display score in top left• Display end of game message in middle
Variables (1)color carColor = color(0,0,255); // blue for carcolor targetColor = color(0,255,0); // green for GO!color redColor = color(255,0,0); // red for dangerint moveX = 0; // -1, 0, or 1 for directionint moveY = 0;int carX = 0; // car x y locationint carY = 0;int targetX = 0; // green light x y locationint targetY = 0;
Variables (2)
boolean newTarget = true; // create new targetint reds = 0; // current number of redsint maxReds = 5; // max possible redsint[] redX = new int[maxReds]; // red light array x locationsint[] redY = new int[maxReds]; // red light array y locationsint windowSize = 500; // screen sizeboolean gameOver = false; // while true keep playingPFont Font = createFont("Arial",20, true); // font for scores etcint score = 0; // player scorefloat startSpeed = 2; // start speed in pixelsfloat speed = startSpeed; // car speedint carSize = 10; // car size (width)int lightSize = 10; // red and green light size (diameter)int crashSize = carSize/2+lightSize/2; // how close car and light get before crash
Setup and draw
void setup(){
size(windowSize, windowSize);frameRate(25);background(0);rectMode(CENTER);reset();
}
void draw(){
runGame();}
Divide program into functionsreset() used at start and end to set
variables to start values
void reset(){
carX = width/2;carY = height/2;gameOver = false;newTarget = true;moveY = 0;moveX = 0;speed = startSpeed;reds = 0;score = 0;
}
Main actions of gamevoid runGame(){
background(0);
if (gameOver){ // TODO Later}else{
drawCar();drawTarget();drawReds();carMove();checkBoundary();hitTarget();hitReds();scoreBoard();
}}
Make empty functionsvoid drawCar(){}
etc
Fill in detailsvoid drawCar(){
fill(carColor);rect(carX, carY, carSize, carSize);
}
// allow movementvoid keyPressed(){
if (keyCode == UP) { moveY = -1; moveX = 0; }if (keyCode == DOWN) { moveY = 1; moveX = 0; }if (keyCode == LEFT) { moveX = -1; moveY = 0; }if (keyCode == RIGHT) { moveX = 1; moveY = 0; }if (keyCode == 'R') { reset(); }
}
carMove()
void carMove(){
carX += moveX*speed;carY += moveY*speed;
}
drawTarget()
void drawTarget(){
fill(targetColor);
if (newTarget) // if newTarget work out location for it{
targetX = (int)random(lightSize, windowSize-lightSize);targetY = (int)random(lightSize, windowSize-lightSize);newTarget = false;
}ellipse(targetX, targetY, lightSize, lightSize);
}
hitTarget() [simple]
void hitTarget(){
if (dist(targetX, targetY, carX, carY) <= crashSize){
newTarget = true;speed += 0.3;score++;
}}
scoreBoard()
void scoreBoard(){
String s = "SCORE " + score + " SPEED " + speed;textAlign(LEFT);textFont(Font);fill(237,149,0);text(s,10,30,40);
}
checkBoundary()
void checkBoundary(){
if (carX > ?|| carX < ? || carY > ?|| carY < ?)gameOver = true;
}
checkBoundary()
void checkBoundary(){
if (carX > windowSize || carX < 0 || carY > windowSize || carY < 0)gameOver = true;
}
hitTarget() [full]
void hitTarget(){
if (dist(targetX, targetY, carX, carY) <= crashSize){
newTarget = true;speed += 0.3;score++;reds++;if (reds >= maxReds) // no more room in array!
gameOver = true;else{
// new red where target wasredX[reds] = targetX;redY[reds] = targetY;
}}
}
hitReds()
void hitReds(){
// Tricky! i < reds so you don't die immediately by hitting the green light that turned red// What if it is i <= reds?for (int i=0; i < reds; i++)
if (dist(carX, carY, redX[i], redY[i]) <= crashSize)gameOver = true;
}
runGame()code for gameOver == true
void runGame(){
background(0);
if (gameOver){
scoreBoard();String s = "Game over! You scored " + score;textAlign(CENTER);textFont(Font);fill(100,200,50);text(s,width/2,height/2,40);
}else // code from before
Tricky version (V2)void hitTarget(){float d = dist(targetX, targetY, carX, carY);boolean hit = (d <= crashSize);
if (hit || (d < 100 && random(0,1) >= 0.99)){
newTarget = true;speed += 0.3;reds++;if (reds >= maxReds) // no more room in array!gameOver = true;
else{// new red where target wasredX[reds] = targetX;redY[reds] = targetY;
}if (hit) score++;
}}
Extensions
• Add a time limit to hit light then move it
• Give the red lights speed so they move around• Need another 2 arrays for x and y speed
• As game progresses create new reds with higher speed