Skip to content

Instantly share code, notes, and snippets.

@RishiRaj22 RishiRaj22/game.cpp
Last active Mar 26, 2019

Embed
What would you like to do?
A game made in computer lab that works in Turbo C++
/**
A game made in computer lab that works in Turbo C++
**/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int sign(int t) {
if(t>0) return 1;
else return -1;
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
int hiscore = 0;
start:
// speed of cart
int speed = 250;
// redraw every del milliseconds
int del = 20;
int adjspeed = speed/del;
int orspeed = adjspeed;
int X_MAX = 640;
int Y_MAX = 500;
int GROUND = 360;
int points [] = {30,330,30,360,70,360,90,350,90,330};
int el1x,el2x;
int bx1,bx2;
int ex,ey;
int es;
int score,lives;
score = 0;
lives = 3;
int espeed = adjspeed;
ey = -es;
double sp = 1;
double dlsp = 0.0003;
char sc[15] = "Score: ";
char hs[23] = "High score: ";
char lv[9] = "Lives: ";
char name[30] = "<insert name here>";
char credit[25] = "A game by Rishi Raj";
char help[45] = "Press a and d to steer your trolley";
el1x = 40; el2x = 62;
bx1 = 90;bx2 = 120;
ex = rand() % (X_MAX - 2*(bx2 - bx1))+ (bx2-bx1)/2;
es = 5;
ey = -es;
if(hiscore == 0) {
cleardevice();
settextstyle(8,0,3);
outtextxy(180,60,name);
settextstyle(0,0,1);
outtextxy(230,120,credit);
outtextxy(160,300,help);
while(!kbhit()){
}
}
while(1) {
sp+= dlsp;
adjspeed= orspeed*sign(adjspeed);
adjspeed *= sp;
if(kbhit()) {
char ch = getch();
if(ch == 'd') adjspeed = adjspeed > 0 ? adjspeed : -adjspeed;
else if(ch == 'a') adjspeed = adjspeed > 0 ? -adjspeed : adjspeed;
else if(ch == 'x') return 0;
}
cleardevice();
setcolor(WHITE);
if(points[0] + adjspeed < 0 || bx2 + adjspeed > X_MAX) adjspeed =-adjspeed;
for(int j = 0; j < 5; j++) {
points[j*2] += adjspeed;
}
espeed = abs(adjspeed)*4/7;
el1x += adjspeed;
el2x += adjspeed;
bx1 += adjspeed;
bx2 += adjspeed;
setfillstyle(SOLID_FILL,WHITE);
bar(0,0,X_MAX+1,0);
bar(0,0,0,GROUND+16);
bar(X_MAX-1,0,X_MAX+1,GROUND+16);
bar(0,GROUND+16,X_MAX+1,GROUND+16);
setfillstyle(SOLID_FILL,RED);
fillpoly(5,points);
setfillstyle(SOLID_FILL,BLUE);
fillellipse(el1x,368,8,8);
fillellipse(el2x,368,8,8);
setfillstyle(SOLID_FILL,GREEN);
ey += espeed;
if(ey <= 360 && ey >= 330) {
if(ex >= points[0] && ex <= points[8]) {
ey = -es;
ex = rand() % (X_MAX - 2*(bx2-bx1));
score++;
}
}
else if(ey > GROUND+2*es) {
lives--;
ey = -es;
ex = rand() % (X_MAX-3*(bx2-bx1)) + (bx2-bx1);
}
sprintf(&sc[7],"%d",score);
sprintf(&hs[12],"%d",hiscore);
sprintf(&lv[7],"%d",lives);
outtextxy(10,10,sc);
outtextxy(200,(GROUND+Y_MAX)/2,hs);
outtextxy(350,10,lv);
fillellipse(ex,ey,es,es);
setfillstyle(SOLID_FILL,BROWN);
bar(bx1,332,bx2,336);
if(lives == 0) break;
delay(del);
}
setcolor(YELLOW);
if(score > hiscore) {
outtextxy(250,130,"Congrats!");
hiscore = score;
}
setcolor(RED);
outtextxy(160,200,"Press x to exit or r to restart");
while(!kbhit()) {
char ch = getch();
if(ch == 'x') break;
if(ch == 'r') goto start;
}
closegraph();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.