-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
305 lines (247 loc) · 8.82 KB
/
Form1.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Hoppespill
{
public partial class Bouncy : Form
{
private Timer beveg = new Timer(); //made timer for movement of player
public Timer obstMove = new Timer(); //made timer for movement of obstacle
public Timer obstSpawn = new Timer(); //made timer for spawning of obstacles
private List<Panel> obstacles = new List<Panel>(); //a list for all the obstacles
public Bouncy()
{
InitializeComponent();
login login = new login();
login.ShowDialog();
this.StartPosition = FormStartPosition.CenterScreen;
this.MaximizeBox = false;
beveg.Interval = 10;
obstSpawn.Interval = new Random().Next(1500,3000);
obstSpawn.Tick += ObstSpawn_Tick; //adds the tick event for the timer
obstSpawn.Start();
obstMove.Interval = 10;
obstMove.Tick += ObstMove_Tick; //adds the tick event for the timer
obstMove.Start();
beveg.Tick += beveg_event; //adds the tick event for the timer
this.FormBorderStyle = FormBorderStyle.FixedDialog;
user2.Text = login.user;
button1.Enabled = false;
}
//variables
int mvDY = 270; //y-value of vehicle in left lane (move down y)
int mvUY = 230; //y-value of vehicle in right lane (move up y)
int ps = 230; //y-value of vehicle (used for jumping)
bool jump = false;
int jumphgt;
bool laneU = true; //vehicle is in upper lane = true
int t = 0; //used for point-adding
string fileName = @".\brukere.txt"; //@"C:\Temp\brukere.txt";
public void ObstSpawn_Tick(object sender, EventArgs e) //Spawning obstacles
{
obstSpawn.Interval = new Random().Next(800, 1500);
int uod = new Random().Next(0, 100);
if (uod > 60)
{
addHind(mvUY);
}
else if (uod <40 )
{
addHind(mvDY);
}
else
{
addHind(mvDY);
addHind(mvUY);
}
}
public void addHind(int lanePosition) //adding obstacles
{
Panel hind = new Panel();
hind.Location = new System.Drawing.Point(this.Width-1, lanePosition);
hind.Name = "hind";
hind.Size = new System.Drawing.Size(35, 30);
hind.TabIndex = 1;
obstacles.Add(hind);
this.Controls.Add(hind);
hind.BringToFront();
car1.BringToFront();
}
private void ObstMove_Tick(object sender, EventArgs e) //Moving obstacles
{
for (int i = 0; i < obstacles.Count; i++)
{
Panel o = obstacles[i];
if(o.Right > 0 && o.Left < this.Width)
{
o.Left -= 5;
}
else
{
obstacles.Remove(o);
life.Text = Convert.ToString(t++); //adds point
}
}
chkCol();
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
private void beveg_event(object sender, EventArgs e) //moving the player
{
if(car1.Top > jumphgt && jump)
{
car1.Top -= 8;
}
else if(car1.Top < (laneU ? mvUY : mvDY))
{
car1.Top += 8;
jump = false;
}
else
{
beveg.Stop();
}
}
private void Bouncy_KeyDown(object sender, KeyEventArgs e) //vehicle jumping and lane-shifting
{
switch (e.KeyData)
{
case Keys.Space: //car jumps
{
if (car1.Top == mvDY || car1.Top == mvUY)
{
jump = true;
jumphgt = car1.Top - 160;
beveg.Start();
}
break;
}
case Keys.Down: //moving the car to the bottom lane
{
if (car1.Location.Y == mvUY)
{
car1.Top = mvDY;
ps = mvDY;
laneU = false;
}
break;
}
case Keys.Up: //moving the car to the upper lane
{
if (car1.Location.Y == mvDY)
{
car1.Top = mvUY;
ps = mvUY;
laneU = true;
}
break;
}
case Keys.R:
{
this.obstMove.Start();
this.obstSpawn.Start();
rudrevyen.Text = "";
t = 0;
obstacles.Clear();
for(int i = 0; i < this.Controls.Count; i++)
{
if(this.Controls[i] is Panel && this.Controls[i].Name != "bakgrunn" && this.Controls[i].Name != "car1" )
{
this.Controls.Remove(this.Controls[i]);
}
{
}
}
label1.Visible = false;
button1.Enabled = false;
button1.Visible = false;
break;
}
}
}
private void chkCol() //checking if the game is lost
{
for (int i = 0; i < obstacles.Count; i++)
{
if (collides(car1, obstacles[i]))
{
this.obstMove.Stop();
this.obstSpawn.Stop();
rudrevyen.Text = "GAME OVER";
this.Controls.Remove(obstacles[i]);
this.obstacles.Remove(obstacles[i]);
button1.Visible = true;
button1.Enabled = true;
highscoreAdd();
break;
}
}
}
private bool collides(Panel p1, Panel p2)
{
if (!laneU && p2.Location.Y == mvUY)
{ return false; }
else if (!laneU && p2.Location.Y != mvUY && p1.Right >= p2.Left && p1.Left <= p2.Right && p1.Bottom >= p2.Top && p1.Top <= p2.Bottom)
{
return true;
}
else if (laneU && p2.Location.Y == mvUY && p1.Right >= p2.Left && p1.Left <= p2.Right && p1.Bottom >= p2.Top && p1.Top <= p2.Bottom)
{
return true;
}
else
{
return false;
}
}
public void highscoreAdd()
{
if(!File.Exists(fileName))
{
File.Create(fileName);
}
FileInfo fi = new FileInfo(fileName);
StreamWriter writer = fi.AppendText();
writer.WriteLine(user2.Text + ":" + life.Text);
writer.Close();
writer.Dispose();
}
public void button1_Click(object sender, EventArgs e)
{
if (!label1.Visible)
{
if (File.Exists(fileName))
{
label1.Text = "";
StreamReader reader = new StreamReader(fileName);
string l;
while ((l = reader.ReadLine()) != null)
{
label1.Text += l + Environment.NewLine;
}
reader.Close();
reader.Dispose();
label1.Visible = true;
}
}
else
{
label1.Visible = false;
}
}
}
}