0% found this document useful (0 votes)
81 views

IIMS College: Putalisadak, Kathmandu, Nepal

This document describes a simple car racing game built in C++. It includes the source code and functions used to draw the car, track, and obstacles. The objective is to control a car to avoid obstacles and get the highest score by surviving as long as possible on the track while the speed increases over time.

Uploaded by

sunil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

IIMS College: Putalisadak, Kathmandu, Nepal

This document describes a simple car racing game built in C++. It includes the source code and functions used to draw the car, track, and obstacles. The objective is to control a car to avoid obstacles and get the highest score by surviving as long as possible on the track while the speed increases over time.

Uploaded by

sunil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

IIMS College

Putalisadak, Kathmandu, Nepal

Name of Course Instructor: Rona Laxmi Maharjan

Course Code: CC206n_ Course Name: Multimedia Programming

Program Name: B.sc (Hons)Computing Semester: 8th Batch: Sept 2018

Assignment I / II / III: Please Tick (√ ) Assignment Type (Individual/Group): Group

Assignment Title: _ Assignment

Max. Marks: ______ Date of Allotment: ___________ Date of Submission:

Name of the Student UCSI ID number Contact Number Email Id


Sunil Basnet 1001852837 9868904541 [email protected]

Evaluation: ________________________ obtained out


of _____________________________________
Evaluator’s Comment:
_____________________________________________________________________________
_
_____________________________________________________________________________
_
-------------------------------------------

Evaluator’s Signature & Date


Game Report

Introduction

Games have become an integral part of everyday life for many people. For this project we build
simple car racing game. We try to implement a simple car game based on basic design. The
objective of this game is to survive as long as possible and get to the high scores in the shortest
possible time while avoiding the obstacles on the tracks. use graphics.h which provide direct
functions to draw different coordinate shapes (like circle, rectangle etc.). By using these
functions, we can draw different objects like car, hut, trees etc. In this program, we will draw a
moving car using line and circles.

Source code:

#include<iostream>
#include<conio.h>
#include<dos.h>
#include <windows.h>
#include <time.h>

#define SCREEN_WIDTH 90
#define SCREEN_HEIGHT 26
#define WIN_WIDTH 70

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);


COORD CursorPosition;

int enemyY[3];
int enemyX[3];
int enemyFlag[3];
char car[4][4] = { ' ','±','±',' ',
'±','±','±','±',
' ','±','±',' ',
'±','±','±','±' };
int carPos = WIN_WIDTH/2;
int score = 0;

void gotoxy(int x, int y){


CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console, CursorPosition);
}
void setcursor(bool visible, DWORD size) {
if(size == 0)
size = 20;

CONSOLE_CURSOR_INFO lpCursor;
lpCursor.bVisible = visible;
lpCursor.dwSize = size;
SetConsoleCursorInfo(console,&lpCursor);
}
void drawBorder(){
for(int i=0; i<SCREEN_HEIGHT; i++){
for(int j=0; j<17; j++){
gotoxy(0+j,i); cout<<"±";
gotoxy(WIN_WIDTH-j,i); cout<<"±";
}
}
for(int i=0; i<SCREEN_HEIGHT; i++){
gotoxy(SCREEN_WIDTH,i); cout<<"±";
}
}
void genEnemy(int ind){
enemyX[ind] = 17 + rand()%(33);
}
void drawEnemy(int ind){
if( enemyFlag[ind] == true ){
gotoxy(enemyX[ind], enemyY[ind]); cout<<"****";
gotoxy(enemyX[ind], enemyY[ind]+1); cout<<" ** ";
gotoxy(enemyX[ind], enemyY[ind]+2); cout<<"****";
gotoxy(enemyX[ind], enemyY[ind]+3); cout<<" ** ";
}
}
void eraseEnemy(int ind){
if( enemyFlag[ind] == true ){
gotoxy(enemyX[ind], enemyY[ind]); cout<<" ";
gotoxy(enemyX[ind], enemyY[ind]+1); cout<<" ";
gotoxy(enemyX[ind], enemyY[ind]+2); cout<<" ";
gotoxy(enemyX[ind], enemyY[ind]+3); cout<<" ";
}
}
void resetEnemy(int ind){
eraseEnemy(ind);
enemyY[ind] = 1;
genEnemy(ind);
}

void drawCar(){
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
gotoxy(j+carPos, i+22); cout<<car[i][j];
}
}
}
void eraseCar(){
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
gotoxy(j+carPos, i+22); cout<<" ";
}
}
}

int collision(){
if( enemyY[0]+4 >= 23 ){
if( enemyX[0] + 4 - carPos >= 0 && enemyX[0] + 4 - carPos <
9 ){
return 1;
}
}
return 0;
}
void gameover(){
system("cls");
cout<<endl;
cout<<"\t\t--------------------------"<<endl;
cout<<"\t\t-------- Game Over -------"<<endl;
cout<<"\t\t--------------------------"<<endl<<endl;
cout<<"\t\tPress any key to go back to menu.";
getch();
}
void updateScore(){
gotoxy(WIN_WIDTH + 7, 5);cout<<"Score: "<<score<<endl;
}

void instructions(){

system("cls");
cout<<"Instructions";
cout<<"\n----------------";
cout<<"\n Avoid Cars by moving left or right. ";
cout<<"\n\n Press 'a' to move left";
cout<<"\n Press 'd' to move right";
cout<<"\n Press 'escape' to exit";
cout<<"\n\nPress any key to go back to menu";
getch();
}

