-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
141 lines (119 loc) · 3.51 KB
/
main.cpp
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
#include<GL/glut.h>
#include<iostream>
#include"LoaderManager.h"
#include<chrono>
#include<string>
#include <cstring>
#include <iomanip>
#include <thread>
#define COLUMNS 40
#define ROWS 40
using namespace std;
// Global variables from LoaderManager.
extern int direction;
extern int score;
bool isGameOver = false;
std::chrono::high_resolution_clock::time_point start;
// Speed of snake (determine the FPS timer)
float speed = 10;
bool firstTime = true;
// Initialize background and the grid.
void init() {
glClearColor(0,0,0,1);
mapInit(COLUMNS, ROWS);
}
// Function to print Text on screen.
void output(int x, int y, const char *string)
{
int len, i;
glColor3f(1.0,1.0,0);
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, string[i]);
}
}
// Startup menu
void welcomeScreen() {
output(13, 20, "Welcome to 8bit snake :)");
output(15, 18, "Use arrows to move");
output(13, 17, "And X to start/restart game");
}
// Display function for drawing.
void display() {
glClear(GL_COLOR_BUFFER_BIT);
drawMap(); // Draw the map
drawSnakePlayer(); // Draw the playable snake
drawEggs(); // Draw eggs (to be eaten)
if (firstTime)
welcomeScreen();
float currSpeed = speed + (score/2.0);
std::stringstream stream;
stream << std::fixed << std::setprecision(1) << currSpeed;
output(0,39, ("score: " + to_string(score)).c_str());
output(0,0, ("speed: " + stream.str()).c_str());
// Player ate himself || hit borders
if (isGameOver) {
output(17,20, "Game over!");
output(10,19, ("Your score: " + to_string(score) + " your speed: " + stream.str()).c_str());
output(14,17, "Press X to restart");
cout << "Game over! your score is: " << score << " and your speed: " << (speed + (score/2.0)) << endl;
cout << "Press X to restart\n";
}
glutSwapBuffers();
}
// Create the Viewport
void reshape(int w, int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, COLUMNS, 0, ROWS, -1, 1);
}
// Timer to redraw shapes.
void timer(int) {
if (!isGameOver && !firstTime)
glutPostRedisplay();
glutTimerFunc(1000/(speed + (score/2.0)), timer, 0);
}
// Keyboard listener handler.
void keyboardSpec(const int key, int x, int y) {
// prevent snake to move over his body!
if ((key == LEFT && direction == RIGHT) || (key == UP && direction == DOWN) || (key == direction) ||
(key == RIGHT && direction == LEFT) || (key == DOWN && direction == UP)) return;
switch (key) {
case UP:
case DOWN:
case LEFT:
case RIGHT:
direction = key; // Assign the direction (Global variable).
break;
case 'x':
mapInit(ROWS, COLUMNS);
break;
}
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'x':
firstTime = false;
mapInit(ROWS, COLUMNS);
cout << "\nNew game started\n";
break;
}
}
int main(int argc, char** argv)
{
start = std::chrono::high_resolution_clock::now();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500, 500);
glutCreateWindow("8bit Snake");
init();
cout << "\nNew game started\n";
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
glutSpecialFunc(keyboardSpec);
glutKeyboardFunc(keyboard);
glutMainLoop();
}