Add project files.
This commit is contained in:
141
Spaceshooter/Form1.cs
Normal file
141
Spaceshooter/Form1.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
using System.Drawing.Imaging;
|
||||
using System.Xaml.Permissions;
|
||||
|
||||
namespace Spaceshooter
|
||||
{
|
||||
public partial class Form1 : Form
|
||||
{
|
||||
bool goUp, goDown;
|
||||
int playerSpeed = 10;
|
||||
int spawnSpeed = 10;
|
||||
int bulletSpeed = 15;
|
||||
int enemySpeed = 8;
|
||||
int score = 0;
|
||||
|
||||
Random rand = new Random();
|
||||
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void player_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void gameTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
if (goUp && player.Top > 0)
|
||||
{
|
||||
player.Top -= playerSpeed;
|
||||
}
|
||||
if (goDown && player.Top + player.Height < this.ClientSize.Height)
|
||||
{
|
||||
player.Top += playerSpeed;
|
||||
}
|
||||
|
||||
for (int i = this.Controls.Count - 1; i >= 0; i--)
|
||||
{
|
||||
Control x = this.Controls[i];
|
||||
|
||||
if (x is PictureBox && (string)x.Tag == "bullet")
|
||||
{
|
||||
x.Left += bulletSpeed;
|
||||
|
||||
if (x.Left > this.ClientSize.Width)
|
||||
{
|
||||
this.Controls.Remove(x);
|
||||
x.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
if (x is PictureBox && (string)x.Tag == "enemy")
|
||||
{
|
||||
x.Left -= enemySpeed;
|
||||
|
||||
if (x.Bounds.IntersectsWith(player.Bounds))
|
||||
{
|
||||
gameTimer.Stop();
|
||||
MessageBox.Show("Game Over! Po<50>ng " + score);
|
||||
Application.Restart();
|
||||
}
|
||||
|
||||
if (x.Left + this.ClientSize.Width < 0)
|
||||
{
|
||||
this.Controls.Remove(x);
|
||||
x.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (rand.Next(1, 101) <= spawnSpeed)
|
||||
{
|
||||
MakeEnemy();
|
||||
}
|
||||
}
|
||||
private void Shoot()
|
||||
{
|
||||
PictureBox bullet = new PictureBox();
|
||||
bullet.Width = 10;
|
||||
bullet.Height = 5;
|
||||
bullet.BackColor = Color.Red;
|
||||
bullet.Tag = "bullet";
|
||||
bullet.Left = player.Left + player.Width;
|
||||
bullet.Top = player.Top + player.Height / 2 - bullet.Height / 2;
|
||||
this.Controls.Add(bullet);
|
||||
bullet.BringToFront();
|
||||
|
||||
}
|
||||
|
||||
private void MakeEnemy()
|
||||
{
|
||||
PictureBox enemy = new PictureBox();
|
||||
enemy.Width = 30;
|
||||
enemy.Height = 30;
|
||||
enemy.BackColor = Color.Green;
|
||||
enemy.Tag = "Enemy";
|
||||
enemy.Left = player.Left + 1000;
|
||||
enemy.Top = rand.Next(0, (this.ClientSize.Height - enemy.Height));
|
||||
this.Controls.Add(enemy);
|
||||
enemy.BringToFront();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void Form1_KeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.W)
|
||||
{
|
||||
goUp = true;
|
||||
}
|
||||
|
||||
if (e.KeyCode == Keys.Down || e.KeyCode == Keys.S)
|
||||
{
|
||||
goDown = true;
|
||||
}
|
||||
if (e.KeyCode == Keys.Space)
|
||||
{
|
||||
Shoot();
|
||||
}
|
||||
}
|
||||
|
||||
private void Form1_KeyUp(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.W)
|
||||
{
|
||||
goUp = false;
|
||||
}
|
||||
|
||||
if (e.KeyCode == Keys.Down || e.KeyCode == Keys.S)
|
||||
{
|
||||
goDown = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void Form1_Load(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user