restarting added
This commit is contained in:
33
Program.cs
33
Program.cs
@@ -10,13 +10,15 @@ internal class Program
|
|||||||
uint gameWidth = 900;
|
uint gameWidth = 900;
|
||||||
uint gameHeight = 504;
|
uint gameHeight = 504;
|
||||||
float gravity = 800f; // px/sec^2
|
float gravity = 800f; // px/sec^2
|
||||||
float jumpForce = -300f; // px/sec
|
float jumpForce = -300f;
|
||||||
float scrollSpeed = 150f; // px/sec
|
float scrollSpeedInitial = 150f;// px/sec
|
||||||
|
float scrollSpeed = scrollSpeedInitial; // px/sec
|
||||||
float scrollDecay = 500f; // px/sec^2
|
float scrollDecay = 500f; // px/sec^2
|
||||||
float maxUpAngle = -30f; // tilt up max
|
float maxUpAngle = -30f; // tilt up max
|
||||||
float maxDownAngle = 60f; // tilt down max
|
float maxDownAngle = 60f; // tilt down max
|
||||||
float maxVelocity = 500f; // velocity that corresponds to max tilt
|
float maxVelocity = 500f; // velocity that corresponds to max tilt
|
||||||
float backgroundSpeed = 0.75f;
|
float backgroundSpeed = 0.75f;
|
||||||
|
int points = 0;
|
||||||
|
|
||||||
// Pillars
|
// Pillars
|
||||||
List<Pillar> pillars = new List<Pillar>();
|
List<Pillar> pillars = new List<Pillar>();
|
||||||
@@ -99,6 +101,17 @@ internal class Program
|
|||||||
// Clock for deltaTime
|
// Clock for deltaTime
|
||||||
Clock clock = new Clock();
|
Clock clock = new Clock();
|
||||||
|
|
||||||
|
void Restart()
|
||||||
|
{
|
||||||
|
birdVelocity = 0;
|
||||||
|
pillars.Clear();
|
||||||
|
bird.Position = new Vector2f(100, gameHeight / 2f);
|
||||||
|
backgroundOffset = 0;
|
||||||
|
alive = true;
|
||||||
|
scrollSpeed = scrollSpeedInitial;
|
||||||
|
points = 0;
|
||||||
|
}
|
||||||
|
|
||||||
while (window.IsOpen)
|
while (window.IsOpen)
|
||||||
{
|
{
|
||||||
float deltaTime = clock.Restart().AsSeconds();
|
float deltaTime = clock.Restart().AsSeconds();
|
||||||
@@ -140,6 +153,11 @@ internal class Program
|
|||||||
birdVelocity = jumpForce;
|
birdVelocity = jumpForce;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!alive && Keyboard.IsKeyPressed(Keyboard.Key.Space))
|
||||||
|
{
|
||||||
|
Restart();
|
||||||
|
}
|
||||||
|
|
||||||
// Update pillars
|
// Update pillars
|
||||||
pillarTimer += deltaTime;
|
pillarTimer += deltaTime;
|
||||||
if (pillarTimer > spawnInterval)
|
if (pillarTimer > spawnInterval)
|
||||||
@@ -177,11 +195,11 @@ internal class Program
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!alive)
|
if (!alive)
|
||||||
{
|
{
|
||||||
// Gradually reduce speed towards 0
|
// Gradually reduce speed towards 0
|
||||||
scrollSpeed -= scrollDecay * deltaTime;
|
scrollSpeed -= scrollDecay * deltaTime;
|
||||||
if (scrollSpeed < 0) scrollSpeed = 0;
|
if (scrollSpeed < 0) scrollSpeed = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scroll background
|
// Scroll background
|
||||||
backgroundOffset += (long)(scrollSpeed * deltaTime * backgroundSpeed);
|
backgroundOffset += (long)(scrollSpeed * deltaTime * backgroundSpeed);
|
||||||
@@ -209,3 +227,4 @@ internal class Program
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user