-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
203 lines (177 loc) · 7.35 KB
/
Form1.cs
File metadata and controls
203 lines (177 loc) · 7.35 KB
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
// Game developed by Asfand Soomro in February 2023
using System;
using System.IO;
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.Media;
namespace CarGame_CS
{
public partial class Form1 : Form
{
// Current Working Directory
static string workingDirectory = Environment.CurrentDirectory;
// Project Directory
static string projectDirectory = Directory.GetParent(workingDirectory).Parent.FullName;
// Initial Setup
PictureBox newBackground = new PictureBox();
static Car userCar;
string defaultUserCarColor = "green";
int handlingSpeed = 10;
int userSpeed = 10;
SoundPlayer gameMusic = new SoundPlayer();
Timer musicTimer = new Timer();
// Flag
Boolean gameOver = false;
Boolean gameMusicIsPlaying = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Make the user car a Car object instead of just PictureBox object
userCar = new Car(defaultUserCarColor, pbUserCar.Location, 0);
this.Controls.Add(userCar);
userCar.BringToFront();
// Second Image Properties
newBackground.Size = pbBackground.Size;
newBackground.Image = pbBackground.Image;
newBackground.SizeMode = pbBackground.SizeMode;
// Add the second image to the the form
this.Controls.Add(newBackground);
// Start the game music
gameMusic.Stream = Properties.Resources.ResourceManager.GetStream("welcome_to_bad_end_theater");
gameMusic.Play();
gameMusicIsPlaying = true;
musicTimer.Interval = 104 * 1000; // Length of the music 104 seconds
musicTimer.Tick += new EventHandler(musicTimer_Tick);
musicTimer.Start();
this.Refresh();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (!gameOver)
{
Point currentLocation = userCar.Location;
// To move the car right when the user presses the right arrow key
if (e.KeyCode == Keys.Right && currentLocation.X < 509)
{
Point updatedLocation = new Point(currentLocation.X + handlingSpeed, currentLocation.Y);
userCar.Location = updatedLocation;
//userCar.RotateCar("right", 30);
}
// To move the car left when the user presses the left arrow key
else if (e.KeyCode == Keys.Left && currentLocation.X > 189)
{
Point updatedLocation = new Point(currentLocation.X - handlingSpeed, currentLocation.Y);
userCar.Location = updatedLocation;
//userCar.RotateCar("left", 30);
}
// Makes the user's car jump
/*else if (e.KeyCode == Keys.Space)
{
userCar.BringToFront();
userCar.Size = new Size(userCar.Size.Width + 40, userCar.Size.Height + 40);
}*/
}
}
private void gameTimer_Tick(object sender, EventArgs e)
{
// Update the location of first image with given speed
Point updatedBackgroundLocation = new Point(pbBackground.Location.X, pbBackground.Location.Y + userSpeed);
// Apply the updated locations for both moveable background images
pbBackground.Location = updatedBackgroundLocation;
newBackground.Location = new Point(pbBackground.Location.X, pbBackground.Location.Y - (pbBackground.Size.Height - 25));
// When the first bg image reaches the end set its location to default
if (pbBackground.Location.Y >= this.Height - (userSpeed + 50))
{
pbBackground.Location = new Point(0, 0);
}
// Check if collision by looping over each random car
foreach (Car theCar in this.Controls.OfType<Car>())
{
if (theCar == userCar) continue;
if (distanceBetweenCars(userCar.Location, theCar.Location) <= (double)70.0) {
Console.WriteLine(distanceBetweenCars(userCar.Location, theCar.Location));
pbFire.Enabled = true;
pbFire.Visible = true;
pbFire.Location = new Point(userCar.Location.X, userCar.Location.Y - 63);
btnRestart.Enabled = true;
btnRestart.Visible = true;
gameTimer.Stop();
randomCarsTimer.Stop();
gameOver = true;
break;
}
}
//this.Refresh();
}
// Generates a random car with each tick
private void randomCarsTimer_Tick(object sender, EventArgs e)
{
// A random object
Random randObj = new Random();
// Random Car
string[] carColors = {"red", "green", "pink", "yellow"};
int randColorIndex = randObj.Next(0, carColors.Length);
int randX = randObj.Next(189, 509); // Random X position with the road boundaries
Point randLocation = new Point(randX, -40);
int randSpeed = randObj.Next(3, 16);
Car randCar = new Car(carColors[randColorIndex], randLocation, randSpeed);
this.Controls.Add(randCar);
randCar.BringToFront();
randCar.MoveCar();
toolStrip.BringToFront(); // Bring the toolbar in front rather than the random car
// Next Random car timer starts anywehre between 1 to 10 seconds
int randInterval = randObj.Next(1, 10);
randomCarsTimer.Interval = randInterval * 1000;
//this.Refresh();
}
private void btnRestart_Click(object sender, EventArgs e)
{
if(gameOver)
{
gameOver = false;
pbFire.Visible = false;
pbFire.Enabled = false;
btnRestart.Visible = false;
btnRestart.Enabled = false;
randomCarsTimer.Start();
gameTimer.Start();
}
}
// Plays the music when it ends
private void musicTimer_Tick(object sender, EventArgs e)
{
gameMusic.Play();
}
// Finds the distance between two Locations (of cars)
public static double distanceBetweenCars(Point location1, Point location2)
{
double distance = Math.Sqrt(
Math.Pow(location2.X - location1.X, 2) + Math.Pow(location2.Y - location1.Y, 2)
);
return distance;
}
private void btnMusic_Click(object sender, EventArgs e)
{
if (gameMusicIsPlaying) {
gameMusic.Stop();
musicTimer.Stop();
gameMusicIsPlaying = false;
}
else
{
gameMusic.Play();
musicTimer.Start();
gameMusicIsPlaying = true;
}
}
}
}