-
Notifications
You must be signed in to change notification settings - Fork 0
/
PongSquared.old
410 lines (343 loc) · 15.7 KB
/
PongSquared.old
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
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 p1Score = 0;
private int p2Score = 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 the pause/play button
pausePlayButton = new PictureBox();
pausePlayButton.Image = Properties.Resources.PlayIcon; // Assume you have pause and play icons in your resources
pausePlayButton.Size = new Size(50, 50);
pausePlayButton.SizeMode = PictureBoxSizeMode.StretchImage;
pausePlayButton.MouseDown += PausePlayButton_MouseDown;
this.Controls.Add(pausePlayButton);
// Create the reset button
resetButton = new PictureBox();
resetButton.Image = Properties.Resources.ResetIcon;
resetButton.Size = new Size(50, 50);
resetButton.SizeMode = PictureBoxSizeMode.StretchImage;
resetButton.MouseDown += ResetButton_MouseDown;
this.Controls.Add(resetButton);
leftPlatformPositionY = (this.ClientRectangle.Height - platformHeight) / 2;
rightPlatformPositionY = (this.ClientRectangle.Height - platformHeight) / 2;
// Set up the initial position and direction of the ball
Random random = new Random();
ballX = this.ClientRectangle.Width / 2;
ballY = random.Next(this.ClientRectangle.Height - ballSize);
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
// Set up the timer for updating the platform position
timer = new Timer();
timer.Interval = 16; // approximately 60 frames per second
timer.Tick += new EventHandler(UpdateGame);
timer.Start();
// Call SetButtonPositionAndSize after the button is created
SetButtonPositionAndSize();
}
private void SetButtonPositionAndSize()
{
// Set the initial position of the buttons (adjust the percentages as needed)
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 ResetButton_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Reset the ball and platforms
ResetBall();
ResetPlatforms();
// Set scores to 0
p1Score = 0;
p2Score = 0;
}
}
private void ResetPlatforms()
{
// Reset platform positions
leftPlatformPositionY = (this.ClientRectangle.Height - platformHeight) / 2;
rightPlatformPositionY = (this.ClientRectangle.Height - platformHeight) / 2;
// Reset platform velocities
leftPlatformVelocity = 0;
rightPlatformVelocity = 0;
}
private void PausePlayButton_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isPaused = !isPaused;
isManuallyPaused = !isManuallyPaused;
UpdatePausePlayButtonImage();
}
}
private void UpdatePausePlayButtonImage()
{
if (isPaused)
{
pausePlayButton.Image = Properties.Resources.PauseIcon;
}
else
{
pausePlayButton.Image = Properties.Resources.PlayIcon;
}
}
private void UpdateGame(object sender, EventArgs e)
{
// Check if either platform is moving
bool isLeftPlatformMoving = leftPlatformVelocity != 0;
bool isRightPlatformMoving = rightPlatformVelocity != 0;
// If neither platform is moving, do not update the ball
if (!isLeftPlatformMoving && !isRightPlatformMoving)
{
//return;
ResetBall();
}
if (!isPaused)
{
// Update left platform position based on velocity
leftPlatformPositionY += leftPlatformVelocity;
// Bounce away from the top and bottom borders
if (leftPlatformPositionY < 0 || leftPlatformPositionY > this.ClientRectangle.Height - platformHeight)
{
// Invert the velocity to create a bouncing effect
leftPlatformVelocity = -leftPlatformVelocity;
}
// Ensure the left platform stays within the form bounds
leftPlatformPositionY = Math.Max(0, Math.Min(this.ClientRectangle.Height - platformHeight, leftPlatformPositionY));
// Update right platform position based on velocity
rightPlatformPositionY += rightPlatformVelocity;
// Bounce away from the top and bottom borders
if (rightPlatformPositionY < 0 || rightPlatformPositionY > this.ClientRectangle.Height - platformHeight)
{
// Invert the velocity to create a bouncing effect
rightPlatformVelocity = -rightPlatformVelocity;
}
// Ensure the right platform stays within the form bounds
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
}
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
}
else if (ballX <= 0 || ballX + ballSize >= this.ClientRectangle.Width)
{
// Check if ball went out of bounds
if (ballX < 0)
{
// Right side scores
p2Score++;
}
else if (ballX + ballSize > this.ClientRectangle.Width)
{
// Left side scores
p1Score++;
}
ResetBall();
}
// Check collisions with top and bottom
else if (ballY <= 0 || ballY + ballSize >= this.ClientRectangle.Height)
{
ballDirectionY = -ballDirectionY; // Bounce back
}
}
// Redraw the form to update positions
this.Invalidate();
}
private void PongSquared_Paint(object sender, PaintEventArgs e)
{
DrawCenterLine(e.Graphics);
DrawCounters(e.Graphics);
}
private void PongSquared_Resize(object sender, EventArgs e)
{
// Update the position of the button when the form is resized
SetButtonPositionAndSize();
// Enforce 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 adjusted borders
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 };
g.DrawLine(dashedPen, new Point(middleX, 0), new Point(middleX, this.Height));
}
private void DrawCounters(Graphics g)
{
// Calculate the position of the middle of the form
int middleX = this.Width / 2;
// Set up the font and brush for drawing the counters
float fontSize = Math.Min(this.Width / 10, this.Height / 10); // Adjust the ratio as needed
Font counterFont = new Font("Agency FB", fontSize);
Brush counterBrush = Brushes.White;
int p1CounterWidth = (int)g.MeasureString(p1Score.ToString(), counterFont).Width;
int p2CounterWidth = (int)g.MeasureString(p2Score.ToString(), counterFont).Width;
// Calculate the middle point for the left counter
int leftCounterMiddleX = middleX - (middleX / 2) - p1CounterWidth / 2;
// Draw the left counter centered to the left of the middle point
g.DrawString(p1Score.ToString(), counterFont, counterBrush, new PointF(leftCounterMiddleX, 20));
// Calculate the middle point for the right counter
int rightCounterMiddleX = middleX + (middleX / 2) - p2CounterWidth / 2;
// Draw the right counter centered to the right of the middle point
g.DrawString(p2Score.ToString(), counterFont, counterBrush, new PointF(rightCounterMiddleX, 20));
}
private void PongSquared_KeyDown(object sender, KeyEventArgs e)
{
if (isPaused) return;
// Update counters when a key is pressed (for demonstration purposes)
if (e.KeyCode == Keys.A)
{
p1Score++;
}
else if (e.KeyCode == Keys.L)
{
p2Score++;
}
// Handle left platform movement
if (e.KeyCode == Keys.W)
{
leftPlatformVelocity = -platformSpeed;
}
else if (e.KeyCode == Keys.S)
{
leftPlatformVelocity = platformSpeed;
}
// Handle right platform movement
if (e.KeyCode == Keys.Up)
{
rightPlatformVelocity = -platformSpeed;
}
else if (e.KeyCode == Keys.Down)
{
rightPlatformVelocity = platformSpeed;
}
// Redraw the counters
this.Invalidate();
}
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
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Draw the left platform
e.Graphics.FillRectangle(Brushes.White, 50 + leftPlatformOffset, leftPlatformPositionY, platformWidth, platformHeight);
// Draw the right platform
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) + 6;
int ballCenterY = (this.ClientRectangle.Height / 2) - (ballSize / 2);
e.Graphics.FillEllipse(Brushes.White, ballCenterX, ballCenterY, ballSize, ballSize);
}
else
{
// Draw the ball when it is moving
e.Graphics.FillEllipse(Brushes.White, ballX, ballY, ballSize, ballSize);
}
}
private void PongSquared_ResizeBegin(object sender, EventArgs e)
{
isPaused = true;
UpdatePausePlayButtonImage();
}
private void PongSquared_ResizeEnd(object sender, EventArgs e)
{
if (!isManuallyPaused)
{
// If not manually paused, resume the game
isPaused = false;
UpdatePausePlayButtonImage();
}
}
// Dispose the timer when the form is closed
protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);
timer.Dispose();
}
}
}