-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
232 lines (179 loc) · 4.92 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
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
#include "src/raylib.h"
struct Ball
{
float x,y;
float speedX , speedY;
float radius;
void Draw()
{
DrawCircle(x, y , radius, WHITE);
}
}ball;
struct Paddle
{
float X, Y;
float width, height;
float speed;
Rectangle Colli()
{
return Rectangle{X - width/2, Y , 10, 100};
// return Rectangle{X - width/2, Y , 10, 100};
}
void Draw()
{
DrawRectangle(X - width/2, Y , 10, 100, WHITE);
}
}leftPaddle, rightPaddle;
int main()
{
InitAudioDevice();
const char* winner = nullptr;
struct Ball ball ;
//ball
ball.x = 400.0f;
ball.y = 200.0f;
ball.speedX = 150;
ball.speedY = 150;
ball.radius = 5;
//leftpaddle
struct Paddle leftPaddle;
leftPaddle.X = 50;
leftPaddle.Y = 200 ;
leftPaddle.width = 10;
leftPaddle.height = 100;
leftPaddle.speed = 300;
//rightPaddle
struct Paddle rightPaddle;
rightPaddle.X = 750;
rightPaddle.Y = 200 ;
rightPaddle.width = 10;
rightPaddle.height = 100;
rightPaddle.speed = 300;
//balltexture
Image Img = LoadImage("resources/sprites.png");
//sound loading
Sound fxhit = LoadSound("resources/hit.wav");
Sound fxselect = LoadSound("resources/select.wav");
//window Intialized
InitWindow(800, 500, "PONG-recreated in raylib by mithil");
SetWindowState(FLAG_VSYNC_HINT);
while(!WindowShouldClose())
{
ball.x += ball.speedX * GetFrameTime();
ball.y += ball.speedY * GetFrameTime();
//ball window collision
if(ball.y > GetScreenHeight())
{
ball.y = GetScreenHeight();
ball.speedY *= -1;
PlaySound(fxhit);
}
if(ball.y < 0)
{
ball.y = 0;
ball.speedY *= -1;
PlaySound(fxhit);
}
//paddle collision
//leftpaddle
if(leftPaddle.Y > 400)
{
leftPaddle.Y = 400;
}
if(leftPaddle.Y < 0)
{
leftPaddle.Y = 0;
}
//right paddle
if(rightPaddle.Y > 400)
{
rightPaddle.Y = 400;
}
if (rightPaddle.Y < 0)
{
rightPaddle.Y = 0;
}
//input left paddle
if(IsKeyDown(KEY_W))
{
leftPaddle.Y -= leftPaddle.speed * GetFrameTime();
}
else if(IsKeyDown(KEY_S))
{
leftPaddle.Y += leftPaddle.speed * GetFrameTime();
}
//INPUT RIGHT PADDLE
if (IsKeyDown(KEY_UP))
{
rightPaddle.Y -= rightPaddle.speed * GetFrameTime();
}
else if(IsKeyDown(KEY_DOWN))
{
rightPaddle.Y += rightPaddle.speed * GetFrameTime();
}
//collision for pads
if (CheckCollisionCircleRec(Vector2{(float)ball.x, (float)ball.y}, ball.radius , leftPaddle.Colli()))
{
if(ball.speedX < 0)
{
ball.speedX *= -1.1f;
PlaySound(fxhit);
}
}
if(CheckCollisionCircleRec(Vector2{(float) ball.x, (float)ball.y}, ball.radius , rightPaddle.Colli()))
{
if(ball.speedX > 0)
{
ball.speedX *= -1.1f;
PlaySound(fxhit);
}
}
//collision for paddle
//winning criteria
if( ball.x < 0)
{
winner = "right wins!";
ball.x = -10.0f;
ball.y = 000.0f;
ball.speedX = 0;
ball.speedY = 0;
}
else if ( ball.x > GetScreenWidth()
)
{
winner = "left wins!";
ball.x = -10.0f;
ball.y = 000.0f;
ball.speedX = 0;
ball.speedY = 0;
}
if(winner && IsKeyDown(KEY_SPACE))
{
winner = nullptr;
ball.x = 400.0f;
ball.y = 200.0f;
ball.speedX = 150;
ball.speedY = 150;
PlaySound(fxselect);
}
BeginDrawing();
//ImageClearBackground("resources/bg/jpg", WHITE);
ClearBackground(DARKGRAY);
//rendering
ball.Draw();
leftPaddle.Draw();
rightPaddle.Draw();
// DrawTexture(Img, 500, 200, WHITE);
if(winner)
{
DrawText("use SPACE bar to replay",GetScreenWidth() -650,GetScreenHeight()/4 -50, 40, YELLOW);
DrawText(winner, GetScreenWidth() -600, GetScreenHeight()/2 -30 , 90, RED);
}
DrawFPS(10, 10);
EndDrawing();
}
UnloadSound(fxselect);
UnloadSound(fxhit);
CloseWindow();
return 0;
}