-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cs
212 lines (192 loc) · 5.87 KB
/
Game.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Drawing;
namespace Asteroids
{
/// <summary>
/// Summary description for CGame.
/// </summary>
public class Game : CommonOps
{
private Ship ship;
private Bullet[] shipBullets;
private AsteroidBelt asteroids;
private Explosions explosions;
private bool inProcess;
private int iLevel;
private bool paused;
const int PAUSE_INTERVAL = (int)(FPS);
int iPauseTimer;
public Game()
{
iLevel = 4; // start with 4 asteroids
inProcess = true;
ship = new Ship(true); // new game - we know ship is alive
shipBullets = new Bullet[4];
for (int i = 0; i<4; i++)
shipBullets[i] = new Bullet();
asteroids = new AsteroidBelt(iLevel);
explosions = new Explosions();
paused = false;
iPauseTimer = PAUSE_INTERVAL;
}
public bool Done()
{
return (!inProcess);
}
public void Thrust(bool bThrustOn)
{
if (!paused && ship.IsAlive())
{
ship.DecayThrust();
if (bThrustOn)
ship.Thrust();
}
}
public void Left()
{
if (!paused && ship.IsAlive())
ship.RotateLeft();
}
public void Right()
{
if (!paused && ship.IsAlive())
ship.RotateRight();
}
public void Hyperspace()
{
if (!paused && ship.IsAlive())
if (!ship.Hyperspace())
ExplodeShip();
}
public void Shoot()
{
if (!paused && ship.IsAlive())
{
foreach (Bullet bullet in shipBullets)
{
if (bullet.Available())
{
bullet.Shoot(ship.GetCurrLoc(), ship.GetRadians(), ship.GetVelocityX(), ship.GetVelocityY());
PlaySound("Fire.WAV");
return;
}
}
}
}
public void Pause()
{
iPauseTimer = PAUSE_INTERVAL;
paused = !paused;
}
private bool CheckPointInAsteroid(Point ptCheck, ref Score score)
{
int pointValue = asteroids.CheckPointCollisions(ptCheck);
if (pointValue > 0)
{
score.AddScore(pointValue);
return true;
}
return false;
}
private void ExplodeShip()
{
Point ptCheck = new Point(0);
PlaySound("EXPLODE1.WAV");
PlaySound("EXPLODE2.WAV");
PlaySound("EXPLODE3.WAV");
foreach (Point ptExp in ship.pointsTransformed)
{
ship.Explode();
ptCheck.X = ptExp.X + ship.GetCurrLoc().X;
ptCheck.Y = ptExp.Y + ship.GetCurrLoc().Y;
explosions.AddExplosion(ptCheck, 2);
}
}
public void DrawScreen(ScreenCanvas sc, int iPictX, int iPictY, ref Score score)
{
Point ptCheck = new Point(0);
if (paused)
{
// Pause flashes on and off
if (iPauseTimer > PAUSE_INTERVAL/2)
{
TextDraw.DrawText(sc, "PAUSE", TextDraw.Justify.CENTER,
iMaxY / 3, 200, 400, iPictX, iPictY);
}
if (--iPauseTimer < 0)
iPauseTimer = PAUSE_INTERVAL;
}
else // Do all game processing if game is not paused
{
// If no ship displaying, after explosions are done
// get a new one - or end the game
if (!ship.IsAlive() &&
(explosions.Count() == 0))
{
if (!score.HasReserveShips())
{
// Game over
inProcess = false;
}
else
{
if (asteroids.IsCenterSafe())
{
score.GetNewShip();
ship = new Ship(true);
}
}
}
// Create a new asteroid belt if
// no explosions and no asteroids
if ((explosions.Count() == 0) &&
(asteroids.Count() == 0))
{
asteroids = new AsteroidBelt(++iLevel);
}
// Move all objects
ship.Move();
foreach (Bullet bullet in shipBullets)
{
bullet.Move();
}
asteroids.Move();
explosions.Move();
// Check bullets for collisions
foreach (Bullet bullet in shipBullets)
{
if (bullet.AcquireLoc(ref ptCheck) &&
CheckPointInAsteroid(ptCheck, ref score))
{
explosions.AddExplosion(ptCheck);
bullet.Disable();
}
}
// Check ship for collisions
if (ship.IsAlive())
{
foreach (Point ptInShip in ship.pointsTransformed)
{
ptCheck.X = ptInShip.X + ship.GetCurrLoc().X;
ptCheck.Y = ptInShip.Y + ship.GetCurrLoc().Y;
if (CheckPointInAsteroid(ptCheck, ref score))
{
ExplodeShip();
break;
}
}
}
}
// Draw all objects
ship.Draw(sc, iPictX, iPictY);
foreach (Bullet bullet in shipBullets)
{
bullet.Draw(sc, iPictX, iPictY);
}
asteroids.Draw(sc, iPictX, iPictY);
explosions.Draw(sc, iPictX, iPictY);
// Draw the score
score.Draw(sc, iPictX, iPictY);
}
}
}