-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.cpp
More file actions
188 lines (143 loc) · 4.24 KB
/
Project.cpp
File metadata and controls
188 lines (143 loc) · 4.24 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include "MacUILib.h"
#include "objPos.h"
#include "GameMechs.h"
#include "Player.h"
#include "Food.h"
using namespace std;
#define DELAY_CONST 100000
#define BOARD_X 40
#define BOARD_Y 20
bool exitFlag;
void Initialize(void);
void GetInput(void);
void RunLogic(void);
void DrawScreen(void);
void LoopDelay(void);
void CleanUp(void);
GameMechs* gameMechanics;
Player* player;
Food* food;
int main(void)
{
Initialize();
while(!exitFlag && !gameMechanics->getLoseFlagStatus())
{
GetInput();
RunLogic();
DrawScreen();
LoopDelay();
}
CleanUp();
}
void Initialize(void)
{
MacUILib_init();
MacUILib_clearScreen();
exitFlag = false;
gameMechanics = new GameMechs(BOARD_X, BOARD_Y); // Board size
food = new Food();
player = new Player(gameMechanics, food);
// Generate initial food on the board
food->generateFood(player->getPlayerPosList());
}
void GetInput(void)
{
if (MacUILib_hasChar())
{
gameMechanics->setInput(MacUILib_getChar());
}
else
{
gameMechanics->clearInput(); // Clear input when no key is pressed
}
}
void RunLogic(void)
{
player->updatePlayerDir();
// Move the player (snake) and handle game logic
player->movePlayer();
// Update game state (e.g., speed, delay, etc.)
player->updatePlayerSpeed();
player->updatePlayerDelay();
// Check if the game has ended
exitFlag = gameMechanics->getExitFlagStatus();
}
void DrawScreen(void)
{
MacUILib_clearScreen(); // Clear the screen for redrawing
for (int y = 0; y < BOARD_Y; y++)
{
for (int x = 0; x < BOARD_X; x++)
{
if (y == 0 || y == BOARD_Y - 1 || x == 0 || x == BOARD_X - 1)
{
MacUILib_printf("#"); // Draw border for the gameboard
}
else
{
bool isRendered = false;
// Check if the position contains food
objPosArrayList* foodList = food->getFood();
for (int i = 0; i < foodList->getSize(); i++)
{
objPos foodPos = foodList->getElement(i);
if (foodPos.pos->x == x && foodPos.pos->y == y)
{
MacUILib_printf("%c", foodPos.getSymbol()); // Spawn food
isRendered = true;
break;
}
}
// If no food, check if the position contains part of the snake
if (!isRendered)
{
objPosArrayList* playerList = player->getPlayerPosList();
for (int i = 0; i < playerList->getSize(); i++)
{
objPos snakePart = playerList->getElement(i);
if (snakePart.pos->x == x && snakePart.pos->y == y)
{
MacUILib_printf("*"); // Render snake segment
isRendered = true;
break;
}
}
}
// If neither food nor snake print empty space
if (!isRendered)
{
MacUILib_printf(" ");
}
}
}
MacUILib_printf("\n");
}
// Display game info
MacUILib_printf("Score: %d\n", gameMechanics->getScore());
MacUILib_printf("Apple, Banana, Grape, & Super Fruit(20%% chance)\n");
MacUILib_printf("Press 'ESC' to exit.\n");
}
void LoopDelay(void)
{
MacUILib_Delay(DELAY_CONST); // 0.1s delay
}
void CleanUp(void)
{
MacUILib_clearScreen();
if (gameMechanics->getLoseFlagStatus()) // game over screen if you lose or quit on purpose
{
MacUILib_printf("Game Over! You lost. Your final score is: %d", gameMechanics->getScore());
}
else
{
MacUILib_printf("Congratulations! You ended the game. Your final score is: %d",gameMechanics->getScore());
}
delete player; // frees memory
player = nullptr;
delete food; // frees memory
food = nullptr;
delete gameMechanics; // frees memory
gameMechanics = nullptr;
MacUILib_uninit();
}