public void CheckApples(Vector2 nextPos) { foreach (var apple in Apples) { if (apple.Position == nextPos) { SpawnPart(); OnAppleEaten?.Invoke(apple, EventArgs.Empty); // Checks if the apple did not spawn on the next HeadPosition, which isn't updated to the BodyPart List yet and therefore not checked otherwise. while (Apples[Apples.Count - 1].Position == nextPos) { Apples.RemoveAt(Apples.Count - 1); OnAppleEaten?.Invoke(apple, EventArgs.Empty); } break; } } }
public void RespOfApples() { while (Apples.Count < applesCount) { Apples.Add(new Apple()); foreach (var item in Obstacles) { if (Apples.Last().Collide(item)) { Apples.RemoveAt(Apples.Count - 1); break; } } for (int i = 0; i < Apples.Count; i++) { if (Apples.Last().Collide(Apples[i]) && Apples.Last() != Apples[i]) { Apples.RemoveAt(Apples.Count - 1); break; } } } }
public void SpawnApples() { while (Apples.Count < applesCount) { Apples.Add(new Apple()); foreach (var item in Walls) { if (Apples.Last().CollidesWith(item)) { Apples.RemoveAt(Apples.Count - 1); break; } } for (int i = 0; i < Apples.Count; i++) { if (Apples.Last().CollidesWith(Apples[i]) && Apples.Last() != Apples[i]) { Apples.RemoveAt(Apples.Count - 1); break; } } } }
internal void Move(int Step) { Point From = new Point(SnakeBody[SnakeBody.Count - 1].End.X, SnakeBody[SnakeBody.Count - 1].End.Y); switch (CurrentDirection) { case Direction.LEFT: SnakeBody[SnakeBody.Count - 1].End.X -= Step; break; case Direction.RIGHT: SnakeBody[SnakeBody.Count - 1].End.X += Step; break; case Direction.UP: SnakeBody[SnakeBody.Count - 1].End.Y -= Step; break; case Direction.DOWN: SnakeBody[SnakeBody.Count - 1].End.Y += Step; break; } Point To = new Point(SnakeBody[SnakeBody.Count - 1].End.X, SnakeBody[SnakeBody.Count - 1].End.Y); bool WillGrow = false; for (int x = Math.Min(From.X, To.X); x <= Math.Max(From.X, To.X); x++) { for (int y = Math.Min(From.Y, To.Y); y <= Math.Max(From.Y, To.Y); y++) { if (x == From.X && y == From.Y) { continue; } Point P = new Point(x, y); foreach (LineSeg Obstacle in Obstacles) { if (Intersect(P, Obstacle)) { SnakeBody[SnakeBody.Count - 1].End = P; if (HitWallAndLose != null) { HitWallAndLose(); } Console.WriteLine("Hit the obstacle, P is " + P.X + ", " + P.Y + ", and Obstacle is" + Obstacle.Start.X + "," + Obstacle.Start.Y + ") and " + Obstacle.End.X + ", " + Obstacle.End.Y); return; } } for (int i = 0; i < SnakeBody.Count - 1; i++) { if (Intersect(P, SnakeBody[i])) { SnakeBody[SnakeBody.Count - 1].End = P; if (HitSnakeAndLose != null) { HitSnakeAndLose(); } Console.WriteLine("Hit the body: P is " + P.X + "," + P.Y + "body ends at" + SnakeBody[i].End.X + ", " + SnakeBody[i].End.Y); return; } } int Removed = -1; for (int i = 0; i < Apples.Count; i++) { if (Math.Abs(Apples[i].X - P.X) <= AppleSize / 2 && Math.Abs(Apples[i].Y - P.Y) <= AppleSize / 2) { Removed = i; Appleseaten = i; WillGrow = true; break; } } if (Removed >= 0) { Apples.RemoveAt(Removed); AddApple(1);//mumber of apple to be added after the snake eats one apple if (EatAndGrow != null) { EatAndGrow(); } } } } if (WillGrow) { return; } if (SnakeBody[0].Start.X == SnakeBody[0].End.X) { if (Math.Abs(SnakeBody[0].Start.Y - SnakeBody[0].End.Y) > Step) { SnakeBody[0].Start.Y += (SnakeBody[0].Start.Y > SnakeBody[0].End.Y) ? (-Step) : Step; } else { SnakeBody.RemoveAt(0); } } else { if (Math.Abs(SnakeBody[0].Start.X - SnakeBody[0].End.X) > Step) { SnakeBody[0].Start.X += (SnakeBody[0].Start.X > SnakeBody[0].End.X) ? (-Step) : Step; } else { SnakeBody.RemoveAt(0); } } }
public void UpdatePlayer() { if (playerState != PlayerState.Dead) { player.velocity.X = MathHelper.Clamp( player.velocity.X + PLAYER_ACCELERATION * ((keyboard.IsKeyDown(Keys.D) ? 1 : 0) - (keyboard.IsKeyDown(Keys.A) ? 1 : 0)), -PLAYER_MAX_SPEED_X, PLAYER_MAX_SPEED_X); if (playerState == PlayerState.OnGround) { //Friction if (player.velocity.X < -PLAYER_FRICTION_GROUND || player.velocity.X > PLAYER_FRICTION_GROUND) { player.velocity.X = player.velocity.X - PLAYER_FRICTION_GROUND * Math.Sign(player.velocity.X); } else { player.velocity.X = 0; } //Jumping if (keyboard.IsKeyDown(Keys.Space) && keyboardOld.IsKeyUp(Keys.Space)) { player.velocity.Y = PLAYER_JUMP; playerState = PlayerState.InAir; } } else if (playerState == PlayerState.InAir) { //Friction if (player.velocity.X < -PLAYER_FRICTION_AIR || player.velocity.X > PLAYER_FRICTION_AIR) { player.velocity.X = player.velocity.X - PLAYER_FRICTION_AIR * Math.Sign(player.velocity.X); } else { player.velocity.X = 0; } } //Vector2 nextPosition = player.Center + player.velocity; //Vector2 nextRopeVector = (nextPosition - hook.Center); //if (hookState != HookState.Hooked || nextRopeVector.Length() < ropeLength || player.Center.Y <= hook.Center.Y) if (hookState == HookState.Hooked) { //player.velocity.Y += GRAVITY * (float)Math.Pow(1 - (1 / ropeLength), (player.Center.Y - hook.Center.Y) - ropeLength); player.velocity.Y += GRAVITY * (float)(-((HOOK_GRAVITY_MULTIPLIER - 1) / ropeLength) * (player.Center.Y - hook.Center.Y) + HOOK_GRAVITY_MULTIPLIER); } else { player.velocity.Y += GRAVITY; } player.velocity.X += (playerState == PlayerState.GrappleOut ? ApplyWind() * 0.5f : (playerState == PlayerState.InAir ? ApplyWind() * 1.5f : ApplyWind())); //Handle rope tension if (hookState == HookState.Hooked) { Vector2 oldNextPosition = player.Center + player.velocity; Vector2 newNextPositionRelative = oldNextPosition - hook.Center; if (newNextPositionRelative.Length() >= ropeLength) { newNextPositionRelative.Normalize(); newNextPositionRelative.X *= (float)ropeLength; newNextPositionRelative.Y *= (float)ropeLength; Vector2 newNextPosition = hook.Center + newNextPositionRelative; float velocityMagnitude = player.velocity.Length(); player.velocity = newNextPosition - player.Center; player.velocity.Normalize(); player.velocity *= velocityMagnitude; } /*if(nextRopeVector.Length() >= ropeLength) * { * Vector2 ropeVector = player.Center - hook.Center; * double theta = Math.Atan2(ropeVector.Y, ropeVector.X); * double forceMagnitude = Math.Sin(theta) * GRAVITY; * double angle = Math.Atan2(ropeVector.Y, ropeVector.X); * if (angle < Math.PI / 2 || angle >= Math.PI * 3 / 2) * angle += Math.PI / 2; * else * angle -= Math.PI / 2; * int sub = (Math.Atan2(player.velocity.Y, player.velocity.X) < angle) ? -1 : 1; * player.velocity = (new Vector2((float)(Math.Cos(angle)), (float)(Math.Sin(angle)))) * (float)(forceMagnitude + (player.velocity.Length() * sub)); * }*/ } //Change rope length if (hookState == HookState.Hooked) { if (keyboard.IsKeyDown(Keys.W) && ropeLength > 16) { ropeLength -= 1; Vector2 offsetFromHook = player.Center - hook.Center; if (offsetFromHook.Length() > ropeLength) { offsetFromHook = offsetFromHook * -1; offsetFromHook.Normalize(); player.velocity += offsetFromHook * 0.5f; } } else if (keyboard.IsKeyDown(Keys.S) && ropeLength < HOOK_MAX_LENGTH) { ropeLength += 1; } } for (int i = 0; i < TilesSpike.Count; i++) { var spike = TilesSpike[i]; if (player.Intersects(spike)) { PlayerDie(); return; } } for (int i = Apples.Count - 1; i >= 0; i--) { var apple = Apples[i]; if (player.WillIntersect(apple)) { Apples.RemoveAt(i); appleCount++; soundPickupApple.Play(); break; } } for (int i = 0; i < Grounders.Count; i++) { var mole = Grounders[i]; if (player.Intersects(mole)) { PlayerDie(); return; } } for (int i = 0; i < Flyer.Count; i++) { var bird = Flyer[i]; if (player.Intersects(bird)) { PlayerDie(); return; } } if (player.Up >= VIEWPORT_HEIGHT + (TILE_HEIGHT * 4)) { PlayerDie(); return; } for (int i = 0; i < TilesSolid.Count; i++) { var tile = TilesSolid[i]; if (player.WillIntersect(tile)) { var oldVelX = player.velocity.X; player.velocity.X = 0; if (player.velocity.Y > 0) { if (player.WillIntersect(tile)) { player.position.Y = tile.Up - player.bounds.Y; player.velocity.Y = 0; playerState = PlayerState.OnGround; } } else if (player.velocity.Y < 0) { if (player.WillIntersect(tile)) { player.position.Y = tile.Down; player.velocity.Y = 0; } } player.velocity.X = oldVelX; var oldVelY = player.velocity.Y; player.velocity.Y = 0; if (player.velocity.X > 0) { if (player.WillIntersect(tile)) { player.position.X = tile.Left - player.bounds.X; player.velocity.X = 0; } } else if (player.velocity.X < 0) { if (player.WillIntersect(tile)) { player.position.X = tile.Right; player.velocity.X = 0; } } player.velocity.Y = oldVelY; } } for (int i = 0; i < TilesOneWayPlatform.Count; i++) { var tile = TilesOneWayPlatform[i]; if (player.WillIntersect(tile)) { var oldVelX = player.velocity.X; player.velocity.X = 0; if (player.velocity.Y > 0 && player.Down <= tile.Up) { if (player.WillIntersect(tile)) { player.position.Y = tile.Up - player.bounds.Y; player.velocity.Y = 0; playerState = PlayerState.OnGround; } } player.velocity.X = oldVelX; } } if (playerState == PlayerState.OnGround) { if (player.velocity.Y != 0) { playerState = PlayerState.InAir; } /*if (player.Intersects(goal)) { * level = (level + 1) % levelNames.Length; * ChangeLevel(level); * }*/ } if (player.Down <= 0) { level = (level + 1) % levelNames.Length; ChangeLevel(level); } player.position += player.velocity; player.velocity.X -= ApplyWind(); } else { deathTimer -= 1f; if (deathTimer <= 0) { ResetLevel(); } } }
public void StepOfShots() { while (DeltaShots != MainForm.sizeCell + 5) { for (int i = 0; i < ShotsKolobok.Count; i++) { Move(ShotsKolobok[i], DeltaShots); } for (int i = 0; i < ShotsTanks.Count; i++) { Move(ShotsTanks[i], DeltaShots); } for (int i = 0; i < Apples.Count; i++) { mapGraphics.DrawImage(Apples[i].Img, Apples[i].X * MainForm.sizeCell, Apples[i].Y * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); } for (int i = 0; i < Obstacles.Count; i++) { if (Obstacles[i].Ability == 1) { mapGraphics.DrawImage(Obstacles[i].Img, Obstacles[i].X * MainForm.sizeCell, Obstacles[i].Y * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); } } DeltaShots += 5; map.Image = backgroundMap; return; } DeltaShots = 5; for (int j = 0; j < ShotsTanks.Count; j++) { if (kolobok.Collide(ShotsTanks[j])) { gameOver = true; break; } } for (int i = 0; i < Tanks.Count; i++) { for (int j = 0; j < ShotsKolobok.Count; j++) { if (Tanks[i].Collide(ShotsKolobok[j])) { mapGraphics.FillRectangle(Brushes.Black, Tanks[i].PrevX * MainForm.sizeCell, Tanks[i].PrevY * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, ShotsKolobok[j].X * MainForm.sizeCell, ShotsKolobok[j].Y * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, Tanks[i].X * MainForm.sizeCell, Tanks[i].Y * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, ShotsKolobok[j].PrevX * MainForm.sizeCell, ShotsKolobok[j].PrevY * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); Tanks.RemoveAt(i); ShotsKolobok.RemoveAt(j); i--; j--; break; } } } for (int i = 0; i < ShotsKolobok.Count; i++) { ShotsKolobok[i].Move(); var index = ShotsKolobok[i].InteractWithFixedObjects(Obstacles); if (index > 0) { mapGraphics.FillRectangle(Brushes.Black, ShotsKolobok[i].PrevX * MainForm.sizeCell, ShotsKolobok[i].PrevY * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, Obstacles[index].X * MainForm.sizeCell, Obstacles[index].Y * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); Obstacles.RemoveAt(index); ShotsKolobok.RemoveAt(i); } } for (int i = 0; i < ShotsTanks.Count; i++) { ShotsTanks[i].Move(); /*if (ShotsTanks[i].CollideWithFixedObjects(Obstacles)) * { * mapGraphics.FillRectangle(Brushes.Black, ShotsTanks[i].PrevX * MainForm.sizeCell, ShotsTanks[i].PrevY * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); * ShotsTanks.RemoveAt(i); * i--; * continue; * }*/ var index = ShotsTanks[i].InteractWithFixedObjects(Obstacles); if (index > 0) { mapGraphics.FillRectangle(Brushes.Black, ShotsTanks[i].PrevX * MainForm.sizeCell, ShotsTanks[i].PrevY * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, Obstacles[index].X * MainForm.sizeCell, Obstacles[index].Y * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); Obstacles.RemoveAt(index); ShotsTanks.RemoveAt(i); i--; continue; } if (kolobok.Collide(ShotsTanks[i])) { gameOver = true; break; } } for (int i = 0; i < Tanks.Count; i++) { for (int j = 0; j < ShotsKolobok.Count; j++) { if (Tanks[i].Collide(ShotsKolobok[j])) { mapGraphics.FillRectangle(Brushes.Black, Tanks[i].PrevX * MainForm.sizeCell, Tanks[i].PrevY * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, ShotsKolobok[j].X * MainForm.sizeCell, ShotsKolobok[j].Y * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, Tanks[i].X * MainForm.sizeCell, Tanks[i].Y * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, ShotsKolobok[j].PrevX * MainForm.sizeCell, ShotsKolobok[j].PrevY * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); Tanks.RemoveAt(i); ShotsKolobok.RemoveAt(j); i--; j--; break; } } } for (int i = 0; i < Apples.Count; i++) { if (Apples[i].Collide(kolobok)) { Apples.RemoveAt(i); Score++; break; } mapGraphics.DrawImage(Apples[i].Img, Apples[i].X * MainForm.sizeCell, Apples[i].Y * MainForm.sizeCell, MainForm.sizeCell, MainForm.sizeCell); } RespOfApples(); DeltaShots += 5; map.Image = backgroundMap; }
public void StepOfShots() { while (DeltaShots != TanksForm.sizeCell + 5) { for (int i = 0; i < ShotsKolobok.Count; i++) { Move(ShotsKolobok[i], DeltaShots); } for (int i = 0; i < ShotsTanks.Count; i++) { Move(ShotsTanks[i], DeltaShots); } for (int i = 0; i < Apples.Count; i++) { mapGraphics.DrawImage(Apples[i].Img, Apples[i].X * TanksForm.sizeCell, Apples[i].Y * TanksForm.sizeCell, TanksForm.sizeCell, TanksForm.sizeCell); } DeltaShots += 5; map.Image = backgroundMap; return; } DeltaShots = 5; for (int j = 0; j < ShotsTanks.Count; j++) { if (kolobok.CollidesWith(ShotsTanks[j])) { gameOver = true; break; } } for (int i = 0; i < Tanks.Count; i++) { for (int j = 0; j < ShotsKolobok.Count; j++) { if (Tanks[i].CollidesWith(ShotsKolobok[j])) { mapGraphics.FillRectangle(Brushes.Black, Tanks[i].OldX * TanksForm.sizeCell, Tanks[i].OldY * TanksForm.sizeCell, TanksForm.sizeCell, TanksForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, ShotsKolobok[j].X * TanksForm.sizeCell, ShotsKolobok[j].Y * TanksForm.sizeCell, TanksForm.sizeCell, TanksForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, Tanks[i].X * TanksForm.sizeCell, Tanks[i].Y * TanksForm.sizeCell, TanksForm.sizeCell, TanksForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, ShotsKolobok[j].OldX * TanksForm.sizeCell, ShotsKolobok[j].OldY * TanksForm.sizeCell, TanksForm.sizeCell, TanksForm.sizeCell); Tanks.RemoveAt(i); ShotsKolobok.RemoveAt(j); i--; j--; break; } } } for (int i = 0; i < ShotsKolobok.Count; i++) { ShotsKolobok[i].Move(); if (ShotsKolobok[i].CollidesWithWalls(Walls)) { mapGraphics.FillRectangle(Brushes.Black, ShotsKolobok[i].OldX * TanksForm.sizeCell, ShotsKolobok[i].OldY * TanksForm.sizeCell, TanksForm.sizeCell, TanksForm.sizeCell); ShotsKolobok.RemoveAt(i); i--; } } for (int i = 0; i < ShotsTanks.Count; i++) { ShotsTanks[i].Move(); if (ShotsTanks[i].CollidesWithWalls(Walls)) { mapGraphics.FillRectangle(Brushes.Black, ShotsTanks[i].OldX * TanksForm.sizeCell, ShotsTanks[i].OldY * TanksForm.sizeCell, TanksForm.sizeCell, TanksForm.sizeCell); ShotsTanks.RemoveAt(i); i--; continue; } if (kolobok.CollidesWith(ShotsTanks[i])) { gameOver = true; break; } } for (int i = 0; i < Tanks.Count; i++) { for (int j = 0; j < ShotsKolobok.Count; j++) { if (Tanks[i].CollidesWith(ShotsKolobok[j])) { mapGraphics.FillRectangle(Brushes.Black, Tanks[i].OldX * TanksForm.sizeCell, Tanks[i].OldY * TanksForm.sizeCell, TanksForm.sizeCell, TanksForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, ShotsKolobok[j].X * TanksForm.sizeCell, ShotsKolobok[j].Y * TanksForm.sizeCell, TanksForm.sizeCell, TanksForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, Tanks[i].X * TanksForm.sizeCell, Tanks[i].Y * TanksForm.sizeCell, TanksForm.sizeCell, TanksForm.sizeCell); mapGraphics.FillRectangle(Brushes.Black, ShotsKolobok[j].OldX * TanksForm.sizeCell, ShotsKolobok[j].OldY * TanksForm.sizeCell, TanksForm.sizeCell, TanksForm.sizeCell); Tanks.RemoveAt(i); ShotsKolobok.RemoveAt(j); i--; j--; break; } } } for (int i = 0; i < Apples.Count; i++) { if (Apples[i].CollidesWith(kolobok)) { Apples.RemoveAt(i); Score++; break; } mapGraphics.DrawImage(Apples[i].Img, Apples[i].X * TanksForm.sizeCell, Apples[i].Y * TanksForm.sizeCell, TanksForm.sizeCell, TanksForm.sizeCell); } SpawnApples(); DeltaShots += 5; map.Image = backgroundMap; }