-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
203 lines (159 loc) · 4.86 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
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
#include <iostream>
#include <raylib.h>
using namespace std;
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 bot_score = 0;
const char* winner = NULL;
char name[20];
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;
}
if(x + radius >= GetScreenWidth()) // bot will get some marks
{
bot_score++;
winner = "BOT wins";
ResetBall();
}
if(x - radius <= 0) // player will get some marks
{
player_score++;
winner = "Player wins";
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_DOWN)) {
y = y + speed;
}
if(IsKeyDown(KEY_UP)) {
y = y - speed;
}
LimitMovement();
}
};
class CPU_Paddle: public Paddle {
public:
void Update (int ball_y) {
if(y + height / 2 > ball_y) {
y = y - speed;
} else {
y = y + speed;
}
LimitMovement();
}
};
Ball ball;
Paddle player;
CPU_Paddle bot;
int main () {
cout << "Game started\n" << endl;
cout << "Enter your name: ";
scanf("%s", name);
for(int i = 0; name[i] != '\0'; i++) {
name[i] = toupper(name[i]);
}
const int screen_width = 1280;
const int screen_height = 800;
InitWindow(screen_width, screen_height, "Delu");
SetTargetFPS(60);
// Initializing Ball
ball.x = screen_width / 2.0;
ball.y = screen_height / 2.0;
ball.radius = 20;
ball.speed_x = 7;
ball.speed_y = 7;
// Initializing Player
player.width = 25;
player.height = 120;
player.x = screen_width - player.width - 10;
player.y = screen_height / 2 - player.height / 2;
player.speed = 6;
// Initializing Bot
bot.width = 25;
bot.height = 120;
bot.x = 10;
bot.y = screen_height / 2 - bot.height / 2;
bot.speed = 6;
while(WindowShouldClose() == false) {
BeginDrawing();
// Updating
if(!winner) {
ball.Update();
player.Update();
bot.Update(ball.y);
}
if(winner && IsKeyPressed(KEY_SPACE)) {
winner = NULL;
}
// Check if collision occurs
if(CheckCollisionCircleRec(Vector2 {ball.x, ball.y}, ball.radius, Rectangle {player.x, player.y, player.width, player.height})) {
ball.speed_x *= -1;
}
if(CheckCollisionCircleRec(Vector2 {ball.x, ball.y}, ball.radius, Rectangle {bot.x, bot.y, bot.width, bot.height})) {
ball.speed_x *= -1;
}
// Drwaring
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();
bot.Draw();
player.Draw();
DrawText("BOT", 40, 10, 80, WHITE);
DrawText(TextFormat("%i", bot_score), screen_width / 2 - 150, 20, 80, WHITE);
DrawText(name, screen_width / 2 + 40, 10, 80, WHITE);
DrawText(TextFormat("%i", player_score), screen_width - 150, 20, 80, WHITE);
if(winner) {
int winner_width = MeasureText(winner, 120);
char* StartAgain = "Press space key to start again or press Esc to exit...";
int StartAgain_width = MeasureText(StartAgain, 25);
DrawText(winner, screen_width / 2 - winner_width / 2, screen_height / 2 - 60, 120, YELLOW);
DrawText(StartAgain, screen_width / 2 - StartAgain_width / 2, screen_height / 2 + 120, 25, BLACK);
// winner = NULL;
}
EndDrawing();
}
CloseWindow();
return 0;
}