Escolar Documentos
Profissional Documentos
Cultura Documentos
CAR RACE
CS10L/Computer Programming Laboratory
Submitted by:
Submitted to:
I. Machine Program Statement CAR RACE is a simple and fun game! Lead the car and avoid blocks on the road. Set the speed of the race from slow to fast. The longer you stay on the race, the higher the score earned. The player has 5 lives and once an obstacle is hit, player life goes down by one. The game is over once player life turns zero.
II. Objective To create a functional computer game using C++ programming language and debug any errors
Functions Function Name coordinates Description sets coordinate (x,y) for positioning of car and obstacles cursor_xy sets console cursor position void Return Type struct Parameters int x, int y, car[], obstacle coordinates cursorCoord field_xy sets coordinate for field void coordinates cursorCoord, char ch textbox_center sets coordinate for text box on right panel of game init_game print_field print_car print_menu print_instructions print_exit move_road move_car new_obstacle detect_obstacle initializes or sets start of game prints game screen prints car prints menu prints instructions prints game over moves obstacles moves car creates obstacle detects if car hit obstacle void void void void void void void void void void none none none none none none none none none none void string text, int y
Variables Identifier Name Data Type obstacleLength obstacleCount score life x y immuneCounter speed delay keyPressed isExit isGame isPaused isMenu isDead isImmune speedCoord lifeCoord scoreCoord cursorCoord int int int int int int int int int char bool bool bool bool bool bool struct struct struct struct global global global global coordinates coordinates main main main main main main main main main main main main main cursor_xy, field_xy, textbox_center, print_field, print_menu, print_exit ch text carIndex char string int field_xy textbox_center init_game, move_car character display text display car positioning/alignment length of obstacle counter for obstacle game score player life x-coordinate y-coordinate counter for immunity speed of race rate of obstacle appearance key pressed determines if game exit determines if game play determines if game pause determines if game menu determines if game over determines immunity coordinate for speed display coordinate for life display coordinate for score display coordinate for cursor Local to Description
number of * printed on screen number of spaces printed number of * printed on (left) game panel
obstacle o oo
direction isObstacleValid c
direction of car movement determines if obstacle created is valid counter for obstacle
V. Screenshots
V. Program Listing
//Project in CS10L Computer Programming //This is a simple car race game made using C++ programming //Submitted by rBermudez, pGoching, cMonteclaro, rNaco and dNavale //Submitted to Engr. Cheryl Mari M. Isip
#include <windows.h> #include <conio.h> #include <ctime> #include <iostream> #include <iomanip> #include <string>
void cursor_xy(coordinates cursorCoord); void field_xy(coordinates cursorCoord, char ch); void textbox_center(string text, int y);
void init_game();
void print_field(); void print_car(); void print_menu(); void print_instructions(); void print_exit();
int main() {
int immuneCounter, speed, delay; char keyPressed; bool isExit = false, isGame = false; bool isPaused = false, isMenu = true, isDead = false; bool isImmune = false;
coordinates speedCoord = {68, 8}; coordinates lifeCoord = {68, 9}; coordinates scoreCoord = {66, 10};
srand(time(NULL));
system("cls"); switch (keyPressed) { case 'a': case 'A': while(isMenu) { system("cls"); cout << "Type the speed 1 (slowest) - 5 (fastest): "; //prompts user-input for car race speed speed = getch(); speed -= 48;
case 'b': case 'B': print_instructions(); _getch(); break; case 'c': case 'C': print_exit(); isExit = true; isMenu = false; break; } }
switch(keyPressed) { case 13: isMenu = true; isGame = false; break; case 27: isPaused = !isPaused; break; } if(!isPaused) { move_car(keyPressed); //function call for car movement move_road(); //function call for obstacle movement
else if(detect_obstacle()) {
score += 5;
Sleep(delay); } }
while(isDead) { system("cls"); print_exit(); cout << "Your score: " << score << endl; cout << "Do you want to play again? (y/n) ";
keyPressed = getch();
switch(keyPressed) { case 'y': system("cls"); init_game(); isGame = true; isDead = false; break; case 'n': cout << endl; isDead = false; isExit = true; break; } } }
return 0; }
SetConsoleCursorPosition(h, coord); }
void textbox_center(string text, int y) { coordinates center; center.x = 65 - (text.length() / 2); center.y = y; cursor_xy(center); cout << text; }
//initialized game
score = 0; life = 5; for(int carIndex = 0; carIndex < 15; carIndex++) //outputs car in screen center { car[carIndex].x = 23 + (carIndex % 5); car[carIndex].y = 19 + (carIndex / 5); }
new_obstacle(); print_car();
textbox_center("CAR RACE!", 6); textbox_center("Speed: 5", 8); textbox_center("Life: 5", 9); textbox_center("Score: 000000", 10); textbox_center("Esc key - Pause / Resume", 12); textbox_center("Enter key - Main Menu", 13);
int filler, spacer; for(filler = 0; filler < 79; filler++) cout << "*"; cout << "\n";
for(int field = 0; field < 23; field++) { cout << "* |"; for(spacer = 0; spacer < 45; spacer++) cout << " "; cout << "| *"; for(spacer = 0; spacer < 25; spacer ++) cout << " "; cout << "*\n"; }
cout << "\n" << "--------------------------------------------------------------------------------" << "______________________________________________________________________________ __" << " " << " << " << " << " << " << " XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXX XX " " " XXXXXXX XXXX XXXXXXX XXXXXXX XXXX XXXXXXX XXXXXXX
XXXXXXXX XXXXXX XX XX XX XX
XXXXXXXX XX XX XX
XX XX XX XX XX
XX XX XX XX XX XX XX XX XX XX XX
XXXXXXX XX XX XX
XX XX XX XXXXXXX XXXXXXX
"
<< "--------------------------------------------------------------------------------" << "______________________________________________________________________________ __" << "\n" << " << " << " << "\n" [A] Play Game [B] Instructions [C] Exit Game " " "
<< " }
void print_instructions() { cout << "\n" <<"How to Play the Game\n" <<"CAR RACE is a simple fun game! Lead the car and avoid blocks on road.\n" <<"You have 5 lives and once you hit an obstacle, your life goes down by one.\n" <<"The game is over when you run out of lives.\n\n" <<"You have to use the following keys to control the car:\n" <<"UP ARROW key - forward movement\n" <<"DOWN ARROW key - backward movement\n" <<"LEFT ARROW key - left movement\n" <<"RIGHT ARROW key - forward movement\n\n" <<"Good luck and enjoy!\n\n" <<"Press any key to return to the main menu."; }
cout << "\n" << "--------------------------------------------------------------------------------" << "______________________________________________________________________________ __" << "XXXXXXXX XXXX XXXX XXXXXXX " XXXX XXXXXXX XXXX XX XX XXXXXXX
XX XX XX XX XX XX XX XX XX XX
XX XX " XX XX "
<< "XX XXXX XXXXXXXX XX XX XX XX XXXXX " << "XX XX XX XX XX XXXX XX XX << "XX XX XX XX XX XX XX XX << "XXXXXXXX XX XX XX
XX XX XX XX XXXXX XXXXXX
XX XX XX XX XX XX XX XX XX XX XXXX
XX XXXXXXX
XX XXXXXXX XX
void move_road() { cursor_xy(obstacle); for(int o = 0; o < obstacleLength; o++) cout << " ";
obstacle.y++;
void move_car(char direction) { for(int carIndex = 0; carIndex < 15; carIndex++) { switch(direction) { case 72: //UP ARROW key for forward movement
print_car(); break; case 75: //LEFT ARROW key for left movement
print_car(); break; case 80: //DOWN ARROW key for backward movement
car[carIndex].y++;
print_car(); break; case 77: //RIGHT ARROW key for right movement
print_car(); break; } } }
void new_obstacle() { bool isObstacleValid = false; while(!isObstacleValid) { obstacleLength = (rand() % 7) + 1; //sets length of obstacle, max is 7
//ensures obstacle
for(int c = 0; c < 15; c++) { if(obstacle.x != car[c].x && obstacle.y != car[c].y) isObstacleValid = true; else { isObstacleValid = false; break; } } }
obstacleCount++; }
bool detect_obstacle() { for(int oo = 0; oo < obstacleLength; oo++) { for(int c = 0; c < 15; c++) { if(car[c].x == (obstacle.x + oo) && car[c].y == obstacle.y) return true; obstacle coordinates are equal //car hit detected if car and
} } return false; }
Member Name
Task
Module Program