-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode_Runner_main.cpp
More file actions
192 lines (168 loc) · 5.22 KB
/
Node_Runner_main.cpp
File metadata and controls
192 lines (168 loc) · 5.22 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
189
190
191
192
/*
* Project: Node Runner
* Environment: Dev-C++ (TDM-GCC 64-bit)
*/
#include <windows.h> // Required for Dev-C++
#include <GL/glut.h>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
// --- CONFIGURATION ---
#define CELL_SIZE 30
#define ROWS 15
#define COLS 15
#define ENEMY_SPEED 400
// --- GAME STATE ---
int gameState = 0; // 0=Playing, 1=Won, 2=Dead
// --- ENTITIES ---
struct Entity {
int x, y;
// CONSTRUCTOR ADDED TO FIX THE RED LINE ERROR
Entity(int _x, int _y) {
x = _x;
y = _y;
}
};
Entity player(1, 1);
Entity goal(13, 13);
// Enemies
vector<Entity> enemies;
// --- THE MAP ---
// 0=Path, 1=Wall, 2=Bomb
int map[ROWS][COLS] = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,0,2,0,0,0,1,0,0,1},
{1,0,1,0,1,0,1,1,1,0,0,1,0,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,2,0,1},
{1,0,1,1,1,1,0,1,1,1,0,1,1,0,1},
{1,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
{1,2,1,0,1,1,1,1,0,1,1,1,1,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,0,1,0,1,0,1,1,2,1},
{1,0,0,0,2,0,0,1,0,0,0,0,0,0,1},
{1,0,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
{1,1,1,0,1,0,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,2,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
void init() {
glClearColor(0.1, 0.1, 0.1, 1.0);
gluOrtho2D(0, COLS * CELL_SIZE, ROWS * CELL_SIZE, 0);
// FIXED SYNTAX FOR DEV-C++
enemies.push_back(Entity(13, 1));
enemies.push_back(Entity(1, 13));
enemies.push_back(Entity(7, 7));
}
// Unit 2: Drawing Primitives
void drawSquare(int x, int y, float r, float g, float b) {
glColor3f(r, g, b);
glBegin(GL_QUADS);
glVertex2i(x * CELL_SIZE, y * CELL_SIZE);
glVertex2i((x + 1) * CELL_SIZE, y * CELL_SIZE);
glVertex2i((x + 1) * CELL_SIZE, (y + 1) * CELL_SIZE);
glVertex2i(x * CELL_SIZE, (y + 1) * CELL_SIZE);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glLineWidth(1);
glBegin(GL_LINE_LOOP);
glVertex2i(x * CELL_SIZE, y * CELL_SIZE);
glVertex2i((x + 1) * CELL_SIZE, y * CELL_SIZE);
glVertex2i((x + 1) * CELL_SIZE, (y + 1) * CELL_SIZE);
glVertex2i(x * CELL_SIZE, (y + 1) * CELL_SIZE);
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
if (gameState == 0) {
for(int y=0; y<ROWS; y++) {
for(int x=0; x<COLS; x++) {
if(map[y][x] == 1) drawSquare(x, y, 0.5, 0.5, 0.5);
else if(map[y][x] == 2) drawSquare(x, y, 0.3, 0.0, 0.0);
else drawSquare(x, y, 0.9, 0.9, 0.9);
}
}
drawSquare(goal.x, goal.y, 0.0, 1.0, 0.0);
drawSquare(player.x, player.y, 0.0, 0.0, 1.0);
for(size_t i=0; i<enemies.size(); i++) {
drawSquare(enemies[i].x, enemies[i].y, 1.0, 0.0, 0.0);
}
}
else if (gameState == 1) {
glClearColor(0.0, 1.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
else if (gameState == 2) {
glClearColor(0.5, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
glutSwapBuffers();
}
bool isValid(int x, int y) {
if(x < 0 || x >= COLS || y < 0 || y >= ROWS) return false;
if(map[y][x] == 1) return false;
return true;
}
void moveEnemies(int value) {
if(gameState != 0) return;
for(size_t i=0; i<enemies.size(); i++) {
int dx = player.x - enemies[i].x;
int dy = player.y - enemies[i].y;
int nextX = enemies[i].x;
int nextY = enemies[i].y;
if(abs(dx) > abs(dy)) {
if(dx > 0) nextX++; else nextX--;
if(!isValid(nextX, nextY)) {
nextX = enemies[i].x;
if(dy != 0) { if(dy > 0) nextY++; else nextY--; }
}
} else {
if(dy > 0) nextY++; else nextY--;
if(!isValid(nextX, nextY)) {
nextY = enemies[i].y;
if(dx != 0) { if(dx > 0) nextX++; else nextX--; }
}
}
if(isValid(nextX, nextY)) {
enemies[i].x = nextX;
enemies[i].y = nextY;
}
if(enemies[i].x == player.x && enemies[i].y == player.y) {
gameState = 2;
}
}
glutPostRedisplay();
glutTimerFunc(ENEMY_SPEED, moveEnemies, 0);
}
void keyboard(unsigned char key, int x, int y) {
if (gameState != 0) return;
int nextX = player.x;
int nextY = player.y;
if(key == 'w' || key == 'W') nextY--;
if(key == 's' || key == 'S') nextY++;
if(key == 'a' || key == 'A') nextX--;
if(key == 'd' || key == 'D') nextX++;
if(isValid(nextX, nextY)) {
player.x = nextX;
player.y = nextY;
if(map[player.y][player.x] == 2) gameState = 2;
if(player.x == goal.x && player.y == goal.y) gameState = 1;
for(size_t i=0; i<enemies.size(); i++) {
if(enemies[i].x == player.x && enemies[i].y == player.y) gameState = 2;
}
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(COLS*CELL_SIZE, ROWS*CELL_SIZE);
glutCreateWindow("Node Runner");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutTimerFunc(ENEMY_SPEED, moveEnemies, 0);
glutMainLoop();
return 0;
}