-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.cpp
More file actions
190 lines (164 loc) · 5.18 KB
/
Copy pathmenu.cpp
File metadata and controls
190 lines (164 loc) · 5.18 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
#include <GL/gl.h>
#include <GL/glut.h>
#include <string.h>
#include <stdio.h>
#include "menu.h"
#include "game.h"
// Menu state
int currentMenu = MENU_MAIN;
int selectedDifficulty = DIFFICULTY_MEDIUM;
extern int FPS;
extern bool gameStarted;
// Button dimensions
struct Button {
float x1, y1, x2, y2;
char text[50];
};
// Menu buttons
Button playButton = {15.0, 25.0, 25.0, 28.0, "PLAY"};
Button helpButton = {15.0, 20.0, 25.0, 23.0, "HELP"};
Button easyButton = {15.0, 25.0, 25.0, 28.0, "EASY"};
Button mediumButton = {15.0, 20.0, 25.0, 23.0, "MEDIUM"};
Button hardButton = {15.0, 15.0, 25.0, 18.0, "HARD"};
Button backButton = {15.0, 5.0, 25.0, 8.0, "BACK"};
// Function to draw text
void drawText(float x, float y, const char* text) {
glRasterPos2f(x, y);
for (int i = 0; text[i] != '\0'; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, text[i]);
}
}
// Function to draw a button
void drawButton(Button btn, bool selected) {
if (selected) {
glColor3f(0.0, 1.0, 0.0); // Green for selected
} else {
glColor3f(0.5, 0.5, 0.5); // Gray for unselected
}
// Draw button background
glBegin(GL_QUADS);
glVertex2f(btn.x1, btn.y1);
glVertex2f(btn.x2, btn.y1);
glVertex2f(btn.x2, btn.y2);
glVertex2f(btn.x1, btn.y2);
glEnd();
// Draw button border
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(btn.x1, btn.y1);
glVertex2f(btn.x2, btn.y1);
glVertex2f(btn.x2, btn.y2);
glVertex2f(btn.x1, btn.y2);
glEnd();
// Draw button text
glColor3f(1.0, 1.0, 1.0);
float textX = (btn.x1 + btn.x2) / 2.0 - strlen(btn.text) * 0.15;
float textY = (btn.y1 + btn.y2) / 2.0 - 0.5;
drawText(textX, textY, btn.text);
}
// Initialize the menu
void initMenu() {
currentMenu = MENU_MAIN;
gameStarted = false;
}
// Draw the main menu
void drawMainMenu() {
// Draw title
glColor3f(1.0, 1.0, 0.0);
drawText(16.0, 32.0, "SNAKE GAME");
// Draw buttons
drawButton(playButton, false);
drawButton(helpButton, false);
}
// Draw the play menu with difficulty options
void drawPlayMenu() {
// Draw title
glColor3f(1.0, 1.0, 0.0);
drawText(12.0, 32.0, "SELECT DIFFICULTY");
// Draw buttons
bool easySelected = (selectedDifficulty == DIFFICULTY_EASY);
bool mediumSelected = (selectedDifficulty == DIFFICULTY_MEDIUM);
bool hardSelected = (selectedDifficulty == DIFFICULTY_HARD);
drawButton(easyButton, easySelected);
drawButton(mediumButton, mediumSelected);
drawButton(hardButton, hardSelected);
drawButton(backButton, false);
}
// Draw the help menu
void drawHelpMenu() {
// Draw title
glColor3f(1.0, 1.0, 0.0);
drawText(17.0, 32.0, "HELP");
// Draw help text
glColor3f(1.0, 1.0, 1.0);
drawText(5.0, 25.0, "Use arrow keys to control the snake");
drawText(5.0, 22.0, "Eat food to grow and increase score");
drawText(5.0, 19.0, "Don't hit the walls or yourself!");
drawText(5.0, 16.0, "Easy: Slow snake speed - for beginners");
drawText(5.0, 14.0, "Medium: Normal snake speed");
drawText(5.0, 12.0, "Hard: Fast snake speed - for experts");
// Draw back button
drawButton(backButton, false);
}
// Draw the current menu
void drawMenu() {
switch(currentMenu) {
case MENU_MAIN:
drawMainMenu();
break;
case MENU_PLAY:
drawPlayMenu();
break;
case MENU_HELP:
drawHelpMenu();
break;
}
}
// Check if a point is inside a button
bool isInsideButton(int x, int y, Button btn) {
// Convert window coordinates to game coordinates
float gameX = x * 40.0 / glutGet(GLUT_WINDOW_WIDTH);
float gameY = 40.0 - (y * 40.0 / glutGet(GLUT_WINDOW_HEIGHT));
return (gameX >= btn.x1 && gameX <= btn.x2 &&
gameY >= btn.y1 && gameY <= btn.y2);
}
// Start the game with the selected difficulty
void startGame(int difficulty) {
FPS = difficulty;
gameStarted = true;
selectedDifficulty = difficulty;
}
// Return to the main menu
void returnToMainMenu() {
currentMenu = MENU_MAIN;
}
// Mouse callback function
void mouseCallback(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
switch(currentMenu) {
case MENU_MAIN:
if (isInsideButton(x, y, playButton)) {
currentMenu = MENU_PLAY;
} else if (isInsideButton(x, y, helpButton)) {
currentMenu = MENU_HELP;
}
break;
case MENU_PLAY:
if (isInsideButton(x, y, easyButton)) {
startGame(DIFFICULTY_EASY);
} else if (isInsideButton(x, y, mediumButton)) {
startGame(DIFFICULTY_MEDIUM);
} else if (isInsideButton(x, y, hardButton)) {
startGame(DIFFICULTY_HARD);
} else if (isInsideButton(x, y, backButton)) {
returnToMainMenu();
}
break;
case MENU_HELP:
if (isInsideButton(x, y, backButton)) {
returnToMainMenu();
}
break;
}
}
}