SlideShare a Scribd company logo
Introduction to Programming
Class 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 car
color targetColor = color(0,255,0); // green for GO!
color redColor = color(255,0,0); // red for danger
int moveX = 0; // -1, 0, or 1 for direction
int moveY = 0;
int carX = 0; // car x y location
int carY = 0;
int targetX = 0; // green light x y location
int targetY = 0;
Variables (2)
boolean newTarget = true; // create new target
int reds = 0; // current number of reds
int maxReds = 5; // max possible reds
int[] redX = new int[maxReds]; // red light array x locations
int[] redY = new int[maxReds]; // red light array y locations
int windowSize = 500; // screen size
boolean gameOver = false; // while true keep playing
PFont Font = createFont("Arial",20, true); // font for scores etc
int score = 0; // player score
float startSpeed = 2; // start speed in pixels
float speed = startSpeed; // car speed
int 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 functions
reset() 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 game
void runGame()
{
background(0);
if (gameOver)
{ // TODO Later
}
else
{
drawCar();
drawTarget();
drawReds();
carMove();
checkBoundary();
hitTarget();
hitReds();
scoreBoard();
}
}
Make empty functions
void drawCar()
{
}
etc
Fill in details
void drawCar()
{
fill(carColor);
rect(carX, carY, carSize, carSize);
}
// allow movement
void 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 was
redX[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 was
redX[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

More Related Content

PPTX
7. chapter vi
PDF
pptuni1
DOCX
Designing the application
PDF
Interactive Mouse (Report On Processing)
PPTX
Introduction to Programming (well, kind of.)
PPTX
Interesting Things - June 2015 slides
PPTX
Boys vs Girls Puberty Quiz
PPTX
C language mini project By Arbab Ahmed
7. chapter vi
pptuni1
Designing the application
Interactive Mouse (Report On Processing)
Introduction to Programming (well, kind of.)
Interesting Things - June 2015 slides
Boys vs Girls Puberty Quiz
C language mini project By Arbab Ahmed

Viewers also liked (10)

DOCX
Converter - C- Language Program
PDF
Introduction to programming - class 10
PPTX
Random Quiz Maker in C Language Project Slide
PPTX
Umesh Taglani,Project on C language,Final Year BCA ,Dezyne E'cole College
DOC
C programming project by navin thapa
PPT
Programming languages
PPTX
Computer Languages.
PPTX
C Programming Language Tutorial for beginners - JavaTpoint
PPT
Lect 1. introduction to programming languages
PPSX
INTRODUCTION TO C PROGRAMMING
Converter - C- Language Program
Introduction to programming - class 10
Random Quiz Maker in C Language Project Slide
Umesh Taglani,Project on C language,Final Year BCA ,Dezyne E'cole College
C programming project by navin thapa
Programming languages
Computer Languages.
C Programming Language Tutorial for beginners - JavaTpoint
Lect 1. introduction to programming languages
INTRODUCTION TO C PROGRAMMING
Ad

Similar to Introduction to programming class 12 (16)

PDF
Creating an Uber Clone - Part XXII - Transcript.pdf
PDF
[EN] Ada Lovelace Day 2014 - Tampon run
PDF
building_games_with_ruby_rubyconf
PDF
building_games_with_ruby_rubyconf
PPTX
The C# programming laguage delegates notes Delegates.pptx
PDF
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
PDF
c++ code#include iostream#include string#include stdlib.h.pdf
PDF
David Kopal - Write better React with ReasonML - Codemotion Milan 2018
PDF
Creating an Uber Clone - Part XXX - Transcript.pdf
PDF
[SI] Ada Lovelace Day 2014 - Tampon Run
PDF
#include iostream #include string #include iomanip #incl.pdf
PDF
Pygame presentation
KEY
Proga 0608
PDF
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
PDF
What's wrong with my code- I am coding a battleship game that uses a d (1).pdf
PDF
The Ring programming language version 1.5 book - Part 2 of 31
Creating an Uber Clone - Part XXII - Transcript.pdf
[EN] Ada Lovelace Day 2014 - Tampon run
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
The C# programming laguage delegates notes Delegates.pptx
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
c++ code#include iostream#include string#include stdlib.h.pdf
David Kopal - Write better React with ReasonML - Codemotion Milan 2018
Creating an Uber Clone - Part XXX - Transcript.pdf
[SI] Ada Lovelace Day 2014 - Tampon Run
#include iostream #include string #include iomanip #incl.pdf
Pygame presentation
Proga 0608
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
What's wrong with my code- I am coding a battleship game that uses a d (1).pdf
The Ring programming language version 1.5 book - Part 2 of 31
Ad

More from Paul Brebner (20)

PPTX
Streaming More For Less With Apache Kafka Tiered Storage
PDF
30 Of My Favourite Open Source Technologies In 30 Minutes
PDF
Superpower Your Apache Kafka Applications Development with Complementary Open...
PDF
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
PDF
Architecting Applications With Multiple Open Source Big Data Technologies
PDF
The Impact of Hardware and Software Version Changes on Apache Kafka Performan...
PDF
Apache ZooKeeper and Apache Curator: Meet the Dining Philosophers
PDF
Spinning your Drones with Cadence Workflows and Apache Kafka
PDF
Change Data Capture (CDC) With Kafka Connect® and the Debezium PostgreSQL Sou...
PDF
Scaling Open Source Big Data Cloud Applications is Easy/Hard
PDF
OPEN Talk: Scaling Open Source Big Data Cloud Applications is Easy/Hard
PDF
A Visual Introduction to Apache Kafka
PDF
Massively Scalable Real-time Geospatial Anomaly Detection with Apache Kafka a...
PDF
Building a real-time data processing pipeline using Apache Kafka, Kafka Conne...
PDF
Grid Middleware – Principles, Practice and Potential
PDF
Grid middleware is easy to install, configure, secure, debug and manage acros...
PPTX
Massively Scalable Real-time Geospatial Data Processing with Apache Kafka and...
PPTX
Massively Scalable Real-time Geospatial Data Processing with Apache Kafka and...
PPTX
Melbourne Big Data Meetup Talk: Scaling a Real-Time Anomaly Detection Applica...
PPTX
Massively Scalable Real-time Geospatial Data Processing with Apache Kafka and...
Streaming More For Less With Apache Kafka Tiered Storage
30 Of My Favourite Open Source Technologies In 30 Minutes
Superpower Your Apache Kafka Applications Development with Complementary Open...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Architecting Applications With Multiple Open Source Big Data Technologies
The Impact of Hardware and Software Version Changes on Apache Kafka Performan...
Apache ZooKeeper and Apache Curator: Meet the Dining Philosophers
Spinning your Drones with Cadence Workflows and Apache Kafka
Change Data Capture (CDC) With Kafka Connect® and the Debezium PostgreSQL Sou...
Scaling Open Source Big Data Cloud Applications is Easy/Hard
OPEN Talk: Scaling Open Source Big Data Cloud Applications is Easy/Hard
A Visual Introduction to Apache Kafka
Massively Scalable Real-time Geospatial Anomaly Detection with Apache Kafka a...
Building a real-time data processing pipeline using Apache Kafka, Kafka Conne...
Grid Middleware – Principles, Practice and Potential
Grid middleware is easy to install, configure, secure, debug and manage acros...
Massively Scalable Real-time Geospatial Data Processing with Apache Kafka and...
Massively Scalable Real-time Geospatial Data Processing with Apache Kafka and...
Melbourne Big Data Meetup Talk: Scaling a Real-Time Anomaly Detection Applica...
Massively Scalable Real-time Geospatial Data Processing with Apache Kafka and...

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Classroom Observation Tools for Teachers
PPTX
Lesson notes of climatology university.
PPTX
GDM (1) (1).pptx small presentation for students
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Institutional Correction lecture only . . .
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
01-Introduction-to-Information-Management.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Structure & Organelles in detailed.
Classroom Observation Tools for Teachers
Lesson notes of climatology university.
GDM (1) (1).pptx small presentation for students
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Supply Chain Operations Speaking Notes -ICLT Program
Abdominal Access Techniques with Prof. Dr. R K Mishra
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
RMMM.pdf make it easy to upload and study
Final Presentation General Medicine 03-08-2024.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Microbial diseases, their pathogenesis and prophylaxis
Institutional Correction lecture only . . .
VCE English Exam - Section C Student Revision Booklet
01-Introduction-to-Information-Management.pdf

Introduction to programming class 12

  • 1. Introduction to Programming Class 12 – A Complete Game Paul Brebner
  • 2. Game design – car and traffic lights
  • 3. 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
  • 4. Variables (1) color carColor = color(0,0,255); // blue for car color targetColor = color(0,255,0); // green for GO! color redColor = color(255,0,0); // red for danger int moveX = 0; // -1, 0, or 1 for direction int moveY = 0; int carX = 0; // car x y location int carY = 0; int targetX = 0; // green light x y location int targetY = 0;
  • 5. Variables (2) boolean newTarget = true; // create new target int reds = 0; // current number of reds int maxReds = 5; // max possible reds int[] redX = new int[maxReds]; // red light array x locations int[] redY = new int[maxReds]; // red light array y locations int windowSize = 500; // screen size boolean gameOver = false; // while true keep playing PFont Font = createFont("Arial",20, true); // font for scores etc int score = 0; // player score float startSpeed = 2; // start speed in pixels float speed = startSpeed; // car speed int 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
  • 6. Setup and draw void setup() { size(windowSize, windowSize); frameRate(25); background(0); rectMode(CENTER); reset(); } void draw() { runGame(); }
  • 7. Divide program into functions reset() 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; }
  • 8. Main actions of game void runGame() { background(0); if (gameOver) { // TODO Later } else { drawCar(); drawTarget(); drawReds(); carMove(); checkBoundary(); hitTarget(); hitReds(); scoreBoard(); } }
  • 9. Make empty functions void drawCar() { } etc
  • 10. Fill in details void drawCar() { fill(carColor); rect(carX, carY, carSize, carSize); } // allow movement void 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(); } }
  • 11. carMove() void carMove() { carX += moveX*speed; carY += moveY*speed; }
  • 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); }
  • 13. hitTarget() [simple] void hitTarget() { if (dist(targetX, targetY, carX, carY) <= crashSize) { newTarget = true; speed += 0.3; score++; } }
  • 14. scoreBoard() void scoreBoard() { String s = "SCORE " + score + " SPEED " + speed; textAlign(LEFT); textFont(Font); fill(237,149,0); text(s,10,30,40); }
  • 15. checkBoundary() void checkBoundary() { if (carX > ?|| carX < ? || carY > ?|| carY < ?) gameOver = true; }
  • 16. checkBoundary() void checkBoundary() { if (carX > windowSize || carX < 0 || carY > windowSize || carY < 0) gameOver = true; }
  • 17. 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 was redX[reds] = targetX; redY[reds] = targetY; } } }
  • 18. 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; }
  • 19. 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
  • 20. 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 was redX[reds] = targetX; redY[reds] = targetY; } if (hit) score++; } }
  • 21. 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