-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
115 lines (92 loc) · 3.77 KB
/
main.c
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
#include "raylib.h"
int main(void)
{
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "Classic Pong Clone");
SetTargetFPS(144);
Vector2 ballPosition = { screenWidth / 2.0f, screenHeight / 2.0f };
Vector2 ballSpeed = { 4.0f, 4.0f };
const int ballSize = 10; // This is the radius of the ball
const int paddleWidth = 10;
const int paddleHeight = 100;
Vector2 player1Position = { 50, screenHeight / 2.0f - paddleHeight / 2.0f };
Vector2 player2Position = { screenWidth - 50 - paddleWidth, screenHeight / 2.0f - paddleHeight / 2.0f };
const float paddleSpeed = 5.0f;
int player1Score = 0;
int player2Score = 0;
const int maxScore = 5;
while (!WindowShouldClose())
{
// Player 1 controls (W/S keys)
if (IsKeyDown(KEY_W)) player1Position.y -= paddleSpeed;
if (IsKeyDown(KEY_S)) player1Position.y += paddleSpeed;
// Player 2 controls (Up/Down arrow keys)
if (IsKeyDown(KEY_UP)) player2Position.y -= paddleSpeed;
if (IsKeyDown(KEY_DOWN)) player2Position.y += paddleSpeed;
// Keep paddles within screen bounds
if (player1Position.y < 0) player1Position.y = 0;
if (player1Position.y + paddleHeight > screenHeight)
player1Position.y = screenHeight - paddleHeight;
if (player2Position.y < 0) player2Position.y = 0;
if (player2Position.y + paddleHeight > screenHeight)
player2Position.y = screenHeight - paddleHeight;
// Ball movement
ballPosition.x += ballSpeed.x;
ballPosition.y += ballSpeed.y;
// Ball collision with top and bottom walls
if (ballPosition.y <= 0 || ballPosition.y + ballSize >= screenHeight)
{
ballSpeed.y *= -1.0f;
}
// Ball collision with paddles
if (CheckCollisionCircleRec(
ballPosition,
ballSize,
(Rectangle){ player1Position.x, player1Position.y, paddleWidth, paddleHeight }))
{
ballSpeed.x *= -1.0f;
ballPosition.x = player1Position.x + paddleWidth + ballSize;
}
if (CheckCollisionCircleRec(
ballPosition,
ballSize,
(Rectangle){ player2Position.x, player2Position.y, paddleWidth, paddleHeight }))
{
ballSpeed.x *= -1.0f;
ballPosition.x = player2Position.x - ballSize;
}
// Scoring system
if (ballPosition.x < 0) // Player 2 scores
{
player2Score++;
ballPosition = (Vector2){ screenWidth / 2.0f, screenHeight / 2.0f };
ballSpeed = (Vector2){ 4.0f, 4.0f };
}
if (ballPosition.x > screenWidth) // Player 1 scores
{
player1Score++;
ballPosition = (Vector2){ screenWidth / 2.0f, screenHeight / 2.0f };
ballSpeed = (Vector2){ -4.0f, -4.0f };
}
// End game if a player reaches the max score
if (player1Score == maxScore || player2Score == maxScore)
{
BeginDrawing();
ClearBackground(BLACK);
DrawText("GAME OVER", screenWidth / 2 - 100, screenHeight / 2 - 20, 40, RED);
EndDrawing();
break;
}
BeginDrawing();
ClearBackground(BLACK);
DrawCircleV(ballPosition, ballSize, WHITE);
DrawRectangleV(player1Position, (Vector2){ paddleWidth, paddleHeight }, WHITE);
DrawRectangleV(player2Position, (Vector2){ paddleWidth, paddleHeight }, WHITE);
DrawText(TextFormat("%d", player1Score), screenWidth / 4, 20, 40, WHITE);
DrawText(TextFormat("%d", player2Score), screenWidth * 3 / 4, 20, 40, WHITE);
EndDrawing();
}
CloseWindow();
return 0;
}