-
Notifications
You must be signed in to change notification settings - Fork 0
/
PongSquared.cs
396 lines (331 loc) · 15.1 KB
/
PongSquared.cs
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BenigaLowLevelGraphics
{
public partial class PongSquared : Form
{
private int leftScore = 0;
private int rightScore = 0;
private int platformSpeed = 10;
private int platformHeight = 50;
private int platformWidth = 10;
private int leftPlatformPositionY;
private int leftPlatformVelocity = 0;
private int leftPlatformOffset = 100;
private int rightPlatformPositionY;
private int rightPlatformVelocity = 0;
private int rightPlatformOffset = 100;
private int ballSize = 20;
private int ballX;
private int ballY;
private int ballSpeed = 5;
private int ballDirectionX;
private int ballDirectionY;
private bool isPaused = false;
private bool isManuallyPaused = false;
private Timer timer;
private PictureBox pausePlayButton;
private PictureBox resetButton;
public PongSquared()
{
InitializeComponent();
this.Width = 1200;
this.Height = 800;
this.Paint += PongSquared_Paint;
this.Resize += PongSquared_Resize;
this.KeyDown += PongSquared_KeyDown;
// Create buttons
pausePlayButton = CreateButton(Properties.Resources.PlayIcon, PausePlayButton_MouseDown, 0.96, 0.02);
resetButton = CreateButton(Properties.Resources.ResetIcon, ResetButton_MouseDown, 0.96, 0.10);
// Set up initial positions of platforms
leftPlatformPositionY = (this.ClientRectangle.Height - platformHeight) / 2;
rightPlatformPositionY = (this.ClientRectangle.Height - platformHeight) / 2;
// Set up the initial position and direction of the ball
ResetBall();
// Set up the timer for updating the platform position
timer = new Timer { Interval = 16 };
timer.Tick += UpdateGame;
timer.Start();
SetButtonPositionAndSize();
}
private PictureBox CreateButton(Image image, MouseEventHandler handler, double xRatio, double yRatio)
{
var button = new PictureBox
{
Image = image,
Size = new Size(50, 50),
SizeMode = PictureBoxSizeMode.StretchImage
};
button.MouseDown += handler;
this.Controls.Add(button);
button.Location = new Point((int)(this.Width * xRatio - button.Width), (int)(this.Height * yRatio));
return button;
}
private void SetButtonPositionAndSize()
{
// Initial button positions and size
if (pausePlayButton != null)
{
pausePlayButton.Location = new Point((int)(this.Width * 0.96 - pausePlayButton.Width), (int)(this.Height * 0.02));
}
if (resetButton != null)
{
resetButton.Location = new Point((int)(this.Width * 0.96 - resetButton.Width), (int)(this.Height * 0.10));
}
}
private void PongSquared_Paint(object sender, PaintEventArgs e)
{
DrawCenterLine(e.Graphics);
DrawScores(e.Graphics);
}
private void PongSquared_Resize(object sender, EventArgs e)
{
// Update the position of the button when the form is resized
SetButtonPositionAndSize();
// Minimum form size
if (this.Width < 1000)
{
this.Width = 1000;
}
if (this.Height < 600)
{
this.Height = 600;
}
// Adjust the positions of the platforms and the ball
leftPlatformPositionY = (this.ClientRectangle.Height - platformHeight) / 2;
rightPlatformPositionY = (this.ClientRectangle.Height - platformHeight) / 2;
// Calculate the new position of the ball based on the change in form dimensions
float xRatio = (float)this.ClientRectangle.Width / this.ClientSize.Width;
float yRatio = (float)this.ClientRectangle.Height / this.ClientSize.Height;
ballX = (int)(ballX * xRatio);
ballY = (int)(ballY * yRatio);
// Ensure the ball stays within the form when resizing
ballX = Math.Max(0, Math.Min(this.ClientRectangle.Width - ballSize, ballX));
ballY = Math.Max(0, Math.Min(this.ClientRectangle.Height - ballSize, ballY));
this.Invalidate();
}
private void DrawCenterLine(Graphics g)
{
int middleX = this.Width / 2;
Pen dashedPen = new Pen(Color.White);
dashedPen.Width = 4;
dashedPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
dashedPen.DashPattern = new float[] { 2f, 3f }; // { dash length, distance to next }
g.DrawLine(dashedPen, new Point(middleX, 0), new Point(middleX, this.Height));
}
private void DrawScores(Graphics g)
{
// Adjusts font size on form size
float fontSize = Math.Min(this.Width / 10, this.Height / 10);
Font counterFont = new Font("Agency FB", fontSize);
Brush counterBrush = Brushes.White;
int middleX = this.Width / 2;
// Calculate font width to offset center
int leftScoreWidth = (int)g.MeasureString(leftScore.ToString(), counterFont).Width;
int rightScoreWidth = (int)g.MeasureString(rightScore.ToString(), counterFont).Width;
// Calculate the middle point for the left and right counter
int p1ScoreMiddleX = middleX - (middleX / 2) - leftScoreWidth / 2;
int p2ScoreMiddleX = middleX + (middleX / 2) - rightScoreWidth / 2;
// Draw the left and right counter centered to respective middle points
g.DrawString(leftScore.ToString(), counterFont, counterBrush, new PointF(p1ScoreMiddleX, 20));
g.DrawString(rightScore.ToString(), counterFont, counterBrush, new PointF(p2ScoreMiddleX, 20));
}
private void UpdateGame(object sender, EventArgs e)
{
// If neither platform is moving, do not move ball
bool isLeftPlatformMoving = leftPlatformVelocity != 0;
bool isRightPlatformMoving = rightPlatformVelocity != 0;
if (!isLeftPlatformMoving && !isRightPlatformMoving)
{
ResetBall();
//return;
}
if (!isPaused)
{
// Update platform positions based on velocity
leftPlatformPositionY += leftPlatformVelocity;
rightPlatformPositionY += rightPlatformVelocity;
// Bounce away from the top and bottom borders
if (leftPlatformPositionY < 0 || leftPlatformPositionY > this.ClientRectangle.Height - platformHeight)
{
leftPlatformVelocity = -leftPlatformVelocity;
}
if (rightPlatformPositionY < 0 || rightPlatformPositionY > this.ClientRectangle.Height - platformHeight)
{
rightPlatformVelocity = -rightPlatformVelocity;
}
// Ensure the platforms stay within the form
leftPlatformPositionY = Math.Max(0, Math.Min(this.ClientRectangle.Height - platformHeight, leftPlatformPositionY));
rightPlatformPositionY = Math.Max(0, Math.Min(this.ClientRectangle.Height - platformHeight, rightPlatformPositionY));
// Update ball position based on direction
ballX += ballSpeed * ballDirectionX;
ballY += ballSpeed * ballDirectionY;
// Check collisions with platforms
if (ballDirectionX == -1 && // Ball is moving towards the left platform
ballX <= 50 + platformWidth + leftPlatformOffset && // Ball is to the left of the left platform
ballX + ballSize >= 50 + leftPlatformOffset && // Ball is to the right of the left platform
ballY + ballSize >= leftPlatformPositionY && ballY <= leftPlatformPositionY + platformHeight) // Ball is within the vertical bounds of the left platform
{
ballDirectionX = 1; // Bounce back
// Check if the ball hits the top or bottom half of the platform
if (ballY + ballSize / 2 < leftPlatformPositionY + platformHeight / 2)
{
ballDirectionY = -1; // Bounce upwards
}
else if (ballY + ballSize / 2 > leftPlatformPositionY + platformHeight / 2)
{
ballDirectionY = 1; // Bounce downwards
}
}
else if (ballDirectionX == 1 && // Ball is moving towards the right platform
ballX + ballSize >= this.ClientRectangle.Width - 50 - platformWidth - rightPlatformOffset && // Ball is to the right of the right platform
ballX <= this.ClientRectangle.Width - 50 - platformWidth - rightPlatformOffset && // Ball is to the left of the right platform
ballY + ballSize >= rightPlatformPositionY && ballY <= rightPlatformPositionY + platformHeight) // Ball is within the vertical bounds of the right platform
{
ballDirectionX = -1; // Bounce back
// Check if the ball hits the top or bottom half of the platform
if (ballY + ballSize / 2 < rightPlatformPositionY + platformHeight / 2)
{
ballDirectionY = -1; // Bounce upwards
}
else if (ballY + ballSize / 2 > rightPlatformPositionY + platformHeight / 2)
{
ballDirectionY = 1; // Bounce downwards
}
}
else if (ballX <= 0 || ballX + ballSize >= this.ClientRectangle.Width) // Check if ball went out of bounds
{
if (ballX < 0)
{
rightScore++;
}
else if (ballX + ballSize > this.ClientRectangle.Width)
{
leftScore++;
}
ResetBall();
}
else if (ballY <= 0 || ballY + ballSize >= this.ClientRectangle.Height) // Check collisions with top and bottom
{
ballDirectionY = -ballDirectionY; // Bounce back
}
}
// Update positions by redrawing the form
this.Invalidate();
}
private void PausePlayButton_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isPaused = !isPaused;
isManuallyPaused = !isManuallyPaused;
UpdatePausePlayButtonImage();
}
}
private void ResetButton_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isPaused = false;
UpdatePausePlayButtonImage();
leftScore = 0;
rightScore = 0;
ResetBall();
ResetPlatforms();
}
}
private void ResetBall()
{
ballX = this.ClientRectangle.Width / 2 - ballSize / 2;
ballY = this.ClientRectangle.Height / 2 - ballSize / 2;
Random random = new Random();
ballDirectionX = (random.Next(2) == 0) ? -1 : 1; // Randomly set initial direction left or right
//ballDirectionY = (random.Next(2) == 0) ? -1 : 1; // Randomly set initial direction up or down
ballDirectionY = (random.Next(3) == 0) ? 0 : (random.Next(2) == 0 ? 1 : -1); // Added option to go straight left or right
}
private void ResetPlatforms()
{
leftPlatformPositionY = (this.ClientRectangle.Height - platformHeight) / 2;
rightPlatformPositionY = (this.ClientRectangle.Height - platformHeight) / 2;
leftPlatformVelocity = 0;
rightPlatformVelocity = 0;
}
private void UpdatePausePlayButtonImage()
{
if (isPaused)
{
pausePlayButton.Image = Properties.Resources.PauseIcon;
}
else
{
pausePlayButton.Image = Properties.Resources.PlayIcon;
}
}
private void PongSquared_KeyDown(object sender, KeyEventArgs e)
{
// Ensures no funny business with the ball's position when paused
if (isPaused) return;
// Left platform controls
if (e.KeyCode == Keys.W)
{
leftPlatformVelocity = -platformSpeed;
}
else if (e.KeyCode == Keys.S)
{
leftPlatformVelocity = platformSpeed;
}
// Right platform controls
if (e.KeyCode == Keys.Up)
{
rightPlatformVelocity = -platformSpeed;
}
else if (e.KeyCode == Keys.Down)
{
rightPlatformVelocity = platformSpeed;
}
}
private void PongSquared_ResizeBegin(object sender, EventArgs e)
{
isPaused = true;
UpdatePausePlayButtonImage();
}
private void PongSquared_ResizeEnd(object sender, EventArgs e)
{
if (!isManuallyPaused)
{
isPaused = false;
UpdatePausePlayButtonImage();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Draw the left and right platforms
e.Graphics.FillRectangle(Brushes.White, 50 + leftPlatformOffset, leftPlatformPositionY, platformWidth, platformHeight);
e.Graphics.FillRectangle(Brushes.White, this.ClientRectangle.Width - platformWidth - 50 - rightPlatformOffset, rightPlatformPositionY, platformWidth, platformHeight);
// Draw the ball in the center if not moving
if (leftPlatformVelocity == 0 && rightPlatformVelocity == 0)
{
int ballCenterX = (this.ClientRectangle.Width / 2) - (ballSize / 2) + 7; // +7 to align the ball with the dashed line
int ballCenterY = (this.ClientRectangle.Height / 2) - (ballSize / 2);
e.Graphics.FillRectangle(Brushes.White, ballCenterX, ballCenterY, ballSize, ballSize);
}
else
{
e.Graphics.FillRectangle(Brushes.White, ballX, ballY, ballSize, ballSize);
}
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);
timer.Dispose();
}
}
}