// Add ball to game private void CreateBall() { ball = new Ball { LocationX = 390, LocationY = 500 }; canvas.Children.Add(ball); }
public Breakout(SpriteBatch spriteBatch) { this.spriteBatch = spriteBatch; this.screenWidth = spriteBatch.GraphicsDevice.Viewport.Width; this.screenHeight = spriteBatch.GraphicsDevice.Viewport.Height; this.paddle = new Paddle(screenWidth, screenHeight); this.wall = new Wall(screenWidth, screenHeight); this.ball = new Ball(paddle, wall, screenWidth, screenHeight); }
public Gamefield(int width, int height, Ball ball, Player player) { this.Width = width; this.Height = height; this.Ball = ball; this.Player = player; this.IsAlive = true; this.GameObjects = new List<GameObject>() { this.Player, this.Ball, }; InitializeBoard(); }
public Level(World world, Rectangle screenBounds, Player player) { _paddle = new Paddle(world, screenBounds); _ball = new Ball(world, _paddle, player); _walls = new Walls(world, screenBounds, _ball, _paddle); // TODO: Load level from file/resource for (int y = 0; y < 5; y++) { for (int x = 0; x < 10; x++) { _blocks.Add(new Block(world, screenBounds, x, y, player, _ball, _paddle)); } } }
/// <summary> /// destroy a ball and take a life /// </summary> /// <param name="ball">the ball being destroyed</param> public static void DestroyBall(Ball ball) { currentForm.Controls.Remove(ball.PictureBox); // remove picturebox from the form ball.PictureBox.Dispose(); // release all the resources being used by the picturebox BallsInPlay--; // there is 1 less ball in play // 2 players with their own lives, so calculate which player lost the life if (CurrentMode == GameMode.Versus) { // the ball hit the bottom in the left half, player 1 loses a life if (ball.Position.X + ball.Size.X * 0.5f <= FormSize.X * 0.5f) { LivesPlayer1 -= 1; LastPlayerLost = Player.Player1; UpdateScores(); // finish the game if it was player 1's last life if (LivesPlayer1 <= 0) { FinishGame(Player.Player1); } // otherwise replace the ball else { AddBall(); } } // the ball hit the bottom in the right half, player 2 else { LivesPlayer2 -= 1; LastPlayerLost = Player.Player2; UpdateScores(); // finish the game if it was player 2's last life if (LivesPlayer2 <= 0) { FinishGame(Player.Player2); } // otherwise replace the ball else { AddBall(); } } } // ball is destroyed, life is lost else { LivesPlayer1 -= 1; UpdateScores(); // display the new lives // finish the game if the was the last life if (LivesPlayer1 <= 0) { FinishGame(0); } // otherwise replace the ball else { AddBall(); } } }
// create a new ball public static void AddBall() { PictureBox newPictureBox = new PictureBox(); // create a picturebox for the ball newPictureBox.BackColor = Color.White; // ball is white newPictureBox.Size = new Size(50, 50); // 50x50 pixels in size currentForm.Controls.Add(newPictureBox); // ad picturebox to form BallsInPlay++; // another ball is in play Ball ball = new Ball(newPictureBox, new Vector(0, ((CurrentMode == GameMode.Classic) ? DefaultStartSpeed : StartSpeed))); // create the ball as a game object }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); _ballTexture = Content.Load<Texture2D>("ball"); _brickTexture = Content.Load<Texture2D>("block"); _paddleTexture = Content.Load<Texture2D>("Paddle"); _gameBall = new Ball(new Vector2(200, 200), _ballTexture, new Vector2(5, 5)); int[,] _map = new int[5,10] { {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1} }; _brickManager = new BrickManager(_map, _brickTexture, 5, 10); _brickManager.Init(); _paddle = new Paddle(new Vector2(200, 400), _paddleTexture); }
/* * Initiates logic. */ protected override void Initialize() { graphics.PreferredBackBufferWidth = 640; graphics.PreferredBackBufferHeight = 420; graphics.IsFullScreen = false; graphics.ApplyChanges(); Window.Title = "Breakout for Atari 2600 Replica"; ball = new Ball(); paddle = new Paddle(); decoration = new Decoration(); barX = 32.0f; barY = 114.0f; bars = new List<List<Bar>>(); for (int i = 0; i < 6; i++) { bars.Add(new List<Bar>()); for (int j = 0; j < 18; j++) { bars[i].Add(new Bar(barX, barY, i)); barX += 32.0f; } barX = 32.0f; barY += 12.0f; } base.Initialize(); }
//see if the ball will collide with the given rect // if it should be inside (like the frame) // or outside the rect (like the paddle) // returns 0,0 if no collision, or // a point indicating a new direction // also adjusts ball velocity so won't collide Point WillCollide( Ball ball, Rectangle rectTest, bool fShouldBeInside) { var res = new Point(); int nCollides = 0; if (fShouldBeInside) { nCollides = 1; } else { var rect = ball.GetRect(); rect = new Rectangle( new Point( rect.Left + ball._velocity.X, rect.Top + ball._velocity.Y), ball._size); if (rect.IntersectsWith(rectTest)) { nCollides = 1; } } if (ball._velocity.X != 0) { if (ball._velocity.X > 0) { var edgeX = rectTest.Left; if (fShouldBeInside) { edgeX = rectTest.Left + rectTest.Width; } if (ball._pos.X < edgeX && ball._pos.X + ball._size.Width + ball._velocity.X >= edgeX) { res.X = -nCollides; } } else { var edgeX = rectTest.Left + rectTest.Width; if (fShouldBeInside) { edgeX = rectTest.Left; } if (ball._pos.X > edgeX && ball._pos.X + ball._velocity.X <= edgeX) { res.X = nCollides; } } } if (ball._velocity.Y != 0) { if (ball._velocity.Y > 0) { var edgeY = rectTest.Top; if (fShouldBeInside) { edgeY = rectTest.Top + rectTest.Height; } if (ball._pos.Y < edgeY && ball._pos.Y + ball._size.Height + ball._velocity.Y >= edgeY) { res.Y = -nCollides; } } else { var edgeY = rectTest.Top + rectTest.Height; if (fShouldBeInside) { edgeY = rectTest.Top; } if (ball._pos.Y > edgeY && ball._pos.Y + ball._velocity.Y <= edgeY) { res.Y = nCollides; } } } if (res.X != 0) // if it collides in x direction { ball._velocity.X = res.X * (1 + _rand.Next(ball._speedMax)); } if (res.Y != 0) { ball._velocity.Y = res.Y * (1 + _rand.Next(ball._speedMax)); } return(res); }
// Use this for initialization void Start() { currentBall = GetComponentInChildren <Ball>(); }