Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
241 lines (224 loc) · 7.15 KB
/
Copy pathProgram.cs
File metadata and controls
241 lines (224 loc) · 7.15 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using Raylib_cs;
using System.Numerics;
class Program
{
public static void Main()
{
int sizex = 1300;
int sizey = 800;
int txtsize = 30;
float playerSpeed = 4.5f;
float speedIncrease = 1.1f;
Raylib.InitWindow(sizex, sizey, "title");
Raylib.SetTargetFPS(60);
Context context = new Context(sizex, sizey);
while (!Raylib.WindowShouldClose())
{
HandleInputs(context, playerSpeed);
if (context.playing) ProcessGame(context, speedIncrease);
DrawContext(context, txtsize);
}
Raylib.CloseWindow();
}
public static void HandleInputs(Context context, float playerSpeed) // QWERTY
{
if (context.playing) // GAME INPUTS
{
if (Raylib.IsKeyDown(KeyboardKey.S) && !Raylib.IsKeyDown(KeyboardKey.X))
{
// P1 LEFT
if (context.player1.y - playerSpeed > 0)
{
context.player1.y -= playerSpeed;
}
}
else if (Raylib.IsKeyDown(KeyboardKey.X) && !Raylib.IsKeyDown(KeyboardKey.S))
{
// P1 RIGHT
if (context.player1.y + playerSpeed + context.player1.sizey < context.sizey)
{
context.player1.y += playerSpeed;
}
}
if (Raylib.IsKeyDown(KeyboardKey.K) && !Raylib.IsKeyDown(KeyboardKey.O))
{
// P2 LEFT
if (context.player2.y + playerSpeed + context.player1.sizey < context.sizey)
{
context.player2.y += playerSpeed;
}
}
else if (Raylib.IsKeyDown(KeyboardKey.O) && !Raylib.IsKeyDown(KeyboardKey.K))
{
// P2 RIGHT
if (context.player2.y - playerSpeed > 0)
{
context.player2.y -= playerSpeed;
}
}
}
else // MENU INPUTS
{
if (Raylib.IsMouseButtonPressed(MouseButton.Left))
{
// Start game
context.playing = true;
context.score = 0;
}
}
}
public static void ProcessGame(Context context, float speedIncrease)
{
// COLLIDE ball
// w players
if (context.ball.Collide(context.player1))
{
// bounce right
context.ball.x = context.player1.x + context.player1.sizex;
context.ball.speed.X = -context.ball.speed.X;
context.ball.speed = Vector2.Multiply(context.ball.speed, speedIncrease);
context.score++;
}
else if (context.ball.Collide(context.player2))
{
// bounce left
context.ball.x = context.player2.x - context.ball.sizex;
context.ball.speed.X = -context.ball.speed.X;
context.ball.speed = Vector2.Multiply(context.ball.speed, speedIncrease);
context.score++;
}
// w walls
if (context.ball.x <= 0)
{
// Game over P2 win
context.playing = false;
context.winner = 2;
context.Reset();
}
else if (context.ball.x + context.ball.sizex >= context.sizex)
{
// Game over P1 win
context.playing = false;
context.winner = 1;
context.Reset();
}
if (context.ball.y <= 0)
{
// bounce down
context.ball.y = 0;
context.ball.speed.Y = -context.ball.speed.Y;
}
else if (context.ball.y + context.ball.sizey >= context.sizey)
{
// bounce up
context.ball.y = context.sizey - context.ball.sizey;
context.ball.speed.Y = -context.ball.speed.Y;
}
// MOVE ball
context.ball.x += context.ball.speed.X;
context.ball.y += context.ball.speed.Y;
}
public static void DrawContext(Context context, int txtsize)
{
bool firstStart = context.score == -1;
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.Black);
if (!context.playing) // game over/not started overlay
{
Raylib.DrawText(
"move with S+X and K+O",
context.sizex - 250,
context.sizey - 60,
20,
Color.White
);
Raylib.DrawText(
"click to play",
context.sizex - 140,
context.sizey - 30,
20,
Color.White
);
if (firstStart)
{
Raylib.DrawText(
"Plopong",
context.sizex / 2 - (int)Math.Round(txtsize * 2 * 2.5),
70,
txtsize * 2,
Color.White
);
}
else
{
Raylib.DrawText(
"game over",
context.sizex / 2 - (int)Math.Round(txtsize * 2.5),
50,
txtsize,
Color.White
);
Raylib.DrawText(
"score was " + context.score,
(int)Math.Round(context.sizex / 8f),
(int)Math.Round(2f * context.sizey / 3f),
txtsize,
Color.White
);
if (context.winner == 1) {
Raylib.DrawText(
"P1 wins !",
(int)Math.Round(context.sizex *0.1),
(int)Math.Round(1f * context.sizey / 3f),
txtsize,
Color.White
);
} else {
Raylib.DrawText(
"P2 wins !",
(int)Math.Round(context.sizex *0.8),
(int)Math.Round(1f * context.sizey / 3f),
txtsize,
Color.White
);
}
}
}
else
{
// score
Raylib.DrawText(
"score - " + context.score,
context.sizex / 2 - txtsize,
20,
(int)Math.Round(txtsize * 0.6),
Color.Gray
);
}
// p1
Raylib.DrawRectangle(
(int)Math.Round(context.player1.x),
(int)Math.Round(context.player1.y),
context.player1.sizex,
context.player1.sizey,
Color.White
);
// p2
Raylib.DrawRectangle(
(int)Math.Round(context.player2.x),
(int)Math.Round(context.player2.y),
context.player2.sizex,
context.player2.sizey,
Color.White
);
// ball
Raylib.DrawRectangle(
(int)Math.Round(context.ball.x),
(int)Math.Round(context.ball.y),
context.ball.sizex,
context.ball.sizey,
Color.Red
);
Raylib.EndDrawing();
}
}