Programming Project: C++ Code For A Basic Game Name:Sarvesh
Programming Project: C++ Code For A Basic Game Name:Sarvesh
PROJECT
NAME:SARVESH
CLASS:XI B
CODE FOR A BASIC GAME
#include <iostream>
#include <conio.h>
void run();
void printMap();
void initMap();
void move(int dx, int dy);
void update();
void changeDirection(char key);
void clearScreen();
void generateFood();
// Map dimensions
const int mapwidth = 20;
const int mapheight = 20;
const int size = mapwidth * mapheight;
// Amount of food the snake has (How long the body is)
int food = 3;
int main()
{
run();
return 0;
}
1
4+2
3
*/
switch (key) {
case 'w':
if (direction != 2) direction = 0;
break;
case 'd':
if (direction != 3) direction = 1;
break;
case 's':
if (direction != 4) direction = 2;
break;
case 'a':
if (direction != 5) direction = 3;
break;
}
}
// Clears screen
void clearScreen() {
// Clear the screen
system("cls");
}
// Initializes map
void initMap()
{
// Places the initual head location in middle of map
headxpos = mapwidth / 2;
headypos = mapheight / 2;
map[headxpos + headypos * mapwidth] = 1;