-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
211 lines (170 loc) · 6.89 KB
/
Program.cs
File metadata and controls
211 lines (170 loc) · 6.89 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Pong demo in Raylib
By PixelRunner
*/
using Raylib_cs;
namespace RaylibGame1;
class Program
{
const int WIDTH = 800;
const int HEIGHT = 480;
[STAThread]
public static void Main()
{
Raylib.InitWindow(WIDTH, HEIGHT, "Pong");
Raylib.SetTargetFPS(60);
int p1Score = 0;
int p2Score = 0;
int gameState = 0;
int frameCounter = 0;
// ball
Ball ball = new Ball(WIDTH / 2, HEIGHT / 2, 1, 1);
Rectangle ballRect;
// paddle p1
Paddle p1 = new Paddle(20, HEIGHT / 2, 2, KeyboardKey.W, KeyboardKey.S);
Rectangle p1Rect;
// paddle p2
Paddle p2 = new Paddle(WIDTH - 20, HEIGHT / 2, 2, KeyboardKey.Up, KeyboardKey.Down);
Rectangle p2Rect;
while (!Raylib.WindowShouldClose())
{
// === UPDATE ===
switch(gameState)
{
case 0: // splash screen
frameCounter++;
if (frameCounter >= 240)
{
gameState++;
frameCounter = 0;
}
break;
case 1: // main menu
p1Score = 0;
p2Score = 0;
if (Raylib.GetKeyPressed() != 0)
{
gameState++;
frameCounter = 0;
}
break;
case 2: // game
ball.Update();
p1.Update();
p2.Update();
// prevent paddles from leaving the screen
if (p1.PosY <= 0)
{
p1.PosY = 0;
}
else if (p1.PosY + 40 >= HEIGHT)
{
p1.PosY = HEIGHT - 40;
}
if (p2.PosY <= 0)
{
p2.PosY = 0;
}
else if (p2.PosY + 40 >= HEIGHT)
{
p2.PosY = HEIGHT - 40;
}
ballRect = new Rectangle((float)ball.PosX + ball.SpeedX * ball.DirX, (float)ball.PosY + ball.SpeedY * ball.DirY, 10f, 10f);
p1Rect = new Rectangle((float)p1.PosX, (float)p1.PosY, 10f, 40f);
p2Rect = new Rectangle((float)p2.PosX, (float)p2.PosY, 10f, 40f);
// change ball direction if hitting the top or bottom of screen
if (ball.PosY <= 0 || ball.PosY + 10 >= HEIGHT)
{
ball.ChangeDirY();
}
// change ball direction if hitting a paddle
if (Raylib.CheckCollisionRecs(ballRect, p1Rect))
{
ball.ChangeDirX();
ball.SpeedX++;
}
if (Raylib.CheckCollisionRecs(ballRect, p2Rect))
{
ball.ChangeDirX();
ball.SpeedX++;
}
// reset game and add to a player's score if ball hits respective wall
if (ball.PosX <= 0)
{
ball.PosX = WIDTH / 2;
ball.PosY = HEIGHT / 2;
p2Score++;
ball.SpeedX = 1;
}
else if (ball.PosX + 10 >= WIDTH)
{
ball.PosX = WIDTH / 2;
ball.PosY = HEIGHT / 2;
p1Score++;
ball.SpeedX = 1;
}
// end the game if one of the player's scores hits 6
if (p1Score >= 6 || p2Score >= 6)
{
gameState++;
frameCounter = 0;
}
break;
case 3: // game over
if (Raylib.GetKeyPressed() != 0)
{
gameState = 1;
frameCounter = 0;
}
break;
}
// === RENDER ===
Raylib.BeginDrawing();
//Raylib.ClearBackground(new Color(0, 0, 0, 16));
switch (gameState)
{
case 0: // splash screen
Raylib.DrawRectangle(0, 0, WIDTH, HEIGHT, new Color(0, 0, 0, 16));
if (frameCounter <= 120)
{
Raylib.DrawText("PixelRunner Presents...", WIDTH / 2 - 130, HEIGHT / 2 - 10, 20, Color.White);
}
break;
case 1: // main menu
Raylib.ClearBackground(new Color(0, 0, 0, 16));
Raylib.DrawText("Raylib Pong", WIDTH / 2 - 150, HEIGHT / 2 - 25, 50, Color.White);
Raylib.DrawText("Press any key to play...", 20, HEIGHT - 50, 20, new Color(0, 255, 0));
break;
case 2: // game
Raylib.DrawRectangle(0, 0, WIDTH, HEIGHT, new Color(0, 0, 0, 64));
frameCounter++;
if (frameCounter <= 120)
{
Raylib.DrawText("P1: Use W/S to move\nP2: Use Arrow Keys to move", WIDTH / 2 - 130, HEIGHT / 2 - 10, 20, new Color(0, 255, 0));
}
ball.Render(ball.PosX, ball.PosY);
p1.Render(p1.PosX, p1.PosY);
p2.Render(p2.PosX, p2.PosY);
Raylib.DrawText(Convert.ToString(p1Score), 40, 40, 40, Color.White);
Raylib.DrawText(Convert.ToString(p2Score), WIDTH - 100, 40, 40, Color.White);
break;
case 3: // game over
Raylib.ClearBackground(new Color(0, 0, 0, 16));
string winTxt;
if (p1Score > p2Score)
{
winTxt = $"Player 1 wins by {p1Score - p2Score} points!";
}
else
{
winTxt = $"Player 2 wins by {p2Score - p1Score} points!";
}
Raylib.DrawText(winTxt, 20, HEIGHT / 2 - 25, 50, Color.White);
Raylib.DrawText("Press any key to continue...", 20, HEIGHT - 50, 20, new Color(0, 255, 0));
break;
}
Raylib.EndDrawing();
}
Raylib.CloseWindow();
}
}