Você está na página 1de 22

MAPUA INSTITUTE OF TECHNOLOGY

Muralla St., Intramuros, Manila

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

III. Data Dictionary

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

filler spacer field

int int int

print_field print_field print_field

number of * printed on screen number of spaces printed number of * printed on (left) game panel

obstacle o oo

struct int int

move_road move_road move_road, new_obstacle, detect_obstacle

obstacle movement counter for obstacle counter for obstacle

direction isObstacleValid c

char bool int

move_car new_obstacle new_obstacle, detect_obstacle

direction of car movement determines if obstacle created is valid counter for obstacle

IV. Program Flowchart

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>

using namespace std;

struct coordinates { int x; int y; } car[15], obstacle;

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

//function prototypes for screen output or display

void move_road(); control void move_car(char direction);

//function prototypes for obstacle movement and car

void new_obstacle(); detection (car hit) bool detect_obstacle();

//function prototypes for obstacle creation and

int obstacleLength; int obstacleCount = 0; int score = 0; int life = 0;

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

while(!isExit) { while(isMenu) { system("cls"); print_menu(); keyPressed = _getch();

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;

if(speed > 0 && speed <= 5) break; }

delay = 50 - (5*speed); race obstacles cursor_xy(speedCoord); cout << speed;

//sets rate of appearance of

init_game(); isGame = true; isMenu = false; break;

case 'b': case 'B': print_instructions(); _getch(); break; case 'c': case 'C': print_exit(); isExit = true; isMenu = false; break; } }

while(isGame) { keyPressed = 0; if(_kbhit())

keyPressed = _getch(); if(keyPressed == char(224)) keyPressed = _getch();

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

if(isImmune) { immuneCounter--; if(immuneCounter == 0) { isImmune = false; print_car(); } }

else if(detect_obstacle()) {

life--; hits obstacle

//player life decrements by 1 when car

immuneCounter = 5; isImmune = true;

cursor_xy(lifeCoord); cout << life; }

if(life == 0) { isDead = true; isGame = false; }

//game over once life turns 0

score += 5;

cursor_xy(scoreCoord); cout << setw(5) << setfill('0') << score;

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

void cursor_xy(coordinates cursorCoord) { COORD coord = {cursorCoord.x, cursorCoord.y}; HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleCursorPosition(h, coord); }

void field_xy(coordinates cursorCoord, char ch) { cursor_xy(cursorCoord); cout << ch;

void textbox_center(string text, int y) { coordinates center; center.x = 65 - (text.length() / 2); center.y = y; cursor_xy(center); cout << text; }

void init_game() { print_field();

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

void print_field() { coordinates cursorCoord = {0,0}; cursor_xy(cursorCoord);

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"; }

for(filler = 0; filler < 79; filler++) cout << "*"; }

void print_car() { cursor_xy(car[0]); cout << " /|\\ "; cursor_xy(car[5]);

cout << "| |"; cursor_xy(car[10]); cout << "|___|"; }

void print_menu() { coordinates cursorCoord = {0,0}; cursor_xy(cursorCoord);

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

XXXXXXX XX " "

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 " " "

<< " }

Please press the letter of your choice. ";

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."; }

void print_exit() { coordinates cursorCoord = {0,0}; cursor_xy(cursorCoord);

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 XX XX

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 XX " XX XX " XX "

XX XXXXXXX

XX XXXXXXX XX

<< "--------------------------------------------------------------------------------" << "______________________________________________________________________________ __\n"; }

void move_road() { cursor_xy(obstacle); for(int o = 0; o < obstacleLength; o++) cout << " ";

obstacle.y++;

if(obstacle.y == 22) { obstacleCount--; new_obstacle(); }

cursor_xy(obstacle); for(int oo = 0; oo < obstacleLength; oo++) cout << "x"; }

void move_car(char direction) { for(int carIndex = 0; carIndex < 15; carIndex++) { switch(direction) { case 72: //UP ARROW key for forward movement

field_xy(car[carIndex], ' '); car[carIndex].y--;

if(car[0].y == 0) { car[0].y = 1; return; }

print_car(); break; case 75: //LEFT ARROW key for left movement

field_xy(car[carIndex], ' '); car[carIndex].x--;

if(car[0].x == 3) { car[0].x = 4; return; }

print_car(); break; case 80: //DOWN ARROW key for backward movement

field_xy(car[carIndex], ' ');

car[carIndex].y++;

if(car[0].y == 22) { car[0].y = 21; return; }

print_car(); break; case 77: //RIGHT ARROW key for right movement

field_xy(car[carIndex], ' '); car[carIndex].x++;

if(car[0].x == 45) { car[0].x = 44; return; }

print_car(); break; } } }

void new_obstacle() { bool isObstacleValid = false; while(!isObstacleValid) { obstacleLength = (rand() % 7) + 1; //sets length of obstacle, max is 7

obstacle.x = (rand() % (44 - obstacleLength)) + 4; appears on allowable portion of screen obstacle.y = 1;

//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; } } }

cursor_xy(obstacle); for(int oo = 0; oo < obstacleLength; oo++) cout << "x";

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

VI. Group Contributions

Member Name

Task

Module Program

Você também pode gostar