-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotDodge.cs
118 lines (100 loc) · 3.07 KB
/
RobotDodge.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using SplashKitSDK;
using System.Collections.Generic;
namespace RobotDodge
{
public class RobotDodge
{
private Player _player;
private Window _gameWindow;
private List<Robot> _robots = new List<Robot>();
public int Lives //Lives property
{
get { return _player.Lives; }
}
public bool Quit
{
get
{
return _player.Quit;
}
}
public RobotDodge(Window gameWindow)
{
_gameWindow = gameWindow;
_player = new Player(gameWindow);
}
public void HandleInput()
{
_player.HandleInput();
_player.StayOnWindow(_gameWindow);
}
public void Draw()
{
_gameWindow.Clear(Color.White);
foreach (Robot robot in _robots)
{
robot.Draw();
}
_player.Draw();
_gameWindow.Refresh();
}
public Robot RandomRobot()
{
int randomChoice = SplashKit.Rnd(3);
switch (randomChoice)
{
case 0:
return new Boxy(_gameWindow, _player);
case 1:
return new Roundy(_gameWindow, _player);
case 2:
return new Swingy(_gameWindow, _player);
default:
return new Boxy(_gameWindow, _player); // Default case
}
}
public void Update()
{
foreach (Robot robot in _robots)
{
robot.Update();
}
_player.UpdateBullets();
if (SplashKit.Rnd() < 0.002)//Change the speed of robot for recorsding purpose
{
_robots.Add(RandomRobot());
}
CheckCollisions();
}
private void CheckCollisions()
{
List<Robot> _removedRobots = new List<Robot>();
List<Bullet> _removedBullets = new List<Bullet>();
foreach (Robot robot in _robots)
{
if (_player.CollideWith(robot) || robot.IsOffscreen(_gameWindow))
{
_removedRobots.Add(robot);
_player.Lives--; //Adding a logic for decreasing live when player collides with a robot
}
foreach (Bullet bullet in _player.Bullets) //Circles Intersection detect and remove if Bullet and Robot collide
{
if (SplashKit.CirclesIntersect(bullet.CollisionCircle, robot.CollisionCircle))
{
_removedRobots.Add(robot);
_removedBullets.Add(bullet);
}
}
}
foreach (Robot robot in _removedRobots)
{
_robots.Remove(robot);
}
foreach (Bullet bullet in _removedBullets)
{
_player.Bullets.Remove(bullet);
}
}
}
}