Introduction to programming class 12

21
Introduction to Programming Class 12 – A Complete Game Paul Brebner

Transcript of Introduction to programming class 12

Page 1: Introduction to programming   class 12

Introduction to ProgrammingClass 12 – A Complete Game

Paul Brebner

Page 2: Introduction to programming   class 12

Game design – car and traffic lights

Page 3: Introduction to programming   class 12

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

Page 4: Introduction to programming   class 12

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;

Page 5: Introduction to programming   class 12

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

Page 6: Introduction to programming   class 12

Setup and draw

void setup(){

size(windowSize, windowSize);frameRate(25);background(0);rectMode(CENTER);reset();

}

void draw(){

runGame();}

Page 7: Introduction to programming   class 12

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;

}

Page 8: Introduction to programming   class 12

Main actions of gamevoid runGame(){

background(0);

if (gameOver){ // TODO Later}else{

drawCar();drawTarget();drawReds();carMove();checkBoundary();hitTarget();hitReds();scoreBoard();

}}

Page 9: Introduction to programming   class 12

Make empty functionsvoid drawCar(){}

etc

Page 10: Introduction to programming   class 12

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(); }

}

Page 11: Introduction to programming   class 12

carMove()

void carMove(){

carX += moveX*speed;carY += moveY*speed;

}

Page 12: Introduction to programming   class 12

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);

}

Page 13: Introduction to programming   class 12

hitTarget() [simple]

void hitTarget(){

if (dist(targetX, targetY, carX, carY) <= crashSize){

newTarget = true;speed += 0.3;score++;

}}

Page 14: Introduction to programming   class 12

scoreBoard()

void scoreBoard(){

String s = "SCORE " + score + " SPEED " + speed;textAlign(LEFT);textFont(Font);fill(237,149,0);text(s,10,30,40);

}

Page 15: Introduction to programming   class 12

checkBoundary()

void checkBoundary(){

if (carX > ?|| carX < ? || carY > ?|| carY < ?)gameOver = true;

}

Page 16: Introduction to programming   class 12

checkBoundary()

void checkBoundary(){

if (carX > windowSize || carX < 0 || carY > windowSize || carY < 0)gameOver = true;

}

Page 17: Introduction to programming   class 12

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;

}}

}

Page 18: Introduction to programming   class 12

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;

}

Page 19: Introduction to programming   class 12

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

Page 20: Introduction to programming   class 12

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++;

}}

Page 21: Introduction to programming   class 12

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