-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
137 lines (120 loc) · 3.84 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
namespace RacingGame
{
public partial class Form1 : Form
{
private Point pos;
private bool dragging, lose = false;
private int countCoins = 0;
public Form1()
{
InitializeComponent();
bg1.MouseDown += MouseClickDown;
bg1.MouseUp += MouseClickUp;
bg1.MouseMove += MouseClickMove;
//BG2
bg2.MouseDown += MouseClickDown;
bg2.MouseUp += MouseClickUp;
bg2.MouseMove += MouseClickMove;
labelLose.Visible = false;
btnRestart.Visible = false;
KeyPreview = true;
}
private void MouseClickDown(object sender, MouseEventArgs e)
{
dragging = true;
pos.X = e.X;
pos.Y = e.Y;
}
private void MouseClickUp(object sender, MouseEventArgs e)
{
dragging = false;
}
private void MouseClickMove(object sender, MouseEventArgs e)
{
if (dragging)
{
Point currPoint = PointToScreen(new Point(e.X, e.Y));
this.Location = new Point(currPoint.X - pos.X, currPoint.Y - pos.Y + bg1.Top);
}
}
//ANOTHER PEW
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Escape)
this.Close();
}
private void timer_Tick(object sender, EventArgs e)
{
int speed = 10;
bg1.Top += speed;
bg2.Top += speed;
int carSpeed = 7;
enemy1.Top += carSpeed;
enemy2.Top += carSpeed;
coin.Top += speed;
if (bg1.Top >= 650)
{
bg1.Top = 0;
bg2.Top = -650;
}
if (coin.Top >= 650)
{
coin.Top = -50;
Random rand = new Random();
coin.Left = rand.Next(150, 560);
}
if (enemy1.Top >= 650)
{
enemy1.Top = -130;
Random rand = new Random();
enemy1.Left = rand.Next(150, 300);
}
if (enemy2.Top >= 650)
{
enemy2.Top = -400;
Random rand = new Random();
enemy2.Left = rand.Next(300, 560);
}
if (player.Bounds.IntersectsWith(enemy1.Bounds)
|| player.Bounds.IntersectsWith(enemy2.Bounds))
{
timer.Enabled = false;
labelLose.Visible = true;
btnRestart.Visible = true;
lose = true;
}
if (player.Bounds.IntersectsWith(coin.Bounds))
{
countCoins++;
labelCoins.Text = "Coins: " + countCoins.ToString();
coin.Top = -50;
Random rand = new Random();
coin.Left = rand.Next(150, 560);
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (lose) return;
int speed = 10;
if ((e.KeyCode == Keys.Left || e.KeyCode == Keys.A) && player.Left > 150)
player.Left -= speed;
else if ((e.KeyCode == Keys.Right || e.KeyCode == Keys.D) && player.Right < 700)
player.Left += speed;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void btnRestart_Click(object sender, EventArgs e)
{
enemy1.Top = -130;
enemy2.Top = -400;
labelLose.Visible = false;
btnRestart.Visible = false;
timer.Enabled = true;
lose = false;
countCoins = 0;
labelCoins.Text = "Coins: 0";
coin.Top = -500;
}
}
}