-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
185 lines (158 loc) · 4.99 KB
/
Form1.cs
File metadata and controls
185 lines (158 loc) · 4.99 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SpaceShooter
{
public partial class SpaceShooter : Form
{
PictureBox[] stars;
int backgroundspeed;
int playerSpeed;
PictureBox[] munitions;
int MunitionSpeed;
Random rnd;
public SpaceShooter()
{
InitializeComponent();
}
//Creating the "star" objects with different color aand at random locations in the window.
private void Form1_Load(object sender, EventArgs e)
{
backgroundspeed = 4;
playerSpeed = 4;
stars = new PictureBox[10];
rnd = new Random();
MunitionSpeed = 20;
munitions = new PictureBox[3];
//Load Image
Image munition = Image.FromFile(@"assets\munition.png");
for (int i = 0; i < stars.Length; i++)
{
stars[i] = new PictureBox();
stars[i].BorderStyle = BorderStyle.None;
stars[i].Location = new Point(rnd.Next(20, 580), rnd.Next(-10,400));
if (i % 2 == 1)
{
stars[i].Size = new Size(2, 2);
stars[i].BackColor = Color.Wheat;
}
else
{
stars[i].Size = new Size(3,3);
stars[i].BackColor = Color.DarkGray;
}
this.Controls.Add(stars[i]);
}
for (int i = 0;i < munitions.Length; i++)
{
munitions[i] = new PictureBox();
munitions[i].Size = new Size(8,8);
munitions[i].Image = munition;
munitions[i].SizeMode = PictureBoxSizeMode.Zoom;
munitions[i].BorderStyle= BorderStyle.None;
this.Controls.Add(munitions[i]);
}
}
//Dots moving at different speeds to add immersiveness.
private void MoveBackground_Tick(object sender, EventArgs e)
{
for (int i = 0; i < stars.Length / 2; i++)
{
stars[i].Top += backgroundspeed;
if (stars[i].Top >= this.Height)
{
stars[i].Top = -stars[i].Height;
}
}
for (int i = stars.Length / 2; i < stars.Length; i++)
{
stars[i].Top = backgroundspeed - 2;
if (stars[i].Top >= this.Height)
{
stars[i].Top = -stars[i].Height;
}
}
}
private void LeftMoveTimer_Tick(object sender, EventArgs e)
{
if (Player.Left > 10)
{
Player.Left -= playerSpeed;
}
}
private void RightMoveTimer_Tick(object sender, EventArgs e)
{
if (Player.Right < 500)
{
Player.Left += playerSpeed;
}
}
private void DownMoveTimer_Tick(object sender, EventArgs e)
{
if (Player.Top < 400)
{
Player.Top += playerSpeed;
}
}
private void UpMoveTimer_Tick(object sender, EventArgs e)
{
if (Player.Top > 10)
{
Player.Top -= playerSpeed;
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void SpaceShooter_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
RightMoveTimer.Start();
}
if (e.KeyCode == Keys.Left)
{
LeftMoveTimer.Start();
}
if (e.KeyCode == Keys.Down)
{
DownMoveTimer.Start();
}
if (e.KeyCode == Keys.Up)
{
UpMoveTimer.Start();
}
}
private void SpaceShooter_KeyUp(object sender, KeyEventArgs e)
{
RightMoveTimer.Stop();
LeftMoveTimer.Stop();
DownMoveTimer.Stop();
UpMoveTimer.Stop();
}
private void MoveMunitionsTimer_Tick(object sender, EventArgs e)
{
for (int i = 0; i < munitions.Length; i++)
{
if (munitions[i].Top > 0)
{
munitions[i].Visible = true;
munitions[i].Top -= MunitionSpeed;
}
else
{
munitions[i].Visible = false;
munitions[i].Location = new Point(Player.Location.X + 42, Player.Location.Y - i * 30);
}
}
}
}
}