0% found this document useful (0 votes)
7 views4 pages

Untitled 1

This document contains a C++ implementation of a simple console-based game called 'Go Dino', where the player controls a dinosaur that must jump over hurdles. The game features scoring, jumping mechanics, and a game-over condition when the dinosaur collides with a hurdle. The main function provides a menu for starting the game, viewing instructions, or quitting.

Uploaded by

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

Untitled 1

This document contains a C++ implementation of a simple console-based game called 'Go Dino', where the player controls a dinosaur that must jump over hurdles. The game features scoring, jumping mechanics, and a game-over condition when the dinosaur collides with a hurdle. The main function provides a menu for starting the game, viewing instructions, or quitting.

Uploaded by

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

#include <iostream>

#include <conio.h>
#include <windows.h>
#include <time.h>
#define DINO_POS 2
#define HURDLE_POS 74

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);

int dinoY = 0;
int speed = 40;

int gameover = 0;

void gotoxy(int x, int y) {


COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(console, coord);
}

void setcursor(bool visible, DWORD size) {


if (size == 0) size = 20;
CONSOLE_CURSOR_INFO cursorInfo;
cursorInfo.bVisible = visible;
cursorInfo.dwSize = size;
SetConsoleCursorInfo(console, &cursorInfo);
}

void init() {
system("cls");
gameover = 0;
gotoxy(3, 2);
cout << "SCORE: ";
for (int i = 0; i < 79; i++) {
gotoxy(1 + i, 1);
cout << "=";
gotoxy(1 + i, 25);
cout << "=";
}
}

void moveDino(int jump = 0) {


static int foot = 0;

if (jump == 0) dinoY = 0;
else if (jump == 2) dinoY--;
else dinoY++;
for (int i = 0; i < 10; i++) {
gotoxy(DINO_POS, 15 - dinoY + i);
cout << " ";
}

gotoxy(DINO_POS, 15 - dinoY);
cout << " ÜÛÛÛÛÜ";
gotoxy(DINO_POS, 16 - dinoY);
cout << " ÛÛÛÛÛÛ";
gotoxy(DINO_POS, 17 - dinoY);
cout << " ÛÛÛÛßßß";
gotoxy(DINO_POS, 18 - dinoY);
cout << " ÜÛÛÛÛÛÛÜÜ";
gotoxy(DINO_POS, 19 - dinoY);
if (jump == 1 || jump == 2) {
cout << " ÛÛÛÛÛß ßÛ";
gotoxy(DINO_POS, 20 - dinoY);
cout << " ÛÛ ";
} else if (foot == 0) {
cout << " ßÛÜß ßÛ";
gotoxy(DINO_POS, 20 - dinoY);
cout << " ÛÛ ";
foot = !foot;
} else {
cout << " ßÛÜ ßÜ ";
gotoxy(DINO_POS, 20 - dinoY);
cout << " ÛÛ ";
foot = !foot;
}

gotoxy(DINO_POS, 25 - dinoY);
if (jump == 0) {
cout << "============";
} else {
cout << " ";
}
Sleep(speed);
}

void drawHurdle() {
static int hurdleX = 0;
static int score = 0;

if (hurdleX == 56 && dinoY < 4) {

score = 0;
speed = 40;
gotoxy(36, 8);
cout << "GAME OVER!";
getch();
gameover = 1;
return;
}

for (int i = 20; i <= 24; i++) {


gotoxy(HURDLE_POS - hurdleX, i);
cout << " ";
}

gotoxy(HURDLE_POS - hurdleX, 20);


cout << "| | ";
gotoxy(HURDLE_POS - hurdleX, 21);
cout << "| | ";
gotoxy(HURDLE_POS - hurdleX, 22);
cout << "|_| ";
gotoxy(HURDLE_POS - hurdleX, 23);
cout << " | ";
gotoxy(HURDLE_POS - hurdleX, 24);
cout << " | ";

hurdleX++;
if (hurdleX == 73) {
hurdleX = 0;
score++;
gotoxy(11, 2);
cout << score;
if (speed > 20) speed--;
}
}

void play() {
system("cls");
char ch;
int i;
init();
while (true) {
while (!kbhit()) {
if (gameover == 1) return;
moveDino();
drawHurdle();
}
ch = getch();
if (ch == 32) {
i = 0;
while (i < 12) {
moveDino(1);
drawHurdle();
i++;
}
while (i > 0) {
moveDino(2);
drawHurdle();
i--;
}
} else if (ch == 'p' || ch == 'P') {
getch();
} else if (ch == 27) {
break;
}
}
}

void instruction() {
system("cls");
cout << "Instructions\n";
cout << "--------------------\n";
cout << "1. Avoid hurdles by jumping.\n";
cout << "2. Press Spacebar to jump.\n";
cout << "3. Press P to pause the game.\n";
cout << "4. Press ESC to quit the game.\n";
cout << "Press any key to return to the menu.";
getch();
}

int main() {
setcursor(0, 0);
do {
system("cls");
gotoxy(10, 5);
cout << "-----------------------------";
gotoxy(10, 6);
cout << "|--------- Go Dino ---------|";
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 = getch();
if (op == '1') {
play();
} else if (op == '2') {
instruction();
} else if (op == '3') {
exit(0);
} else {
gotoxy(10, 15);
cout << "Invalid option! Try again.";
Sleep(1000);
}
} while (1);
return 0;
}

You might also like