/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { var screenBounds = GraphicsDevice.Viewport.Bounds; if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } var touchState = Keyboard.GetState(); if (touchState.IsKeyDown(Keys.Left)) { PaddleBottom.X = PaddleBottom.X - (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, screenBounds.Left, screenBounds.Right - PaddleBottom.Width); } if (touchState.IsKeyDown(Keys.Right)) { PaddleBottom.X = PaddleBottom.X + (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, screenBounds.Left, screenBounds.Right - PaddleBottom.Width); } if (touchState.IsKeyDown(Keys.A)) { PaddleTop.X = PaddleTop.X - (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); PaddleTop.X = MathHelper.Clamp(PaddleTop.X, screenBounds.Left, screenBounds.Right - PaddleTop.Width); } if (touchState.IsKeyDown(Keys.D)) { PaddleTop.X = PaddleTop.X + (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); PaddleTop.X = MathHelper.Clamp(PaddleTop.X, screenBounds.Left, screenBounds.Right - PaddleTop.Width); } var ballPositionChange = Ball.Direction * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed); Ball.X += ballPositionChange.X; Ball.Y += ballPositionChange.Y; // Ball - side walls if (Walls.Any(w => CollisionDetector.Overlaps(Ball, w))) { Ball.Direction = Ball.Direction * new Vector2(-1, 1); Ball.Speed = Ball.Speed * Ball.BumpSpeedIncreaseFactor; } // Ball - winning walls if (Goals.Any(w => CollisionDetector.Overlaps(Ball, w))) { Ball.X = screenBounds.Center.ToVector2().X; Ball.Y = screenBounds.Center.ToVector2().Y; Ball.Speed = GameConstants.DefaultInitialBallSpeed; HitSound.Play(); } // Paddle - ball collision if (CollisionDetector.Overlaps(Ball, PaddleTop) && Ball.Direction.Y < 0 || (CollisionDetector.Overlaps(Ball, PaddleBottom) && Ball.Direction.Y > 0)) { Ball.Direction = Ball.Direction * new Vector2(1, -1); Ball.Speed *= Ball.BumpSpeedIncreaseFactor; } base.Update(gameTime); }
public Coordinates GetDestination(Direction direction, int x, int y) { switch (direction) { case Direction.Up: y--; break; case Direction.Right: x++; break; case Direction.Down: y++; break; case Direction.Left: x--; break; default: return(new Coordinates(x, y)); } // destination is wall var isWall = Walls.Any(w => w.X == x && w.Y == y); // destination out of map var isOut = x >= Width || x < 0 || y >= Height || y < 0; return(new Coordinates(x, y, isWall, isOut)); }
private ComputerSnake GetRandomComputerSnake() { Color headColor = Color.FromArgb(rng.Next(256), rng.Next(256), rng.Next(256)); Color bodyColor = Color.FromArgb(rng.Next(256), rng.Next(256), rng.Next(256)); Array directionValues = Enum.GetValues(typeof(Direction)); Direction startDirection = (Direction)directionValues.GetValue(rng.Next(directionValues.Length)); while (startDirection == Direction.None) { startDirection = (Direction)directionValues.GetValue(rng.Next(directionValues.Length)); } int x = rng.Next(2, Width - 1); int y = rng.Next(2, Height - 1); while (Walls.Any(w => w.X == x && w.Y == y)) { x = rng.Next(2, Width - 1); y = rng.Next(2, Height - 1); } ComputerSnake randomComputerSnake = new ComputerSnake(x, y, startDirection, headColor, bodyColor); return(randomComputerSnake); }
private void AssembleMap() { Map = new List <List <Piece> >(); for (int y = 0; y < Height; y++) { Map.Add(new List <Piece>()); for (int x = 0; x < Width; x++) { if (Walls.Any(p => p.X == x && p.Y == y)) { Map[y].Add(new Piece(x, y, PieceType.Wall, Color.Blue)); } else if (Snakes.Any(s => s.snakePieces.Any(p => p.X == x && p.Y == y && p.Type == PieceType.SnakePiece))) { Map[y].Add(new Piece(x, y, PieceType.SnakePiece, Snakes.Where(s => s.IsAtPos(x, y)).FirstOrDefault().snakePieces.Where(p => p.X == x && p.Y == y).FirstOrDefault().Color)); } else if (Snakes.Any(s => s.snakePieces.Any(p => p.X == x && p.Y == y && p.Type == PieceType.HeadPiece))) { Map[y].Add(new Piece(x, y, PieceType.HeadPiece, Snakes.Where(s => s.IsAtPos(x, y)).FirstOrDefault().snakePieces.Where(p => p.X == x && p.Y == y).FirstOrDefault().Color)); } else if (FoodPieces.Any(p => p.X == x && p.Y == y)) { Piece piece = FoodPieces.Where(p => p.X == x && p.Y == y).FirstOrDefault(); Map[y].Add(piece); } else { Map[y].Add(new Piece(x, y, PieceType.Empty)); } } } }
private bool TryMove(int dx, int dy) { //preconditions if (dx * dy != 0) { throw new InvalidOperationException("Only horizontal or vertical movement allowed."); } if (dx < -1 || dx > 1) { throw new ArgumentException(nameof(dx)); } if (dy < -1 || dy > 1) { throw new ArgumentException(nameof(dy)); } var x = PlayerX + dx; var y = PlayerY + dy; if (x < 0 || x >= Width || y < 0 || y >= Height) { return(false); } //Wall if (Walls.Any(wall => wall.Item1 == x && wall.Item2 == y)) { return(false); } var step = new Step(); //Crate var crate = Crates.FirstOrDefault(c => c.X == x && c.Y == y); if (crate != null) { var x0 = x + dx; var y0 = y + dy; if (Walls.Any(wall => wall.Item1 == x0 && wall.Item2 == y0) || Crates.Any(c => c.X == x0 && c.Y == y0)) { return(false); } step.Crate = crate; step.OldCrateX = crate.X; step.OldCrateY = crate.Y; crate.X += dx; crate.Y += dy; } step.OldPlayerX = PlayerX; step.OldPlayerY = PlayerY; _steps.Push(step); PlayerX += dx; PlayerY += dy; return(true); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } var touchState = Keyboard.GetState(); if (touchState.IsKeyDown(Keys.Left)) { PaddleBottom.X = PaddleBottom.X - (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); } if (touchState.IsKeyDown(Keys.Right)) { PaddleBottom.X = PaddleBottom.X + (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); } if (touchState.IsKeyDown(Keys.A)) { PaddleTop.X = PaddleTop.X - (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); } if (touchState.IsKeyDown(Keys.D)) { PaddleTop.X = PaddleTop.X + (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); } PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, 0, 500 - PaddleBottom.Width); PaddleTop.X = MathHelper.Clamp(PaddleTop.X, 0, 500 - PaddleTop.Width); if (Walls.Any(w => CollisionDetector.Overlaps(Ball, w))) { Ball.Direction *= Direction.SW; Ball.incrementSpeed(); } if (Goals.Any(w => CollisionDetector.Overlaps(Ball, w))) { Ball.X = 250; Ball.Y = 450; Ball.Speed = GameConstants.DefaultInitialBallSpeed; HitSound.Play(); } if ((CollisionDetector.Overlaps(Ball, PaddleTop) && Ball.Direction.Y < 0) || (CollisionDetector.Overlaps(Ball, PaddleBottom) && Ball.Direction.Y > 0)) { Ball.Direction *= Direction.NE; Ball.incrementSpeed(); } var ballPositionState = Ball.Direction * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed); Direction d = Ball.Direction; Ball.X += ballPositionState.X; Ball.Y += ballPositionState.Y; base.Update(gameTime); }
public void FromMapData(MapData mapData) { Turn = mapData.Turn; Walls = mapData.Walls.Select(item => new Position(item[0], item[1])).ToList(); Blocks = mapData.Blocks.Select(item => new Position(item[0], item[1])).ToList(); Players = mapData.Players; Me = mapData.Players.Find(p => p.Name == Consts.MyName); Bombs = mapData.Bombs; Items = mapData.Items; Fires = mapData.Fires.Select(item => new Position(item[0], item[1])).ToList(); MinX = Walls.Select(item => item.x).Min(); int MaxX = Walls.Select(item => item.x).Max(); SizeX = MaxX - MinX + 1; MinY = Walls.Select(item => item.y).Min(); int MaxY = Walls.Select(item => item.y).Max(); SizeY = MaxY - MinY + 1; logger.Debug($"MinX: {MinX}, SizeX: {SizeX}, MinY {MinY}, SizeY {SizeY}"); var ary2 = Enumerable.Range(MinY, SizeY).Select(y => { return(Enumerable.Range(MinX, SizeX).Select(x => { var cell = new Cell() { X = x, Y = y, Wall = Walls.Any(w => w.x == x && w.y == y), Block = Blocks.Any(b => b.x == x && b.y == y), AnyPlayer = Players.Any(p => p.pos.x == x && p.pos.y == y && p.Name != Consts.MyName), MyPosition = Players.Any(p => p.pos.x == x && p.pos.y == y && p.Name == Consts.MyName), Bomb = Bombs.Find(b => b.pos.x == x && b.pos.y == y), Item = Items.Any(i => i.pos.x == x && i.pos.y == y), Fire = Fires.Any(f => f.x == x && f.y == y) }; var pos = new Position(x, y); return new KeyValuePair <Position, Cell>(pos, cell); })); }).SelectMany(e => e); CellDict = new CellDict(ary2, MinX, SizeX, MinY, SizeY); SetAboutToFire(); SetFallingWall(); CellDict.SetDistance(Me.pos); CellDict.Log(); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } var touchState = Keyboard.GetState(); if (touchState.IsKeyDown(Keys.Left)) { PaddleBottom.X = PaddleBottom.X - (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); } if (touchState.IsKeyDown(Keys.Right)) { PaddleBottom.X = PaddleBottom.X + (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); } if (touchState.IsKeyDown(Keys.A)) { PaddleTop.X = PaddleTop.X - (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); } if (touchState.IsKeyDown(Keys.D)) { PaddleTop.X = PaddleTop.X + (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); } PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, 0, 500 - PaddleBottom.Width); PaddleTop.X = MathHelper.Clamp(PaddleTop.X, 0, 500 - PaddleBottom.Width); var ballPositionChange = Ball.Direction * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed); Ball.X += ballPositionChange.X; Ball.Y += ballPositionChange.Y; // Ball - side walls if (Walls.Any(w => CollisionDetector.Overlaps(Ball, w))) { Ball.Direction = new Vector2(-Ball.Direction.X, Ball.Direction.Y); Ball.Speed = Ball.Speed * Ball.BumpSpeedIncreaseFactor; } // Ball - winning walls if (Goals.Any(w => CollisionDetector.Overlaps(Ball, w))) { Ball.X = (graphics.PreferredBackBufferWidth - GameConstants.DefaultBallSize) / 2; Ball.Y = (graphics.PreferredBackBufferHeight - GameConstants.DefaultBallSize) / 2; Ball.Speed = GameConstants.DefaultInitialBallSpeed; HitSound.Play(); } // Paddle - ball collision if (CollisionDetector.Overlaps(Ball, PaddleTop) && Ball.Direction.Y < 0 || (CollisionDetector.Overlaps(Ball, PaddleBottom) && Ball.Direction.Y > 0)) { Ball.Direction = new Vector2(Ball.Direction.X, -Ball.Direction.Y); Ball.Speed *= Ball.BumpSpeedIncreaseFactor; } if (Ball.Speed > GameConstants.MaxBallSpeed) { Ball.Speed = GameConstants.MaxBallSpeed; } base.Update(gameTime); }
private bool IntersectsWall(Point oldPosition, Point newPosition) { return(Walls.Any((x) => x.IntersectsWall(oldPosition, newPosition))); }
private bool HitsWall(Point proposedPosition, double radius) { return(Walls.Any((x) => x.HitsWall(proposedPosition, radius))); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } var touchState = Keyboard.GetState(); if (touchState.IsKeyDown(Keys.Left)) { movePaddleLeft(PaddleBottom, gameTime); } if (touchState.IsKeyDown(Keys.Right)) { movePaddleRight(PaddleBottom, gameTime); } if (touchState.IsKeyDown(Keys.A)) { movePaddleLeft(PaddleTop, gameTime); } if (touchState.IsKeyDown(Keys.D)) { movePaddleRight(PaddleTop, gameTime); } var screenBounds = GraphicsDevice.Viewport.Bounds; // D∈[-1 , 1] var ballPositionChange = Ball.Direction * MathHelper.Clamp((float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed), -5, 5); Ball.X += ballPositionChange.X; Ball.Y += ballPositionChange.Y; // Ball - side walls if (Walls.Any(w => CollisionDetector.Overlaps(Ball, w))) { Vector2 vector; vector = Ball.Direction; vector.X = -vector.X; Ball.Direction = vector; Ball.Direction = vector; if (Ball.Speed * Ball.BumpSpeedIncreaseFactor <= GameConstants.MaxBallSpeed) { Ball.Speed = Ball.Speed * Ball.BumpSpeedIncreaseFactor; } } // Ball - winning walls if (Goals.Any(w => CollisionDetector.Overlaps(Ball, w))) { Ball.X = screenBounds.Center.ToVector2().X; Ball.Y = screenBounds.Center.ToVector2().Y; Ball.Speed = GameConstants.DefaultInitialBallSpeed; HitSound.Play(); } // Paddle - ball collision if (CollisionDetector.Overlaps(Ball, PaddleTop) && Ball.Direction.Y < 0 || (CollisionDetector.Overlaps(Ball, PaddleBottom) && Ball.Direction.Y > 0)) { Vector2 vector; vector = Ball.Direction; vector.Y = -vector.Y; Ball.Direction = vector; if (Ball.Speed * Ball.BumpSpeedIncreaseFactor <= GameConstants.MaxBallSpeed) { Ball.Speed = Ball.Speed * Ball.BumpSpeedIncreaseFactor; } } base.Update(gameTime); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } var touchState = Keyboard.GetState(); // 1st player if (touchState.IsKeyDown(Keys.Left)) { PaddleBottom.X = PaddleBottom.X - (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); } if (touchState.IsKeyDown(Keys.Right)) { PaddleBottom.X = PaddleBottom.X + (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); } // 2nd player if (touchState.IsKeyDown(Keys.A)) { PaddleTop.X = PaddleTop.X - (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); } if (touchState.IsKeyDown(Keys.D)) { PaddleTop.X = PaddleTop.X + (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds); } // check if coordinates fit right into the game screen PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, 0, 500 - PaddleBottom.Width); PaddleTop.X = MathHelper.Clamp(PaddleTop.X, 0, 500 - PaddleTop.Width); // ball movement var ballPositionChange = Ball.Direction * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed); Ball.X += ballPositionChange.X; Ball.Y += ballPositionChange.Y; // Ball - side walls if (Walls.Any(w => CollisionDetector.Overlaps(Ball, w))) { Ball.Direction = new Vector2(-Ball.Direction.X, Ball.Direction.Y); SetNewBallSpeed(); } // Ball - winning walls if (Goals.Any(w => CollisionDetector.Overlaps(Ball, w))) { Ball.X = 230; Ball.Y = 430; Ball.Speed = GameConstants.DefaultInitialBallSpeed; HitSound.Play(); } // Paddle - ball collision if (CollisionDetector.Overlaps(Ball, PaddleTop) && Ball.Direction.Y < 0 || (CollisionDetector.Overlaps(Ball, PaddleBottom) && Ball.Direction.Y > 0)) { Ball.Direction = new Vector2(Ball.Direction.X, -Ball.Direction.Y); SetNewBallSpeed(); } base.Update(gameTime); }