void play(){
carPos = -1 + WIN_WIDTH/2;
score = 0;
enemyFlag[0] = 1;
enemyFlag[1] = 0;
enemyY[0] = enemyY[1] = 1;

system("cls");
drawBorder();
updateScore();
genEnemy(0);
genEnemy(1);

gotoxy(WIN_WIDTH + 7, 2);cout<<"Car Game";


gotoxy(WIN_WIDTH + 6, 4);cout<<"----------";
gotoxy(WIN_WIDTH + 6, 6);cout<<"----------";
gotoxy(WIN_WIDTH + 7, 12);cout<<"Control ";
gotoxy(WIN_WIDTH + 7, 13);cout<<"-------- ";
gotoxy(WIN_WIDTH + 2, 14);cout<<" A Key - Left";
gotoxy(WIN_WIDTH + 2, 15);cout<<" D Key - Right";

gotoxy(18, 5);cout<<"Press any key to start";


getch();
gotoxy(18, 5);cout<<" ";

while(1){
if(kbhit()){
char ch = getch();
if( ch=='a' || ch=='A' ){
if( carPos > 18 )
carPos -= 4;
}
if( ch=='d' || ch=='D' ){
if( carPos < 50 )
carPos += 4;
}
if(ch==27){
break;
}
}

drawCar();
drawEnemy(0);
drawEnemy(1);
if( collision() == 1 ){
gameover();
return;
}
Sleep(50);
eraseCar();
eraseEnemy(0);
eraseEnemy(1);

if( enemyY[0] == 10 )
if( enemyFlag[1] == 0 )
enemyFlag[1] = 1;

if( enemyFlag[0] == 1 )
enemyY[0] += 1;

if( enemyFlag[1] == 1 )
enemyY[1] += 1;

if( enemyY[0] > SCREEN_HEIGHT-4 ){


resetEnemy(0);
score++;
updateScore();
}
if( enemyY[1] > SCREEN_HEIGHT-4 ){
resetEnemy(1);
score++;
updateScore();
}
}
}

int main()
{
setcursor(0,0);
srand( (unsigned)time(NULL));

do{
system("cls");
gotoxy(10,5); cout<<" -------------------------- ";
gotoxy(10,6); cout<<" | Car Game | ";
gotoxy(10,7); cout<<" --------------------------";
gotoxy(10,9); cout<<"1. Start Game";
gotoxy(10,10); cout<<"2. Instructions";
gotoxy(10,11); cout<<"3. Quit";
gotoxy(10,13); cout<<"Select option: ";
char op = getche();

if( op=='1') play();


else if( op=='2') instructions();
else if( op=='3') exit(0);

}while(1);

return 0;
}
Output:
Video Report

Introduction:

We use graphics.h which provide direct functions to draw different coordinate shapes (like
circle, rectangle etc.). A car is made using two rectangles and two circles which act as tires of the
car. A for loop is used to move the car forward by changing the rectangle and circle coordinates
and erasing the previous contents on screen using clear viewport, you can also use clear device.
Speed of car can be adjusted using delay function, more the delay lesser will be the speed or
lesser the delay your car will move fast. In this program color of the car also keeps on changing,
this is accomplished by incrementing the color value by one each time in the for loop, you can
also use random function for this purpose. Before you see a car moving you will be asked to
press a key.

Functions used in program:

delay(n): This function is used for holding the program output for a small period of time since
processing is very fast so use it to see the result.

line (x1, y1, x2, y2): A function from graphics.h header file which draw a line with (x1, y1) as
first coordinate of line and (x2, y2) as second coordinate of the line.

circle (x, y, r): A function from graphics.h header file which draw a circle with center (x, y) and
radius r.

Source code:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void full();
void car();
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");
full();
car();
getch();
closegraph();
}
void full()
{
//two line
line(1,150,800,150);
line(1,380,800,380);

//1st mountain
line(100,70,1,150);
line(100,70,200,150);

//2nd mountain
line(300,70,200,150);
line(300,70,400,150);

//3rd mountain
line(500,70,400,150);
line(500,70,600,150);

//void space
line(600,150,700,1);

//sun
ellipse(200,130,20,160,50,50);
}

void car()
{
int i;
for(i=1;i<600;i++)
{
full();
line(100+i,350,100+i,370);
line(100+i,350,110+i,350);
line(110+i,350,130+i,325);
line(130+i,325,190+i,325);
line(190+i,325,210+i,350);
line(210+i,350,240+i,350);
line(240+i,350,240+i,370);
//adjust whele
line(100+i,370,110+i,370);
line(130+i,370,210+i,370);
line(230+i,370,240+i,370);

//middle window
line(120+i,350,135+i,330);
line(120+i,350,160+i,350);
line(160+i,350,160+i,330);
line(160+i,330,135+i,330);

//right window
line(165+i,350,165+i,330);
line(188+i,330,205+i,350);
line(165+i,350,205+i,350);
line(165+i,330,188+i,330);

circle(120+i,370,10);
circle(220+i,370,10);

//right whele
pieslice(120+i,370,0-i,1-i,10);

pieslice(120+i,370,90-i,91-i,10);

pieslice(120+i,370,180-i,181-i,10);

pieslice(120+i,370,270-i,271-i,10);

//right whele
pieslice(220+i,370,0-i,1-i,10);

pieslice(220+i,370,90-i,91-i,10);

pieslice(220+i,370,180-i,181-i,10);

pieslice(220+i,370,270-i,271-i,10);

delay(100);
cleardevice();
}

}
Output:

You might also like