-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
157 lines (131 loc) · 3.61 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <raylib.h>
#include <iostream>
Color Green = Color{38, 185, 154, 255};
Color Dark_Green = Color{20, 160, 133, 255};
Color Light_Green = Color{129, 204, 184, 255};
Color Yellow = Color{243, 213, 91, 255};
int player_score = 0;
int cpu_score = 0;
class Ball {
public:
float x, y;
int speed_x, speed_y;
int radius;
void Draw() {
DrawCircle(x, y, radius, Yellow);
}
void Update() {
x += speed_x;
y += speed_y;
if (y + radius >= GetScreenHeight() || y - radius <= 0) {
speed_y *= -1;
}
// Cpu wins
if (x + radius >= GetScreenWidth()) {
cpu_score++;
ResetBall();
}
if (x - radius <= 0) {
player_score++;
ResetBall();
}
}
void ResetBall() {
x = GetScreenWidth() / 2;
y = GetScreenHeight() / 2;
int speed_choices[2] = {-1, 1};
speed_x *= speed_choices[GetRandomValue(0, 1)];
speed_y *= speed_choices[GetRandomValue(0, 1)];
}
};
class Paddle {
protected:
void LimitMovement() {
if (y <= 0) {
y = 0;
}
if (y + height >= GetScreenHeight()) {
y = GetScreenHeight() - height;
}
}
public:
float x, y;
float width, height;
int speed;
void Draw() {
DrawRectangleRounded(Rectangle{x, y, width, height}, 0.8, 0, WHITE);
}
void Update() {
if (IsKeyDown(KEY_UP)) {
y = y - speed;
}
if (IsKeyDown(KEY_DOWN)) {
y = y + speed;
}
LimitMovement();
}
};
class CpuPaddle : public Paddle {
public:
void Update(int ball_y){
if (y + height / 2 > ball_y) {
y = y - speed;
}
if (y + height / 2 <= ball_y) {
y = y + speed;
}
LimitMovement();
}
};
Ball ball;
Paddle player;
CpuPaddle cpu;
int main() {
std::cout << "Starting the game" << std::endl;
const int screen_width = 1280;
const int screen_height = 800;
InitWindow(screen_width, screen_height, "My Pong Game!");
SetTargetFPS(60);
ball.radius = 20;
ball.x = screen_width / 2;
ball.y = screen_height / 2;
ball.speed_x = 7;
ball.speed_y = 7;
player.width = 25;
player.height = 120;
player.x = screen_width - player.width - 10;
player.y = screen_height / 2 - player.height / 2;
player.speed = 6;
cpu.height = 120;
cpu.width = 25;
cpu.x = 10;
cpu.y = screen_height / 2 - cpu.height / 2;
cpu.speed = 6;
while (WindowShouldClose() == false) {
BeginDrawing();
// Updating
ball.Update();
player.Update();
cpu.Update(ball.y);
// Checking for collisions
if (CheckCollisionCircleRec({ball.x, ball.y}, ball.radius, {player.x, player.y, player.width, player.height})) {
ball.speed_x *= -1;
}
if (CheckCollisionCircleRec({ball.x, ball.y}, ball.radius, {cpu.x, cpu.y, cpu.width, cpu.height})) {
ball.speed_x *= -1;
}
// Drawing
ClearBackground(Dark_Green);
DrawRectangle(screen_width / 2, 0, screen_width / 2, screen_height, Green);
DrawCircle(screen_width / 2, screen_height / 2, 150, Light_Green);
DrawLine(screen_width / 2, 0, screen_width / 2, screen_height, WHITE);
ball.Draw();
cpu.Draw();
player.Draw();
DrawText(TextFormat("%i", cpu_score), screen_width / 4 - 20, 20, 80, WHITE);
DrawText(TextFormat("%i", player_score), 3 * screen_width / 4 - 20, 20, 80, WHITE);
EndDrawing();
}
CloseWindow();
return 0;
}