-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
192 lines (163 loc) · 3.99 KB
/
main.cpp
File metadata and controls
192 lines (163 loc) · 3.99 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
#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 cpu_score = 0;
class Ball
{
public:
float x,y;
int speedx,speedy;
int radius;
void draw()
{
DrawCircle(x,y,radius,Yellow);
}
void update()
{
x += speedx;
y += speedy;
if(y+radius >= GetScreenHeight() || y-radius<=0)
{
speedy = speedy * -1;
}
if(x+radius >= GetScreenWidth())
{
player_score++;
resetball();
}
if(x-radius <= 0)
{
cpu_score++;
resetball();
}
}
void resetball()
{
x = GetScreenWidth()/2;
y = GetScreenHeight()/2;
int speed_choices[2] = {-1,1};
speedx = speedx*speed_choices[GetRandomValue(0,1)];
speedy = speedy*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,BLACK);
}
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 bally)
{
if(y+height/2 > bally)
{
y = y-speed;
}
if(y+height/2 < bally)
{
y = y + speed;
}
limitmovement();
}
};
int main()
{
Ball ball;
Paddle player;
cpupaddle cpu;
cout<<"starting the game"<<endl;
//creating a window
const int screen_width = 1280;
const int screen_height = 800;
InitWindow(screen_width,screen_height,"My Pong Game");
//setting fps limit
SetTargetFPS(60);
//setting ball attributes
ball.radius = 20;
ball.x=screen_width/2;
ball.y=screen_height/2;
ball.speedx=7;
ball.speedy=7;
//setting player attributes (paddle on the left side of the screen)
player.width = 25;
player.height = 120;
player.speed = 6;
player.x = 10;
player.y = screen_height/2 - player.height/2;
//setting cpu paddle attributes(paddle on the right side of the screen)
cpu.width = 25;
cpu.height = 120;
cpu.speed = 6;
cpu.x = screen_width - player.width - 10;
cpu.y = screen_height/2 - player.height/2;
//Game loop
while(WindowShouldClose()==false)
{
BeginDrawing();
//updating ball speed
ball.update();
player.update();
cpu.update(ball.y);
ClearBackground(Dark_Green);
DrawRectangle(0,0,screen_width/2,screen_height,Green);
DrawCircle(screen_width/2,screen_height/2,150,Light_Green);
//checking for collisions
if(CheckCollisionCircleRec(Vector2{ball.x,ball.y},ball.radius,Rectangle{player.x,player.y,player.width,player.height}))
{
ball.speedx = ball.speedx*-1;
}
if(CheckCollisionCircleRec(Vector2{ball.x,ball.y},ball.radius,Rectangle{cpu.x,cpu.y,cpu.width,cpu.height}))
{
ball.speedx = ball.speedx*-1;
}
//drawing the paddles
cpu.draw();
ball.draw();
player.draw();
//text for score
DrawText(TextFormat("%i",player_score),screen_width/4-20,20,80,WHITE);
DrawText(TextFormat("%i",cpu_score),3*screen_width/4-20,20,80,WHITE);
//drawing the mid line
DrawLine(screen_width/2,0,screen_width/2,screen_height,WHITE);
EndDrawing();
}
//closing the window
CloseWindow();
return 0;
}