Repository navigation
Expand file tree
/
Copy pathProject.cpp
More file actions
160 lines (118 loc) · 3.62 KB
/
Copy pathProject.cpp
File metadata and controls
160 lines (118 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include "MacUILib.h"
#include "objPos.h"
#include "Player.h"
using namespace std;
const int speeds[5] = {200000, 175000, 150000, 125000, 100000}; //contols how fast the game is
// speed levels in seconds: 0.2, 0.175, 0.15, 0.125, 0.1
Player *myPlayer; // global pointer meant to instantiate a player object
GameMechs *myGM;
//function prototypes
void Initialize(void);
void GetInput(void);
void RunLogic(void);
void DrawScreen(void);
void LoopDelay(void);
void CleanUp(void);
int main(void)
{
Initialize();
while(myGM->getExitFlagStatus()==false)
{
GetInput();
RunLogic();
DrawScreen();
LoopDelay();
}
CleanUp();
}
void Initialize(void)
{
MacUILib_init();
MacUILib_clearScreen();
myGM = new GameMechs();
myPlayer = new Player(myGM);
myGM->generateFood(myPlayer->getPlayerPos());
myGM->setFoodPos(-1,-1,'*');
srand(time(NULL)); //for random number generation
}
void GetInput(void) //gets input
{
char input = myGM->getInput();
myGM->setInput(input);
}
void RunLogic(void)
{
if (myGM->getFoodPos().pos->x == -1 && myGM->getFoodPos().pos->y == -1)
{
myGM->generateFood(myPlayer->getPlayerPos());
}
myPlayer->updatePlayerDir();
myPlayer->movePlayer();
}
void DrawScreen(void)
{
MacUILib_clearScreen();
//GAME STATS
MacUILib_printf("Press 'W A S D' to turn 'up, left, down, right' respectively.\n");
MacUILib_printf("(90* turns only, no 180* reversals.\n\n");
MacUILib_printf("Press 'Q' to decrease speed and 'E' to increase speed.\nCurrent Speed Level: %d\n\n", (myGM->getSpeed()+1));
MacUILib_printf("Press 'esc' to exit.\n\n");
MacUILib_printf("Score: %d\n\n\n", myGM->getScore());
//POSITION STATS
objPosArrayList* playerPos = myPlayer->getPlayerPos();
objPos foodPos = myGM->getFoodPos();
int boardX = myGM->getBoardSizeX(); //gets rows value
int boardY = myGM->getBoardSizeY(); //gets column value
for (int i = 0; i < boardY; i++) //columns
{
for (int j = 0; j < boardX; j++) //rows
{
if (j == 0 || j == boardX-1 || i == 0 || i == boardY-1) //draws border
{
MacUILib_printf("#");
}
else
{
bool isPrinted = false;
for (int k = 0; k < playerPos->getSize(); k++)
{
if (i == playerPos->getElement(k).pos->y && j == playerPos->getElement(k).pos->x) //draws player
{
MacUILib_printf("%c", playerPos->getElement(k).symbol);
isPrinted = true;
break;
}
}
if (!isPrinted && i == foodPos.pos->y && j == foodPos.pos->x) //draws food
{
MacUILib_printf("%c", foodPos.symbol);
}
// If neither snake nor food, print an empty space
if (!isPrinted && !(i == foodPos.pos->y && j == foodPos.pos->x))
{
MacUILib_printf(" ");
}
}
}
MacUILib_printf("\n");
}
}
void LoopDelay(void)
{
MacUILib_Delay(speeds[myGM->getSpeed()]); //delay depends on game speed level
}
void CleanUp(void)
{
if(myGM->getLoseFlagStatus())
{
MacUILib_printf("\n\nGame Over ! you lose.\n"); //losing message
}
else
{
MacUILib_printf("\n\nThanks for playing!!!\n"); //forced exit message
}
delete myPlayer;
delete myGM;
MacUILib_uninit();
}