-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlappyGame.cs
172 lines (140 loc) · 4.88 KB
/
FlappyGame.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
using FlappyXna.Objects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Timers;
namespace FlappyXna
{
public class FlappyGame : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Panorama panorama;
Bird bird;
Ground ground;
List<Pipes> pipes;
PhysicsEngine physics;
TweenEngine tweener;
KeyboardState lastKeyboardState;
bool gameOver;
Random rnd;
int width = 0;
Timer pipeGenerator;
public FlappyGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
gameOver = true;
rnd = new Random(1223);
pipeGenerator = new Timer(1250);
pipeGenerator.Elapsed += delegate { GeneratePipes(); };
}
protected override void Initialize()
{
// Need to change this in Initialize to work on linux
graphics.PreferredBackBufferHeight = 505;
graphics.PreferredBackBufferWidth = 288;
graphics.ApplyChanges();
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
this.Services.AddService<SpriteBatch>(spriteBatch);
physics = new PhysicsEngine(this);
this.Components.Add(physics);
tweener = new TweenEngine();
this.Services.AddService(tweener);
panorama = new Panorama(this);
this.Components.Add(panorama);
bird = new Bird(this);
this.Components.Add(bird);
physics.AddBody(bird);
ground = new Ground(this);
this.Components.Add(ground);
pipes = new List<Pipes>();
width = Window.ClientBounds.Width;
base.Initialize();
}
protected override void LoadContent()
{
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
private void StartGame()
{
bird.Reset();
pipeGenerator.Start();
}
protected override void Update(GameTime gameTime)
{
tweener.Update(gameTime);
physics.CheckCollision(bird, ground, OnBirdGroundCollided);
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
var currentKeyboardState = Keyboard.GetState();
if (currentKeyboardState.IsKeyDown(Keys.Space) && lastKeyboardState.IsKeyUp(Keys.Space))
{
if (gameOver)
{
StartGame();
gameOver = false;
ground.IsAlive = true;
panorama.IsAlive = true;
pipes.Clear();
pipeGenerator.Start();
}
bird.Flap();
}
pipes.ForEach(p =>
{
physics.CheckCollision(bird, p.TopPipe, OnBirdPipesCollided);
physics.CheckCollision(bird, p.BottomPipe, OnBirdPipesCollided);
p.Update(gameTime);
});
base.Update(gameTime);
lastKeyboardState = currentKeyboardState;
}
private void GeneratePipes()
{
var newPipes = pipes.Where(p => !p.IsAlive).FirstOrDefault();
if (newPipes == null)
{
newPipes = new Pipes(this);
pipes.Add(newPipes);
}
var pipesY = (int)(rnd.NextDouble() * 200 - 100);
newPipes.Reset(width + 20, pipesY);
}
private void OnBirdPipesCollided(ICollidable bird, ICollidable pipe)
{
bird.OnCollideWith(pipe);
CheckGameOver();
}
private void OnBirdGroundCollided(ICollidable bird, ICollidable ground)
{
bird.OnCollideWith(ground);
CheckGameOver();
}
private void CheckGameOver() {
if (!gameOver)
{
gameOver = true;
ground.IsAlive = false;
panorama.IsAlive = false;
pipes.ForEach(p => p.IsAlive = false);
pipeGenerator.Stop();
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.FromNonPremultiplied(7, 140, 254, 255));
spriteBatch.Begin(SpriteSortMode.FrontToBack, null, SamplerState.LinearWrap, null, null);
base.Draw(gameTime);
pipes.ForEach(p => p.Draw(gameTime));
spriteBatch.End();
}
}
}