public Ball(Control control, float minX, float maxX, float minY, float maxY, Bat player1, Bat player2, GameState gameState) : base(control) { //Keep a reference to the gamestate around for updating the scores _gameState = gameState; //Limits for the ball to bounce _minX = minX; _maxX = maxX; _minY = minY; _maxY = maxY; reset(); //Must be called after the limits are set since it uses them! //The bats to check for collisions with _player1 = player1; _player2 = player2; //Look & feel control.BackColor = _color; control.Width = _radius; control.Height = _radius; setHeightWidth(); // Create SoundPlayer Object _beep = new SoundPlayer("beep.wav"); //Preload the sound file _beep.Load(); }
public TinyTennis() { InitializeComponent(); SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true); gameState = new GameState(); //Create the bat Sprites - they need the keyboard controls and the gameplay area limits _player1 = new Bat(player1Bat, 30, Keys.Q, Keys.A, 0, ClientSize.Height); //use this line for a second human player //_player2 = new Bat(player2Bat, ClientSize.Width - 30 - Bat.Width, Keys.P, Keys.L, 0, ClientSize.Height); //use this line for a computer player _player2 = new Bat(player2Bat, ClientSize.Width - 30 - Bat.Width, 0, ClientSize.Height); //Create the ball sprite. It needs the gameplay area limits and references to the bats to be able to check for collisions _ball = new Ball(ballControl, 0, ClientSize.Width, 0, ClientSize.Height, _player1, _player2, gameState); //Connect the AI player with the ball _player2.Ball = _ball; //Initialise and start the timer _lastTime = 0.0; _timer.Start(); }
/// <summary> /// Does a rectangle to rectangle test between this sprites rectangle and the passed in bat's rectangle /// See http://www.tekpool.com/?p=23 for an explanation of this algorithm /// </summary> /// <param name="bat">The bat to test for intersection with</param> /// <returns></returns> private bool collision(Bat bat) { return !(Location.X > bat.Location.X + bat.Size.Width || Location.X + Size.Width < bat.Location.X || Location.Y > bat.Location.Y + bat.Size.Height || Location.Y + Size.Height < bat.Location.Y); }