 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Aug 18, 2007 6:49 pm Post subject: [C#] Pong |
|
|
This is a project that my dad and I completed a while ago, and it just kind of occured to me to post it on here.
Yes, it is in C#, so I'm sorry for people that don't do C#.
Anyways, here's the code:
AI Class:
| Code: |
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
namespace Pong
{
public class AI : GameObject
{
private GameState _gameState;
private Paddle _paddle;
public override void Update(double gameTime, double elapsedTime)
{
Ball ClosestBall = null;
foreach (GameObject gameObject in _gameState.GameObjects)
{
if (gameObject is Ball)
{
if (ClosestBall == null)
{
ClosestBall = ((Ball)gameObject);
}
else
{
if (ClosestBall.Location.X > ((Ball)gameObject).Location.X)
{
}
else
{
ClosestBall = ((Ball)gameObject);
}
}
}
}
float Direction=0;
float Distance = _paddle.Location.Y - ClosestBall.Location.Y;
if (Distance > _gameState._speedPaddle)
Distance = _gameState._speedPaddle;
if (Distance > 0)
{
Direction = -1;
}
else
{
Direction = 1;
Distance = Distance * -1;
}
float _mult = 0;
if (Difficulty._noob == true)
{
_mult = 5;
}
else if (Difficulty._average == true)
{
_mult = 7;
}
else if (Difficulty._skilled == true)
{
_mult = 10;
}
else if (Difficulty._veryHard == true)
{
_mult = 15;
}
else
{
_mult = 7;
}
_paddle.Velocity.Y = (Direction * Distance * _mult) - ((_paddle.Size.Height / 2) * _mult);
//_paddle.Location.Y = ClosestBall.Location.Y - (_paddle.Size.Height / 2);
}
public override void Draw(Graphics graphics)
{
}
public AI(GameState gameState, Paddle paddle)
{
_gameState = gameState;
_paddle = paddle;
}
}
}
|
Ball Class:
| Code: |
using System;
using System.Collections.Generic;
using System.Text;
namespace Pong
{
public class Ball: Sprite
{
public Ball(GameState gameState, float x, float y)
: base(gameState, x, y, "Images/ball.bmp")
{
}
}
}
|
Difficulty Class:
| Code: |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Pong
{
public partial class Difficulty : Form
{
public static bool _noob = false;
public static bool _average = false;
public static bool _skilled = false;
public static bool _veryHard = false;
public Difficulty()
{
InitializeComponent();
}
public void Noob_CheckedChanged(object sender, EventArgs e)
{
//Noob.Checked = true;
_noob = true;
return;
}
public void Average_CheckedChanged(object sender, EventArgs e)
{
//Average.Checked = true;
_average = true;
return;
}
public void Skilled_CheckedChanged(object sender, EventArgs e)
{
//Skilled.Checked = true;
_skilled = true;
return;
}
private void Veryhard_CheckedChanged(object sender, EventArgs e)
{
//Veryhard.Checked = true;
_veryHard = true;
return;
}
public void Difficulty_Load(object sender, EventArgs e)
{
}
}
}
|
GameObject Class:
| Code: |
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Pong
{
public abstract class GameObject
{
public abstract void Update(double gameTime, double elapsedTime);
public abstract void Draw(Graphics graphics);
}
}
|
GameState Class:
| Code: |
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Media;
namespace Pong
{
public class GameState
{
public int LeftScore;
public int RightScore;
public int _speedPaddle = 1000;
public SizeF GameArea;
public Ball ball;
public Ball ball2;
public Ball ball3;
public Ball ball4;
public List<GameObject> GameObjects = new List<GameObject>();
public Paddle PaddleLeft;
public Paddle PaddleRight;
public States State = States.Starting;
private Font _font = new Font("OCR A Extended", 18);
private Brush _brush = new SolidBrush(Color.White);
private static Random _random = new Random();
public int ScoreToWin = Setup.ScoreToWin;
// PlaySound()
[DllImport("winmm.dll", SetLastError = true,
CallingConvention = CallingConvention.Winapi)]
static extern bool PlaySound(
string pszSound,
IntPtr hMod,
SoundFlags sf);
// Flags for playing sounds. For this example, we are reading
// the sound from a filename, so we need only specify
// SND_FILENAME | SND_ASYNC
[Flags]
public enum SoundFlags : int
{
SND_SYNC = 0x0000, // play synchronously (default)
SND_ASYNC = 0x0001, // play asynchronously
SND_NODEFAULT = 0x0002, // silence (!default) if sound not found
SND_MEMORY = 0x0004, // pszSound points to a memory file
SND_LOOP = 0x0008, // loop the sound until next sndPlaySound
SND_NOSTOP = 0x0010, // don't stop any currently playing sound
SND_NOWAIT = 0x00002000, // don't wait if the driver is busy
SND_ALIAS = 0x00010000, // name is a registry alias
SND_ALIAS_ID = 0x00110000, // alias is a predefined ID
SND_FILENAME = 0x00020000, // name is file name
SND_RESOURCE = 0x00040004 // name is resource name or atom
}
public GameState(SizeF gameArea)
{
GameArea = gameArea;
//Reload the sound effects
}
public void Draw(Graphics graphics)
{
//and draw the game objects
foreach (GameObject gameObject in GameObjects)
{
gameObject.Draw(graphics);
}
//Draw the scores and any text
if (State != States.Playing)
{
graphics.DrawString("Press any key to play", _font, _brush, 135, 300);
}
if (State == States.RightWin)
{
graphics.DrawString("Right Player Wins! Press F1 to return to the main menu.", _font, _brush, 135, 200);
}
if (State == States.LeftWin)
{
graphics.DrawString("Left Player Wins! Press F1 to return to the main menu.", _font, _brush, 135, 200);
}
//Score goes on the right hand side of the screen so calculate the correct position by measuring the string
graphics.DrawString(LeftScore.ToString(), _font, _brush, GameArea.Width / 3 - graphics.MeasureString(LeftScore.ToString(), _font).Width, 0);
graphics.DrawString(RightScore.ToString(), _font, _brush, GameArea.Width / 2 - graphics.MeasureString(RightScore.ToString(), _font).Width, 0);
}
public void Update(double gameTime, double elapsedTime)
{
//Updates all the game objects
if (State == States.Playing)
{
//Set the velocity of the sprite based on which keys are pressed
PaddleLeft.Velocity.Y = 0;
PaddleRight.Velocity.Y = 0;
if (Keyboard.LeftUp)
{
PaddleLeft.Velocity.Y = -_speedPaddle;
}
if (Keyboard.LeftDown)
{
PaddleLeft.Velocity.Y = _speedPaddle;
}
if (Keyboard.RightUp)
{
PaddleRight.Velocity.Y = -_speedPaddle;
}
if (Keyboard.RightDown)
{
PaddleRight.Velocity.Y = _speedPaddle;
}
//Limit the animation to the screen
if (PaddleRight.Location.Y < 0)
PaddleRight.Location.Y = 0;
if (PaddleLeft.Location.Y < 0)
PaddleLeft.Location.Y = 0;
if (PaddleRight.Location.Y > GameArea.Height - PaddleRight.Size.Height)
PaddleRight.Location.Y = GameArea.Height - PaddleRight.Size.Height;
if (PaddleLeft.Location.Y > GameArea.Height - PaddleRight.Size.Height)
PaddleLeft.Location.Y = GameArea.Height - PaddleRight.Size.Height;
foreach (GameObject gameObject in GameObjects)
{
gameObject.Update(gameTime, elapsedTime);
}
//Check for any collisions
CheckBallCollision(ball);
CheckBallCollision(ball2);
CheckBallCollision(ball3);
CheckBallCollision(ball4);
}
}
public void CheckBallCollision(Ball ball)
{
if (ball == null)
return;
bool bounce=false;
if (Sprite.Collision(ball, (Sprite)PaddleLeft))
bounce = true;
if (Sprite.Collision(ball, (Sprite)PaddleRight))
bounce = true;
if (bounce)
{
if (Setup.Volume)
PlaySound(@"music\click.wav", IntPtr.Zero,
SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC);
ball.Velocity.Y = ball.Velocity.Y * -1;
int mult = 1;
if (ball.Velocity.X > 0)
mult = -1;
ball.Velocity.X = (_random.Next(200, 400) + 0) * mult;
ball.Velocity.Y = (_random.Next(200, 400) + 0);
}
else
{
if (ball.Location.Y < 0)
{
ball.Location.Y = 0;
ball.Velocity.Y = ball.Velocity.Y * -1;
if (Setup.Volume)
PlaySound(@"music\click.wav", IntPtr.Zero,
SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC);
}
if (ball.Location.Y + ball.Size.Height > GameArea.Height)
{
ball.Location.Y = GameArea.Height - ball.Size.Height;
ball.Velocity.Y = ball.Velocity.Y * -1;
if (Setup.Volume)
PlaySound(@"music\click.wav", IntPtr.Zero,
SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC);
}
if (ball.Location.X < 0)
{
if (Setup.Volume)
{
PlaySound(@"music\hold.wav", IntPtr.Zero,
SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC);
}
spawnBall(ball);
RightScore += 1;
CheckWin();
}
if (ball.Location.X > GameArea.Width)
{
if (Setup.Volume)
{
PlaySound(@"music\hold.wav", IntPtr.Zero,
SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC);
}
spawnBall(ball);
LeftScore += 1;
CheckWin();
}
}
}
private void CheckWin()
{
if (RightScore == ScoreToWin)
{
this.State = States.RightWin;
}
if (LeftScore == ScoreToWin)
this.State = States.LeftWin;
}
public void spawnBall(Ball ball)
{
//System.Threading.Thread.Sleep(500);
ball.Location.X = GameArea.Width / 2;
ball.Location.Y = GameArea.Height / 2;
int mult = 1;
if (ball.Velocity.Y > 0)
mult = -1;
ball.Velocity.X = 300 * mult;
ball.Velocity.Y = (_random.Next(400) + 200);
}
public void Initialize()
{
//Create all the main gameobjects
GameObjects.Clear();
ball = new Ball(this, GameArea.Width / 2, GameArea.Height/2);
GameObjects.Add(ball);
if (Setup.Balls == 2)
{
ball2 = new Ball(this, GameArea.Width / 2, GameArea.Height / 2);
GameObjects.Add(ball2);
}
if (Setup.Balls == 4)
{
ball2 = new Ball(this, GameArea.Width / 2, GameArea.Height / 2);
GameObjects.Add(ball2);
ball3 = new Ball(this, GameArea.Width / 2, GameArea.Height / 2);
GameObjects.Add(ball3);
ball4 = new Ball(this, GameArea.Width / 2, GameArea.Height / 2);
GameObjects.Add(ball4);
}
PaddleLeft = new Paddle(this, 20, GameArea.Height / 2);
PaddleRight = new Paddle(this,562, GameArea.Height / 2);
if (Setup.VsComputer == true)
{
AI ai = new AI(this, PaddleRight);
GameObjects.Add(ai);
}
GameObjects.Add(PaddleLeft);
GameObjects.Add(PaddleRight);
//Reset the game state
LeftScore = 0;
RightScore = 0;
ball.Velocity.X = 200;
ball.Velocity.Y = 1;
if (ball2 != null)
{
ball2.Velocity.X =- 200;
ball2.Velocity.Y = -1;
}
if (ball3 != null)
{
ball3.Velocity.X = 200;
ball3.Velocity.Y = 1;
}
if (ball4 != null)
{
ball4.Velocity.X = -200;
ball4.Velocity.Y = -1;
}
}
}
public enum States
{
Starting,
Playing,
RightWin,
LeftWin
}
}
|
Keyboard Class:
| Code: |
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace Pong
{
public static class Keyboard
{
//public static bool Left = false;
public static bool LeftUp = false;
public static bool LeftDown = false;
public static bool RightUp = false;
public static bool RightDown = false;
public static void KeyDown(Keys keys)
{
if (keys == Keys.Escape)
System.Environment.Exit(0);
if (keys == Keys.F1)
Settings.returnToMenu();
//if (keys == Keys.Left) Left = true;
if (keys == Keys.Z) LeftDown = true;
if (keys == Keys.A) LeftUp = true;
if (keys == Keys.L) RightDown = true;
if (keys == Keys.P) RightUp = true;
}
public static void KeyUp(System.Windows.Forms.Keys keys)
{
//if (keys == Keys.Left) Left = false;
if (keys == Keys.Z) LeftDown = false;
if (keys == Keys.A) LeftUp = false;
if (keys == Keys.L) RightDown = false;
if (keys == Keys.P) RightUp = false;
}
}
}
|
Paddle Class:
| Code: |
using System;
using System.Collections.Generic;
using System.Text;
namespace Pong
{
public class Paddle : Sprite
{
public Paddle(GameState gameState, float x, float y)
: base(gameState, x, y, "Images/paddle.bmp")
{
}
}
}
|
Pong Class:
| Code: |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace Pong
{
public partial class Pong : Form
{
private Stopwatch _timer = new Stopwatch();
private double _lastTime;
private long _frameCounter;
private GameState _gameState;
public Pong()
{
//Setup the form
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
//Startup the game state
_gameState = new GameState(ClientSize);
initialize();
}
private void initialize()
{
_gameState.Initialize();
//Initialise and start the timer
_lastTime = 0.0;
_timer.Reset();
_timer.Start();
}
private void Pong_Paint(object sender, PaintEventArgs e)
{
//Work out how long since we were last here in seconds
double gameTime = _timer.ElapsedMilliseconds / 1000.0;
double elapsedTime = gameTime - _lastTime;
_lastTime = gameTime;
_frameCounter++;
//Perform any animation and updates
_gameState.Update(gameTime, elapsedTime);
//Draw everything
_gameState.Draw(e.Graphics);
//Force the next Paint()
this.Invalidate();
}
private void Pong_KeyUp(object sender, KeyEventArgs e)
{
Keyboard.KeyUp(e.KeyCode);
}
private void Pong_KeyDown(object sender, KeyEventArgs e)
{
//If we are not playing then a keypress starts the game
if (_gameState.State != States.Playing)
{
//If we are currently at gameover then need to reset everything
if (_gameState.State == States.LeftWin || _gameState.State == States.RightWin )
{
initialize();
}
_gameState.State = States.Playing;
}
Keyboard.KeyDown(e.KeyCode);
}
private void Pong_Load(object sender, EventArgs e)
{
this.Top = 50;
}
}
}
|
Program Class:
| Code: |
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Pong
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Settings());
}
}
}
|
Settings Class:
| Code: |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Pong
{
public partial class Settings : Form
{
public static Pong frm;
public static Difficulty difficulty;
public static Settings settings;
public static void returnToMenu()
{
frm.Hide();
settings.Show();
frm = null;
}
public Settings()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler(Settings_KeyDown);
}
private void Settings_Load(object sender, EventArgs e)
{
}
private void ScoreToWin_TextChanged(object sender, EventArgs e)
{
Setup.ScoreToWin = int.Parse(ScoreToWin.Text);
}
private void Volume_CheckedChanged(object sender, EventArgs e)
{
Setup.Volume = Volume.Checked;
}
private void VsPlayer_Click(object sender, EventArgs e)
{
Setup.VSPlayer = true;
settings = this;
frm = new Pong();
frm.Show();
this.Hide();
}
private void Settings_KeyDown(object sender, KeyEventArgs e)
{
Keyboard.KeyDown(e.KeyCode);
}
private void DoubleBall_CheckedChanged_1(object sender, EventArgs e)
{
Setup.Balls = 2;
}
private void SingleBall_CheckedChanged(object sender, EventArgs e)
{
Setup.Balls = 1;
}
private void QuadBall_CheckedChanged(object sender, EventArgs e)
{
Setup.Balls = 4;
}
private void VsComp_Click(object sender, EventArgs e)
{
{
Setup.VsComputer = true;
settings = this;
frm = new Pong();
frm.Show();
this.Hide();
}
}
private void button1_Click(object sender, EventArgs e)
{
difficulty = new Difficulty();
difficulty.Show();
}
}
}
|
Setup Class:
| Code: |
using System;
using System.Collections.Generic;
using System.Text;
namespace Pong
{
public static class Setup
{
public static bool Volume = true;
public static int Balls = 1;
public static int ScoreToWin = 10;
private static bool _vsComputer;
public static bool VsComputer
{
get {
return _vsComputer;
}
set {
_vsComputer = value;
if (_vsComputer == true)
_vsPlayer = false;
else
_vsPlayer = true;
}
}
private static bool _vsPlayer;
public static bool VSPlayer
{
get{
return _vsPlayer;
}
set{
_vsPlayer = value;
if (_vsPlayer == true)
_vsComputer = false;
else
_vsComputer = true;
}
}
}
}
|
Sprite Class:
| Code: |
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Pong
{
public class Sprite : GameObject
{
public PointF Velocity;
public PointF Location;
public SizeF Size;
public int CurrentFrame;
protected List<Bitmap> _frames = new List<Bitmap>();
protected GameState _gameState;
public Sprite(GameState gameState, float x, float y, string filename)
{
//Load the bitmap
string path = System.Environment.CurrentDirectory;
filename = System.IO.Path.Combine(path, filename);
_frames.Add(new Bitmap(filename));
//Set the location and use the hieght and width from the 1st frame
initialize(gameState, x, y, _frames[0].Width, _frames[0].Height);
}
public Sprite(GameState gameState, float x, float y, string filename1, string filename2)
{
//Load the 2 animation frames
_frames.Add(new Bitmap(filename1));
_frames.Add(new Bitmap(filename2));
//Set the location and use the hieght and width from the 1st frame
initialize(gameState, x, y, _frames[0].Width, _frames[0].Height);
}
private void initialize(GameState gameState, float x, float y, float width, float height)
{
_gameState = gameState;
Location.X = x;
Location.Y = y;
Size.Width = width;
Size.Height = height;
CurrentFrame = 0;
}
public override void Update(double gameTime, double elapsedTime)
{
//Move the sprite
Location.X += Velocity.X * (float)elapsedTime;
Location.Y += Velocity.Y * (float)elapsedTime;
}
public override void Draw(Graphics graphics)
{
//Draw the correct frame at the current point
graphics.DrawImage(_frames[CurrentFrame], Location.X, Location.Y, Size.Width, Size.Height);
}
public static bool isInRectangle(PointF point, Sprite sprite)
{
float px = point.X;
float py = point.Y;
float leftx = sprite.Location.X ;
float rightx = sprite.Location.X + sprite.Size.Width ;
float topy = sprite.Location.Y ;
float bottomy = sprite.Location.Y + sprite.Size.Height;
if( px >= leftx && px <= rightx && py >= topy && py <= bottomy)
return true;
return false;
}
public static bool Collision(Sprite sprite1, Sprite sprite2)
{
//See if the sprite rectangles overlap
return !(sprite1.Location.X >= sprite2.Location.X + sprite2.Size.Width
|| sprite1.Location.X + sprite1.Size.Width <= sprite2.Location.X
|| sprite1.Location.Y >= sprite2.Location.Y + sprite2.Size.Height
|| sprite1.Location.Y + sprite1.Size.Height <= sprite2.Location.Y);
}
}
}
|
Edit:
Sorry, forgot to post to compiled version. =P
http://rapidshare.com/files/41285667/Pong.zip
Compiled Version ^
Last edited by samuri25404 on Sun Aug 19, 2007 9:07 am; edited 1 time in total |
|
| Back to top |
|
 |
