-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameCode.cs
157 lines (146 loc) · 3.57 KB
/
GameCode.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FlappyBirdGame
{
public partial class GameCode : Form
{
private int PipeSpeed { get; set; }
private int Gravity { get; set; }
private int ScoreValue { get; set; }
private string ScoreString { get; set; }
private SoundPlayer music;
private SoundPlayer quack;
private IPipe topPipeObject;
private IPipe bottomPipeObject;
private IBird birdObject;
public GameCode()
{
PipeSpeed = 8;
Gravity = 5;
ScoreValue = 0;
ScoreString = "Score: ";
quack = new SoundPlayer(Properties.Resources.Quack_Sound_Effect);
PlayMusic();
InitializeComponent();
Restart.Enabled = false;
topPipeObject = new Pipe(TopPipe.Left, TopPipe.Right, TopPipe.Bottom);
bottomPipeObject = new Pipe(BottomPipe.Left, BottomPipe.Right, BottomPipe.Top);
birdObject = new Bird(Bird.Top, Bird.Bottom, Bird.Left, Bird.Right);
}
private void PlayMusic()
{
music = new SoundPlayer(Properties.Resources.Dead_Or_Alive___You_Spin_Me_Round__MIDI_);
music.Play();
}
private void PlacePipes()
{
MovePipes(GameCode.ActiveForm.Width);
TopPipe.Top = topPipeObject.GetPipeYPlacement();
topPipeObject.PipeY += TopPipe.Height;
BottomPipe.Top = TopPipe.Bottom + 100;
bottomPipeObject.PipeY = BottomPipe.Top;
}
private void MovePipes (int newLocale)
{
if (newLocale == GameCode.ActiveForm.Width)
{
TopPipe.Left = GameCode.ActiveForm.Width;
BottomPipe.Left = GameCode.ActiveForm.Width;
}
else
{
BottomPipe.Left -= PipeSpeed;
TopPipe.Left -= PipeSpeed;
}
UpdatePipes();
}
private void UpdatePipes()
{
topPipeObject.PipeLeft = TopPipe.Left;
topPipeObject.PipeRight = TopPipe.Right;
bottomPipeObject.PipeLeft = BottomPipe.Left;
bottomPipeObject.PipeRight = BottomPipe.Right;
}
private void UpdateBird()
{
Bird.Top += Gravity;
UpdateBirdHelper();
GameOverCheck();
}
private void UpdateBirdHelper()
{
if (birdObject.BirdBottom >= Ground.Top - 5)
{
Bird.Top -= 5;
}
birdObject.BirdTop = Bird.Top;
birdObject.BirdBottom = Bird.Bottom;
}
private void GameOverCheck()
{
if (IsGameOver())
{
GameTimer.Stop();
music.Dispose();
quack.Play();
ScoreString = "Final Score: ";
ScoreText.BringToFront();
Restart.Enabled = true;
}
}
private bool IsGameOver()
{
return ((birdObject.BirdTop <= topPipeObject.PipeY &&
birdObject.BirdRight >= topPipeObject.PipeLeft &&
birdObject.BirdLeft <= topPipeObject.PipeRight) ||
(birdObject.BirdBottom >= bottomPipeObject.PipeY &&
birdObject.BirdRight >= bottomPipeObject.PipeLeft &&
birdObject.BirdLeft <= bottomPipeObject.PipeRight));
}
private void GameTimerEvent(object sender, EventArgs e)
{
UpdateBird();
ScoreText.Text = ScoreString + (ScoreValue++);
MovePipes(PipeSpeed);
if (BottomPipe.Left + BottomPipe.Width < 0)
{
PlacePipes();
}
if (ScoreValue % 500 == 0)
{
PipeSpeed += 1;
}
}
private void GameKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
Gravity = -5;
}
}
private void GameKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
Gravity = 5;
}
}
private void MusicTimerEvent(object sender, EventArgs e)
{
music.Stop();
music.Play();
}
private void Restart_Click(object sender, EventArgs e)
{
Application.Restart();
}
}
}