-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
177 lines (137 loc) · 4.4 KB
/
main.cpp
File metadata and controls
177 lines (137 loc) · 4.4 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
#include "raylib.h"
#include <vector>
#include <cstdlib>
#include <time.h>
#include <charconv>
#include <iostream> // for logging
typedef std::pair<int,int> pair;
enum DIRECTION {
IDLE,
UP,
DOWN,
RIGHT,
LEFT,
};
const int width = 20, height = 15;
const int screenWidth = 800, screenHeight = 600;
const int tileWidth = screenWidth/width; //screenWidth / width;
const int offsetX = 0, offsetY = 0;
int FPS = 60, UPS = 3, FPU = FPS/UPS; // Frames Per Second, Updates Per Second, Frames Per Update
bool drawOutlines = false;
bool allowOverlap = false;
int fruitAmount = 2;
bool gameOver = false;
std::vector<pair> player = {};
DIRECTION dir = IDLE;
DIRECTION lastDir = IDLE;
std::vector<pair> fruit = {};
void generate_fruit() {
while(fruit.size()<fruitAmount) {
bool generate; pair temp;
do {
generate = false;
// generate new position
temp = {rand() % width, rand() % height};
// check if square is taken
for(pair i:fruit) if(i==temp) generate = true;
for(pair i:player) if(i==temp) generate = true;
} while (generate);
fruit.push_back(temp);
}
}
int frameCounter = 0;
void Update() {
frameCounter++;
DIRECTION tmp = dir; lastDir = dir;
if(IsKeyPressed(KEY_UP)&&tmp!=DOWN) dir = UP;
if(IsKeyPressed(KEY_DOWN)&&tmp!=UP) dir = DOWN;
if(IsKeyPressed(KEY_RIGHT)&&tmp!=LEFT) dir = RIGHT;
if(IsKeyPressed(KEY_LEFT)&&tmp!=RIGHT) dir = LEFT;
if(frameCounter != FPU) return;
frameCounter = 0;
if(gameOver)return;
if(dir==IDLE) return;
// MOVE
pair front = player.at(player.size()-1);
switch(dir) {
case UP:
front.second--;
break;
case DOWN:
front.second++;
break;
case LEFT:
front.first--;
break;
case RIGHT:
front.first++;
break;
}
// DEATH CHECK
if(!allowOverlap) {
for(int i=0;i<player.size()-1;i++) { // playersize-1 cuz the end of the vector is the front
if(player[i]==front) {
gameOver = true;
return;
}
}
}
if(front.first<0 || front.first+1>width || front.second<0 || front.second+1>height) {
gameOver = true;
return;
}
player.push_back(front);
// FRUIT
bool ate = false;
for(int i=0;i<fruit.size();i++) {
if(front == fruit[i]) {
ate = true;
fruit.erase(fruit.begin()+i);
generate_fruit();
// increase speed
if(player.size()%(2*fruitAmount)==0&&UPS<15) {
FPU = FPS/++UPS;
std::cout<<UPS<<std::endl;
}
}
}
if(!ate) player.erase(player.begin()); // pop back of snake
}
void Draw() {
BeginDrawing();
// BACKGROUND
ClearBackground(BLACK);
// DRAW OUTLINES
if(drawOutlines) for(int x=0;x<width;x++) for(int y=0;y<height;y++) DrawRectangleLines(offsetX+x*tileWidth,offsetY+y*tileWidth,tileWidth,tileWidth,GRAY);
// DRAW OBJECTS
for(pair i:player) DrawRectangle(offsetX+i.first*tileWidth,offsetY+i.second*tileWidth,tileWidth,tileWidth,GREEN);
for(pair i:fruit) DrawRectangle(offsetX+i.first*tileWidth,offsetY+i.second*tileWidth,tileWidth,tileWidth,RED);
// TEXT
if(gameOver) {
DrawText("GAME OVER", screenWidth/2-MeasureText("GAME OVER",screenHeight/6)/2, screenHeight/2-screenHeight/6/2-screenHeight/10, screenHeight/6, WHITE);
DrawText("SCORE: ",screenWidth/2-MeasureText("SCORE: ",screenHeight/10)/2, screenHeight/2-screenHeight/10/2+screenHeight/10, screenHeight/10, WHITE);
DrawText(std::to_string(player.size()).c_str(),screenWidth/2+MeasureText("SCORE: ",screenHeight/10)/6, screenHeight/2-screenHeight/10/2+screenHeight/10, screenHeight/10, WHITE);
} else {
DrawText("SCORE:", 40, 20, 40, WHITE);
DrawText(std::to_string(player.size()).c_str(), 200, 20, 40, WHITE);
}
EndDrawing();
}
void Start() {
// Set random seed
srand(time(NULL));
// starting posistion
player.push_back({width/2,height/2});
generate_fruit();
}
int main() {
// Window Setup
InitWindow(screenWidth, screenHeight, "Snake");
SetTargetFPS(60);
Start();
while(!WindowShouldClose()) {
Draw();
Update();
}
CloseWindow();
}