[C#] Pong Emulator Using GDI

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
I was bored, so I created my own PongEmulator that you can watch.

2cBZa.gif


Code:
//
// Pong Emulator Using GDI Demonstration
// Developed by AceInfinity
// Copyright (c) Tech.Reboot.Pro
//

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class PongEmulator : PictureBox
{
	private Rectangle rect1;
	private Rectangle rect2;
	private Rectangle ball;

	private int vertical = 5;

	private BallDirection ballDirectionY = BallDirection.Up;
	private BallDirection ballDirectionX = BallDirection.Left;
	private enum BallDirection { Left = 0, Right, Up, Down }

	#region Constructor
	public PongEmulator()
	{
		rect1 = new Rectangle(Point.Empty, new Size(5, 20));
		rect2 = new Rectangle(new Point(this.Width - 5, 0), new Size(5, 20));
		ball = new Rectangle(new Point(this.Width / 2 - 3, this.Height / 2 - 3), new Size(5, 5));
		Start();
	}
	#endregion Constructor

	public void Start()
	{
		T.Tick -= T_Tick;
		T.Tick += T_Tick;
		T.Interval = 1;
		T.Start();
	}

	public void Stop()
	{
		T.Tick -= T_Tick;
		T.Stop();
	}

	#region Timer Animation
	private Timer T = new Timer();
	private static Random R = new Random();

	private void T_Tick(object sender, EventArgs e)
	{
		switch (ballDirectionX)
		{
			case BallDirection.Left:
				ball.Location = new Point(ball.X - 2, ball.Y + vertical);
				rect1.Location = new Point(rect1.X, ball.Y - rect1.Height / 2);
				break;
			case BallDirection.Right:
				ball.Location = new Point(ball.X + 2, ball.Y + vertical);
				rect2.Location = new Point(rect2.X, ball.Y - rect2.Height / 2);
				break;
		}
		
		if (ball.IntersectsWith(rect1) || ball.IntersectsWith(rect2))
		{
			ballDirectionX = (BallDirection)(((int)ballDirectionX + 1) % 2);
		}

		if (ball.Y < 0)
		{
			ball.Y = 0;
			ballDirectionY = BallDirection.Down;
			vertical = R.Next(-3, 3);
			if (vertical == 0) vertical = 1;
		}
		else if (ball.Y > this.Height - 5)
		{
			ball.Y = this.Height - 5;
			ballDirectionY = BallDirection.Up;
			vertical = R.Next(-3, 3);
			if (vertical == 0) vertical = -1;
		}

		Invalidate();
	}
	#endregion Timer Animation

	#region Overrides
	protected override void OnPaint(PaintEventArgs e)
	{
		base.OnPaint(e);
		e.Graphics.Clear(Color.FromArgb(15, 15, 15));

		using (HatchBrush HB = new HatchBrush(HatchStyle.Cross, Color.FromArgb(20, 20, 20)))
		{
			e.Graphics.FillRectangle(HB, this.ClientRectangle);
		}

		SolidBrush SB = new SolidBrush(Color.FromArgb(0, 255, 150));

		// Draw bar borders
		Pen P = new Pen(SB);
		e.Graphics.DrawRectangles(P, new Rectangle[] { rect1, rect2 });

		// Draw ball border
		P = new Pen(Color.FromArgb(255, 50, 0));
		e.Graphics.DrawRectangle(P, ball);

		// Fill bar rectangles
		SB = new SolidBrush(Color.FromArgb(0, 100, 45));
		e.Graphics.FillRectangles(SB, new Rectangle[] {
			Rectangle.FromLTRB(rect1.Left + 1, rect1.Top + 1, rect1.Right, rect1.Bottom),
			Rectangle.FromLTRB(rect2.Left + 1, rect2.Top + 1, rect2.Right, rect2.Bottom)
		});

		// Fill ball rectangle
		SB = new SolidBrush(Color.FromArgb(100, 15, 0));
		e.Graphics.FillRectangle(SB, Rectangle.FromLTRB(ball.Left + 1, ball.Top + 1, ball.Right, ball.Bottom));

		P.Dispose();
		SB.Dispose();
	}

	protected override void OnResize(EventArgs e)
	{
		base.OnResize(e);
		rect1 = new Rectangle(Point.Empty, new Size(5, 20));
		rect2 = new Rectangle(new Point(this.Width - 5, 0), new Size(5, 20));
		ball = new Rectangle(new Point(this.Width / 2 - 3, this.Height / 2 - 3), new Size(5, 5));
	}
	#endregion Overrides
}

(Nobody loses :thumbsup2:)

:cheers:
 
Last edited:
If you don't feel like debugging, then put Start(); into the constructor, and watch it play in the designer! :lol:
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top