/
Level3.cs
222 lines (183 loc) · 4.79 KB
/
Level3.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
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
namespace JumpGame
{
public class Level3 : ILevel
{
private List<Block> blocks = new List<Block>();
private Player player;
private Jump game;
private Random random = new Random();
private int points;
private int speed = 150;
private int leftoverSpeed = 0;
private int elapsedTime = 0;
private int speedUp = 2000;
private int yVariance = 40; //220
private int xVariance = 40; //220
private int xVarianceStep = 10;
private int yVarianceStep = 10;
private int widthMax = 300;
private int widthVariance = 10; //50 - 268
private int widthVarianceStep = 10;
private SoundEffect music;
private SoundEffectInstance musicInstance;
private bool gameOver;
private HighScoresWindow highScore;
// private int points;
private Text scoreText;
public Level3(Jump game)
{
this.game = game;
}
public void Init()
{
speed = 5;//1000 / speed;
player = new Player(game, this, new Rectangle(512, 0, 32, 32));
game.Components.Add(player);
Block floor = new Block(game, this, new Rectangle(0, 736, 1024, 32), Color.SpringGreen, 0, 0);//740
blocks.Add(floor);
game.Components.Add(floor);
CheckNextBlockSpawn();
this.highScore = new HighScoresWindow(game);
game.Components.Add(this.highScore);
this.scoreText = new Text(game, "Score: " + this.points, new Vector2(0,0), Color.Blue);
game.Components.Add(this.scoreText);
music = this.game.Content.Load<SoundEffect>("home_at_last");
musicInstance = music.CreateInstance();
musicInstance.IsLooped = true;
musicInstance.Play();
points = 0;
}
public void Tick()
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
{
if (!this.highScore.Active)
{
Environment.Exit(0);
}
}
if (gameOver)
{
return;
}
this.scoreText.Content = "Score: " + this.points;
elapsedTime += Jump.Time.ElapsedGameTime.Milliseconds;
int totalTime = leftoverSpeed + Jump.Time.ElapsedGameTime.Milliseconds;
int movePixels = -(totalTime / speed);
leftoverSpeed = totalTime % speed;
bool movePlayer = false;
if (movePixels < -3)
{
points++;
}
foreach (Block block in blocks)
{
block.rect = new Rectangle (block.rect.X + movePixels, block.rect.Y, block.rect.Width, block.rect.Height);
if (!movePlayer && block.rect.Intersects(player.rect))
{
movePlayer = true;
player.rect = new Rectangle(player.rect.X + movePixels, player.rect.Y, player.rect.Width, player.rect.Height);
}
}
if (elapsedTime > speedUp)
{
elapsedTime = elapsedTime % speedUp;
NextStep();
Console.WriteLine("NEXT STEP!");
}
CheckNextBlockSpawn();
if (player.rect.Y > 1024 || (player.rect.X + player.rect.Width) < 0)
{
if (!this.gameOver)
{
player.Enabled = false;
this.gameOver = true;
this.highScore.SetStateInput(this.points, 2);
while(blocks.Count > 0)
{
game.Components.Remove(blocks [0]);
blocks.RemoveAt(0);
}
this.game.Components.Remove(this.player);
this.game.Components.Remove(this.scoreText);
this.musicInstance.Stop();
}
}
}
public void End()
{
this.game.Components.Remove(this.player);
}
public void AddPoints(int points)
{
this.points += points;
}
public List<Block> GetBlocks()
{
return blocks;
}
private void SpawnNextBlock()
{
Block lastBlock = blocks[blocks.Count - 1];
int nextWidth = random.Next(widthMax - widthVariance, widthMax);
Rectangle tRect = new Rectangle (XRange(lastBlock.rect.X + lastBlock.rect.Width), YRange(lastBlock.rect.Y), nextWidth, 32);//YRange(lastBlock.rect.Y)
Block block = new Block (game, this, tRect, Color.SpringGreen, 0);
blocks.Add(block);
game.Components.Add(block);
}
private int YRange(int lastY)
{
int newY;
if (yVariance < 300)
{
newY = random.Next(lastY - yVariance, lastY + yVariance);
}
else
{
newY = random.Next(lastY - 300, lastY + 300);
}
if (newY < 64)
newY = 64;
else if (newY > 736)
newY = 736;
return newY;
}
private int XRange(int lastX)
{
int newX;
if (xVariance < 300)
{
newX = lastX + random.Next(xVariance/2, xVariance);
}
else
{
newX = lastX + random.Next(xVariance/2, 300);
}
return newX;
}
private void NextStep()
{
yVariance += yVarianceStep; //220
if (yVariance > 300)
yVariance = 300;
xVariance += xVarianceStep; //220
if (xVariance > 300)
xVariance = 300;
widthVariance += widthVarianceStep;//50; //50 - 268
if (widthVariance > 268)
widthVariance = 268;
}
private void CheckNextBlockSpawn()
{
while (blocks [blocks.Count - 1].rect.X < 1024)
{
SpawnNextBlock();
}
}
}
}