gmaster1440 Master Cheater
Reputation: 0
Joined: 26 Jun 2006 Posts: 365 Location: a,b,c++
|
Posted: Sat Aug 18, 2007 7:55 pm Post subject: |
|
|
i congratulate you on making a very fine code. I like it...
_________________
c++ pointers... |
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Aug 18, 2007 8:19 pm Post subject: |
|
|
nice job. but y woudnt u just release the project file instead of posting all the source code?
_________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sat Aug 18, 2007 8:29 pm Post subject: |
|
|
Yea. Please post the .exe file. I really want to see the finished thing =)
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
FullyAwesome I post too much
Reputation: 0
Joined: 05 Apr 2007 Posts: 4438 Location: Land Down Under
|
Posted: Sat Aug 18, 2007 8:30 pm Post subject: |
|
|
very nice job you and your dad did =D
_________________
|
|
| Back to top |
|
 |
mikeymike Grandmaster Cheater
Reputation: 0
Joined: 16 Nov 2006 Posts: 575 Location: Russia
|
Posted: Sat Aug 18, 2007 9:04 pm Post subject: |
|
|
Compile it for us please, and post the source, and not the code of it!
_________________
|
|
| Back to top |
|
 |
kaikashu Master Cheater
Reputation: 0
Joined: 04 Apr 2007 Posts: 251 Location: Wait Let me check...
|
Posted: Sat Aug 18, 2007 9:44 pm Post subject: |
|
|
NOO SAM!!!
Lol He sent me the program so Its lookin good.
_________________
|
|
| Back to top |
|
 |
Lyfa The Lonely Man
Reputation: 12
Joined: 02 Nov 2008 Posts: 743
|
Posted: Sun Dec 21, 2008 6:01 pm Post subject: |
|
|
Could you please update the download link? Theres an error or whatever
_________________
|
|
| Back to top |
|
 |
FullyAwesome I post too much
Reputation: 0
Joined: 05 Apr 2007 Posts: 4438 Location: Land Down Under
|
Posted: Sun Dec 21, 2008 7:05 pm Post subject: |
|
|
you know you could of pmed him instead of bumping a 4 month old thread.
_________________
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|