Skip to content

Commit eeff174

Browse files
committed
Add Pong game to games directory
1 parent e5dad3f commit eeff174

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed

games/pong/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CC = gcc
2+
CFLAGS = -Wall -std=c99
3+
SRC = main.c
4+
OUT = game
5+
6+
LIBS = -lraylib -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo
7+
8+
$(OUT): $(SRC)
9+
$(CC) $(CFLAGS) $(SRC) -o $(OUT) $(LIBS)
10+
11+
clean:
12+
rm -f $(OUT)

games/pong/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Pong Game in C
2+
3+
A simple Pong game written in C using Raylib.
4+
5+
## How to Build
6+
7+
Make sure you have [raylib](https://www.raylib.com/) installed, then:
8+
9+
```bash
10+
make
11+
./game

games/pong/assets/button.wav

177 KB
Binary file not shown.

games/pong/main.c

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#include <stdio.h>
2+
#include <raylib.h>
3+
#include <stdlib.h>
4+
#define ScreenWidth 800
5+
#define ScreenHeight 600
6+
#define PaddleWidth 30
7+
#define PaddleHeight 100
8+
#define PaddleSpeed 3
9+
#define BallRadius 15
10+
11+
int SCORE =0;
12+
int HightScore= 0;
13+
int gamesPlayed = 0;
14+
typedef enum {
15+
STATE_MENU,
16+
STATE_PLAYING
17+
} GameState;
18+
GameState gameState = STATE_MENU;
19+
20+
21+
22+
23+
24+
typedef struct Paddle{
25+
26+
float x ;
27+
float y ;
28+
29+
30+
31+
32+
}Paddle;
33+
34+
35+
36+
typedef struct Ball{
37+
38+
float x ;
39+
float y ;
40+
float xv;
41+
float yv;
42+
}Ball;
43+
44+
void Menu(Sound fxButton) {
45+
int x = (ScreenWidth - 80)/2;
46+
int y = (ScreenHeight - 30)/2;
47+
Rectangle start = {x, y, PaddleHeight, PaddleWidth};
48+
DrawRectangleRec(start, DARKGRAY);
49+
DrawText("START", x + 15, y + 10, 20, RAYWHITE);
50+
51+
char scoreBuffer[32];
52+
sprintf(scoreBuffer, "Score: %d", SCORE);
53+
DrawText(scoreBuffer, 10, 10, 20, WHITE);
54+
55+
if (gamesPlayed > 0) {
56+
char highScoreBuffer[32];
57+
sprintf(highScoreBuffer, "High Score: %d", HightScore);
58+
DrawText(highScoreBuffer, 10, 40, 20, WHITE);
59+
}
60+
61+
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && CheckCollisionPointRec(GetMousePosition(), start)) {
62+
PlaySound(fxButton);
63+
gameState = STATE_PLAYING;
64+
65+
// Update high score before resetting score
66+
if (SCORE > HightScore) {
67+
HightScore = SCORE;
68+
}
69+
70+
SCORE = 0;
71+
gamesPlayed++;
72+
}
73+
}
74+
75+
76+
void resetBall(Ball *ball){
77+
78+
79+
ball->x =400 ;
80+
ball->y =300;
81+
82+
}
83+
void resetPaddle(Paddle *paddle_left ,Paddle *paddle_right){
84+
paddle_left->x =0 ;
85+
paddle_left->y = (ScreenHeight - PaddleHeight) / 2;
86+
87+
paddle_right->x =ScreenWidth - PaddleWidth ;
88+
paddle_right->y =(ScreenHeight - PaddleHeight) / 2 ;
89+
90+
91+
}
92+
void update(Ball *ball, Paddle *left, Paddle *right, float deltaTime) {
93+
char buffer[16];
94+
sprintf(buffer, "%d", SCORE);
95+
DrawText(buffer, 400, 10, 20, WHITE);
96+
97+
DrawRectangle((int)left->x, (int)left->y, PaddleWidth, PaddleHeight, RAYWHITE);
98+
DrawRectangle((int)right->x, (int)right->y, PaddleWidth, PaddleHeight, RAYWHITE);
99+
DrawCircle((int)ball->x, (int)ball->y, BallRadius, RAYWHITE);
100+
101+
// Paddle movement
102+
if (IsKeyDown(KEY_W)) left->y -= PaddleSpeed * deltaTime * 200;
103+
if (IsKeyDown(KEY_S)) left->y += PaddleSpeed * deltaTime * 200;
104+
if (IsKeyDown(KEY_UP)) right->y -= PaddleSpeed * deltaTime * 200;
105+
if (IsKeyDown(KEY_DOWN)) right->y += PaddleSpeed * deltaTime * 200;
106+
107+
// Clamp paddles
108+
if (left->y < 0) left->y = 0;
109+
if (left->y + PaddleHeight > ScreenHeight) left->y = ScreenHeight - PaddleHeight;
110+
if (right->y < 0) right->y = 0;
111+
if (right->y + PaddleHeight > ScreenHeight) right->y = ScreenHeight - PaddleHeight;
112+
113+
// Update ball position
114+
ball->x += ball->xv * deltaTime * 200;
115+
ball->y += ball->yv * deltaTime * 200;
116+
117+
if (ball->y - BallRadius < 0) {
118+
ball->y = BallRadius;
119+
ball->yv *= -1;
120+
}
121+
if (ball->y + BallRadius > ScreenHeight) {
122+
ball->y = ScreenHeight - BallRadius;
123+
ball->yv *= -1;
124+
}
125+
126+
if (ball->x - BallRadius < 0 || ball->x + BallRadius > ScreenWidth) {
127+
resetBall(ball);
128+
resetPaddle(left, right);
129+
gameState = STATE_MENU;
130+
return;
131+
}
132+
133+
// Paddle collision
134+
Vector2 center = { ball->x, ball->y };
135+
136+
Rectangle leftRec = { left->x, left->y, PaddleWidth, PaddleHeight };
137+
Rectangle rightRec = { right->x, right->y, PaddleWidth, PaddleHeight };
138+
139+
if (CheckCollisionCircleRec(center, BallRadius, leftRec)) {
140+
ball->x = left->x + PaddleWidth + BallRadius; // push ball away
141+
ball->xv *= -1;
142+
SCORE++;
143+
}
144+
if (CheckCollisionCircleRec(center, BallRadius, rightRec)) {
145+
ball->x = right->x - BallRadius; // push ball away
146+
ball->xv *= -1;
147+
SCORE++;
148+
}
149+
}
150+
151+
152+
153+
int main(void){
154+
155+
SetConfigFlags(FLAG_WINDOW_HIGHDPI);
156+
InitWindow(ScreenWidth, ScreenHeight, "Pong");
157+
InitAudioDevice();
158+
Sound fxButton = LoadSound("assets/button.wav");
159+
160+
Paddle left = {0, (ScreenHeight - PaddleHeight) / 2};
161+
Paddle right = {ScreenWidth - PaddleWidth, (ScreenHeight - PaddleHeight) / 2};
162+
Ball ball = {400.0,300.0,0.8,0.7};
163+
164+
165+
while (!WindowShouldClose()) {
166+
float deltaTime = GetFrameTime();
167+
168+
169+
BeginDrawing();
170+
ClearBackground(BLACK);
171+
172+
if (gameState == STATE_MENU) {
173+
Menu(fxButton);
174+
} else if (gameState == STATE_PLAYING) {
175+
//Game Logic
176+
update(&ball,&left,&right,deltaTime);
177+
178+
}
179+
180+
EndDrawing();
181+
182+
}
183+
184+
CloseWindow();
185+
186+
187+
188+
return 0;
189+
}
190+
191+
// gcc -o game main.c -lraylib -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo

0 commit comments

Comments
 (0)