This repository was archived by the owner on Apr 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame1.cs.txt
More file actions
317 lines (260 loc) · 11.3 KB
/
Game1.cs.txt
File metadata and controls
317 lines (260 loc) · 11.3 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Commando
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Player player;
public List<Enemy> enemies = new List<Enemy>();
public List<Rectangle> spawnPointRects = new List<Rectangle>();
public Color backColor = Color.SpringGreen;
public SpriteFont spriteFont;
public SpriteFont spriteFont2;
public int money = 0;
int timer = 0;
int btimer = 200;
int htimer = 0;
int score = 0;
public int gameTime = 0;
public int hiScore = 0;
public bool startDone = false;
MouseState oldState = Mouse.GetState();
Texture2D crossHairs;
StartInterface startInterface;
Random xRandom = new Random();
Random yRandom = new Random();
Random theRandom = new Random();
Vector2 newSpawnPoint;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
player = new Player(Content.Load<Texture2D>("commandoPlayer"), this);
crossHairs = Content.Load<Texture2D>("CrossHairs");
spriteFont = Content.Load<SpriteFont>("SpriteFont1");
spriteFont2 = Content.Load<SpriteFont>("SpriteFont2");
startInterface = new StartInterface(this);
Window.AllowUserResizing = true;
//graphics.ToggleFullScreen();
spawnEnemy(new Vector2(20, 30));
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
graphics.ToggleFullScreen();
player.Update();
timer += 1;
this.gameTime = (int)gameTime.TotalGameTime.TotalSeconds;
if (!startDone)
{
startInterface.Update();
this.startDone = startInterface.startDone;
startDone = true;
}
else if (startDone)
{
if (backColor.R > 0)
backColor.R--;
if (backColor.G > 0)
backColor.G--;
if (backColor.B > 0)
backColor.B--;
if (backColor.A < 255)
backColor.A++;
if (htimer > 0)
htimer--;
if (player.lifePoints != 0)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (btimer > 20)
if (btimer % 500 == 0)
btimer -= 5;
if (timer % btimer == 0)
{
spawnEnemy(new Vector2(20, 30));
if (timer > 1000)
spawnBlueEnemy(new Vector2(GraphicsDevice.Viewport.Width - 80, 30));
if (timer > 2000)
spawnBlueEnemy(new Vector2(20, GraphicsDevice.Viewport.Height - 30));
if (timer > 3000)
spawnEnemy(new Vector2(GraphicsDevice.Viewport.Width - 80, GraphicsDevice.Viewport.Height - 30));
//4000 and 10 is default
if (timer > 1000 && timer % 5 == 0)
{
newSpawnPoint = new Vector2(
xRandom.Next(0, GraphicsDevice.Viewport.Width - 60),
yRandom.Next(0, GraphicsDevice.Viewport.Height - 60));
if (theRandom.Next(0, 100) > 30)
spawnRedEnemy(newSpawnPoint);
else
spawnGreenEnemy(newSpawnPoint);
}
}
foreach (Enemy enemy in enemies)
{
enemy.UpdateEnemy(ref player);
foreach (Bullet b in player.bullets)
{
if (enemy.enemyRectangle.Intersects(b.bulletRectangle))
{
//removes both the bullet and the enemy
enemy.health--;
b.isVisible = false;
if (enemy.health == 0)
{
enemy.isVisible = false;
if (enemy.alienColor == Color.White)
score += 10;
if (enemy.alienColor == Color.Blue)
score += 30;
if (enemy.alienColor == Color.Red)
score += 50;
if (enemy.alienColor == Color.Green)
score += 200;
}
}
}
foreach (Bullet b in enemy.enemyBullets)
{
b.position += b.velocity;
if (player.playerRect.Intersects(b.bulletRectangle))
{
b.isVisible = false;
player.lifePoints -= 1;
}
//new code
if (enemy.enemyRectangle.Intersects(b.bulletRectangle))
{
enemy.health--;
}
}
if (enemy.enemyRectangle.Intersects(player.playerRect))
{
enemy.isVisible = false;
player.lifePoints -= 1;
}
}
for (int i = 0; i == enemies.Count; i++)
{
if (!enemies[i].isVisible)
{
enemies.RemoveAt(i);
i--;
}
}
}
}
if (player.lifePoints == 0)
{
if (backColor.A < 250)
backColor.A += 5;
if (backColor.R < 250)
backColor.R += 5;
if (backColor.B > 5)
backColor.B -= 5;
if (backColor.G > 5)
backColor.G -= 5;
if (hiScore < score)
hiScore = score;
foreach (Enemy enemy in enemies)
{
//enemy.alienColor = backColor;
money += score;
}
if (Keyboard.GetState().IsKeyDown(Keys.Enter) || Keyboard.GetState().IsKeyDown(Keys.Space))
{
enemies = new List<Enemy>();
backColor = Color.SpringGreen;
spawnEnemy(new Vector2(20, 30));
player.lifePoints = 10;
timer = 0;
btimer = 200;
score = 0;
startInterface.startDone = false;
startDone = false;
}
}
base.Update(gameTime);
}
public void spawnEnemy(Vector2 spawnerPos)
{
Enemy enemy = new Enemy(Content.Load<Texture2D>("commandoEnemy"),
Content.Load<Texture2D>("commandoEnemySpawner"), Content.Load<Texture2D>("commandoSquareBullet"), spawnerPos, Color.White, this);
enemies.Add(enemy);
}
public void spawnGreenEnemy(Vector2 spawnerPos)
{
Enemy enemy = new Enemy(Content.Load<Texture2D>("commandoEnemy"),
Content.Load<Texture2D>("commandoEnemySpawner"), Content.Load<Texture2D>("commandoSquareBullet"), spawnerPos, Color.Green, this);
enemies.Add(enemy);
}
public void spawnBlueEnemy(Vector2 spawnerPos)
{
Enemy enemy = new Enemy(Content.Load<Texture2D>("commandoEnemy"),
Content.Load<Texture2D>("commandoEnemySpawner"), Content.Load<Texture2D>("commandoSquareBullet"), spawnerPos, Color.Blue, this);
enemies.Add(enemy);
}
public void spawnRedEnemy(Vector2 spawnerPos)
{
Enemy enemy = new Enemy(Content.Load<Texture2D>("commandoEnemy"),
Content.Load<Texture2D>("commandoEnemySpawner"), Content.Load<Texture2D>("commandoSquareBullet"), spawnerPos, Color.Red, this);
enemies.Add(enemy);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(backColor);
//spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Begin();
spriteBatch.DrawString(spriteFont2,
"Score: " + score.ToString(), new Vector2(GraphicsDevice.Viewport.Width / 2 - 20, 0), Color.Red);
spriteBatch.DrawString(spriteFont2,
"Lives: " + player.lifePoints.ToString(), new Vector2(GraphicsDevice.Viewport.Width / 2 - 20, 15), Color.Red);
spriteBatch.Draw(crossHairs, new Vector2(
Mouse.GetState().X - crossHairs.Bounds.Width / 2,
Mouse.GetState().Y - crossHairs.Bounds.Height / 2), null,
Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 1);
spriteBatch.End();
foreach (Enemy E in enemies)
E.Draw(spriteBatch);
player.Draw(spriteBatch);
spriteBatch.Begin();
/*if (backColor != Color.Black && startDone)
{
spriteBatch.DrawString(spriteFont, "GAME OVER!", new Vector2(GraphicsDevice.Viewport.Width / 2 - 30,
GraphicsDevice.Viewport.Height / 2), Color.Black);
spriteBatch.DrawString(spriteFont, "score:" + score, new Vector2(GraphicsDevice.Viewport.Width / 2 - 30,
GraphicsDevice.Viewport.Height / 2 + 50), Color.Black);
spriteBatch.DrawString(spriteFont, "high-score:" + hiScore, new Vector2(GraphicsDevice.Viewport.Width / 2 - 30,
GraphicsDevice.Viewport.Height / 2 + 100), Color.Black);
}*/
spriteBatch.End();
if (!startDone)
{
startInterface.Draw(spriteBatch, spriteFont, GraphicsDevice, crossHairs);
}
base.Draw(gameTime);
}
}
}