-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
244 lines (239 loc) · 9.59 KB
/
Copy pathtest.cpp
File metadata and controls
244 lines (239 loc) · 9.59 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <cstdlib>
#include <ctime>
#include "raylib.h"
#include <iostream>
enum GameState {
STATE_MENU, // <--- New state for selecting difficulty
STATE_PLAYING,
STATE_GAMEOVER
};
// Start the game in the Menu state
GameState currentGameState = STATE_MENU;
class Ball {
public:
float x, y;
float radius;
float speedX, speedY;
int player_score = 0;
int CPU_score = 0;
Ball(float startX, float startY, float r) {
x = startX;
y = startY;
radius = r;
speedX = 12.0f; // Initial horizontal speed
speedY = 12.0f; // Initial vertical speed
}
void Update() {
x += speedX;
y += speedY;
// Check for collision with top and bottom walls
if (y - radius < 0 || y + radius > GetScreenHeight()) {
speedY *= -1; // Reverse vertical direction
}
// FIXED: Handled scoring and resetting cleanly without double-reversing
if (x + radius >= GetScreenWidth()) {
player_score++; // player scores when ball goes past right wall
ResetBall();
}
else if (x - radius <= 0) {
CPU_score++; // CPU scores when ball goes past left wall
ResetBall();
}
}
void Draw() {
// FIXED: Changed hardcoded 10 to use the actual radius variable
DrawCircle((int)x, (int)y, radius, RED);
}
void ResetBall(){
x=GetScreenWidth()/2;
y=GetScreenHeight()/2;
int speedOptions[2] = {1, -1};
speedX *= speedOptions[GetRandomValue(0, 1)];
// Randomly choose to start left or right
speedY *= speedOptions[GetRandomValue(0, 1)];
}
void Winner(){
if (player_score >= 10 || CPU_score >= 10) {
currentGameState = STATE_GAMEOVER; // Switch the state of the game
}
}
void FullReset(){
player_score = 0;
CPU_score = 0;
ResetBall(); // Centers the ball and randomizes direction
}
};
class Paddle {
public:
float x, y;
float width, height;
float speed; // Changed to float to match the CPU speed type
void Draw() {
DrawRectangle((int)x, (int)y, (int)width, (int)height, DARKBLUE);
}
void Movement() {
if (IsKeyDown(KEY_UP)) {
y -= speed;
}
if (IsKeyDown(KEY_DOWN)) {
y += speed;
}
}
void LimitMovement() {
if (y <= 0) {
y = 0;
}
if (y + height >= GetScreenHeight()) {
y = GetScreenHeight() - height;
}
}
};
// 2. This is the Derived Class (The Child)
// The ": public Paddle" means CpuPaddle automatically gets x, y, width, height, Draw(), and LimitMovement()!
class CpuPaddle : public Paddle {
public:
// We only need to write what makes the CPU unique: its AI behavior
void cpupdate(int ballY) {
if (ballY < y + height / 2) {
y -= speed;
}
if (ballY > y + height / 2) {
y += speed;
}
LimitMovement();
}
};
Paddle player;
CpuPaddle CPU;
int main() {
const int screenWidth = 1280;
const int screenHeight = 800;
InitWindow(screenWidth, screenHeight, "Welcome to Pong!");
SetTargetFPS(60);
// FIXED: Instantiate the ball properly inside main using its constructor
Ball ball(screenWidth / 2.0f, screenHeight / 2.0f, 10.0f);
Rectangle easyButton = { (float)screenWidth / 2 - 150, 300, 300, 60 };
Rectangle mediumButton = { (float)screenWidth / 2 - 150, 400, 300, 60 };
Rectangle hardButton = { (float)screenWidth / 2 - 150, 500, 300, 60 };
player.width = 25;
player.height = 120;
player.x= 10; // Position the paddle on the right side
player.y = screenHeight / 2 - player.height / 2; // Center the
player.speed=10;
CPU.width = 25;
CPU.height = 120;
CPU.x = screenWidth - CPU.width-10; // Position the CPU paddle on the left side
CPU.y = screenHeight / 2 - CPU.height / 2; // Center the CPU paddle
while (WindowShouldClose() == false){
Vector2 mousePos = GetMousePosition();
// ==========================================
// 1. STATE_MENU LOGIC (DIFFICULTY SELECTION)
// ==========================================
if (currentGameState == STATE_MENU) {
// --- Option A: Handle Keyboard Inputs ---
if (IsKeyPressed(KEY_E)) {
CPU.speed = 3;
currentGameState = STATE_PLAYING;
}
else if (IsKeyPressed(KEY_M)) {
CPU.speed = 5;
currentGameState = STATE_PLAYING;
}
else if (IsKeyPressed(KEY_H)) {
CPU.speed = 6;
currentGameState = STATE_PLAYING;
}// --- Option B: Handle Mouse Clicks ---
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
if (CheckCollisionPointRec(mousePos, easyButton)) {
CPU.speed = 3;
currentGameState = STATE_PLAYING;
}
else if (CheckCollisionPointRec(mousePos, mediumButton)) {
CPU.speed = 5;
currentGameState = STATE_PLAYING;
}
else if (CheckCollisionPointRec(mousePos, hardButton)) {
CPU.speed = 6;
currentGameState = STATE_PLAYING;
}
}
}
else if (currentGameState == STATE_PLAYING) {
// 1. Update Game State
ball.Update();
player.Movement();
player.LimitMovement();
CPU.cpupdate((int)ball.y);
CPU.LimitMovement();
ball.Winner();
}else if (currentGameState == STATE_GAMEOVER) {
// Game Over Menu Logic (Waiting for User Choice)
if (IsKeyPressed(KEY_R)) {
ball.FullReset(); // Reset scores and ball
currentGameState = STATE_PLAYING; // Go back to playing
}
if (IsKeyPressed(KEY_Q)) {
break; // Breaks out of the while loop to safely close the game
}
}
if(CheckCollisionCircleRec((Vector2){ball.x, ball.y}, ball.radius, (Rectangle){player.x, player.y, player.width, player.height})){
ball.speedX *= -1; // Reverse horizontal direction on collision with player's paddle
}
if(CheckCollisionCircleRec((Vector2){ball.x, ball.y}, ball.radius, (Rectangle){CPU.x, CPU.y, CPU.width, CPU.height})){
ball.speedX *= -1; // Reverse horizontal direction on collision with CPU's paddle
}
// 2. Drawing
BeginDrawing();
ClearBackground(RAYWHITE);
// The Ball
if (currentGameState == STATE_MENU) {
// Draw Header Text
DrawText("SELECT DIFFICULTY", screenWidth / 2 - 220, 150, 40, DARKGRAY);
DrawText("Click a box or press [E], [M], [H]", screenWidth / 2 - 200, 210, 20, GRAY);
// --- Draw Easy Button ---
// If mouse hovers over it, highlight it light gray, otherwise leave it blank/white
bool hoverEasy = CheckCollisionPointRec(mousePos, easyButton);
DrawRectangleRec(easyButton, hoverEasy ? LIGHTGRAY : RAYWHITE);
DrawRectangleLinesEx(easyButton, 3, GREEN); // Outline border
DrawText("EASY", easyButton.x + 110, easyButton.y + 15, 30, GREEN);
// --- Draw Medium Button ---
bool hoverMedium = CheckCollisionPointRec(mousePos, mediumButton);
DrawRectangleRec(mediumButton, hoverMedium ? LIGHTGRAY : RAYWHITE);
DrawRectangleLinesEx(mediumButton, 3, ORANGE);
DrawText("MEDIUM", mediumButton.x + 90, mediumButton.y + 15, 30, ORANGE);
// --- Draw Hard Button ---
bool hoverHard = CheckCollisionPointRec(mousePos, hardButton);
DrawRectangleRec(hardButton, hoverHard ? LIGHTGRAY : RAYWHITE);
DrawRectangleLinesEx(hardButton, 3, RED);
DrawText("HARD", hardButton.x + 110, hardButton.y + 15, 30, RED);
}else if (currentGameState == STATE_PLAYING) {
// Draw normal gameplay elements
ball.Draw();
player.Draw();
CPU.Draw();
DrawText(TextFormat("Player: %d", ball.player_score), screenWidth/4-20, 20, 80, BLACK);
DrawText(TextFormat("CPU: %d", ball.CPU_score), 3*screenWidth/4-20, 20, 80, BLACK);
}else if (currentGameState == STATE_GAMEOVER) {
// Draw Game Over Menu UI
if (ball.player_score >= 10) {
DrawText("PLAYER WINS!", screenWidth / 2 - 150, screenHeight / 2 - 100, 40, GREEN);
} else {
DrawText("CPU WINS!", screenWidth / 2 - 120, screenHeight / 2 - 100, 40, RED);
}
// Display Menu Instructions
DrawText("Press [R] to Try Again", screenWidth / 2 - 180, screenHeight / 2, 30, DARKGRAY);
DrawText("Press [Q] to Quit Game", screenWidth / 2 - 170, screenHeight / 2 + 50, 30, DARKGRAY);
}
// UI and Lines
DrawRectangle(0, 0, 10, 10, BLACK);
DrawRectangle(screenWidth - 10, 0, 10, 10, BLACK);
DrawRectangle(0, screenHeight - 10, 10, 10, BLACK);
DrawRectangle(screenWidth - 10, screenHeight - 10, 10, 10, BLACK);
DrawText("Centre of Field", 640, 400, 20, YELLOW);
DrawLine(0, screenHeight / 2, screenWidth, screenHeight / 2, GRAY);
DrawLine(screenWidth/2, 0, screenWidth/2, screenHeight, GRAY);
EndDrawing();
}
CloseWindow();
return 0;
}