diff --git a/Spaceshooter.sln b/Spaceshooter.sln new file mode 100644 index 0000000..3449fe3 --- /dev/null +++ b/Spaceshooter.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36408.4 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spaceshooter", "Spaceshooter\Spaceshooter.csproj", "{FCD2D4AE-6F52-4D83-9E1A-81ADC16ACB1F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FCD2D4AE-6F52-4D83-9E1A-81ADC16ACB1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FCD2D4AE-6F52-4D83-9E1A-81ADC16ACB1F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FCD2D4AE-6F52-4D83-9E1A-81ADC16ACB1F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FCD2D4AE-6F52-4D83-9E1A-81ADC16ACB1F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1612866E-5110-4D81-8894-1E3C87D915B7} + EndGlobalSection +EndGlobal diff --git a/Spaceshooter/Form1.Designer.cs b/Spaceshooter/Form1.Designer.cs new file mode 100644 index 0000000..ea8ea03 --- /dev/null +++ b/Spaceshooter/Form1.Designer.cs @@ -0,0 +1,73 @@ +namespace Spaceshooter +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + player = new PictureBox(); + gameTimer = new System.Windows.Forms.Timer(components); + ((System.ComponentModel.ISupportInitialize)player).BeginInit(); + SuspendLayout(); + // + // player + // + player.BackColor = Color.OrangeRed; + player.Location = new Point(33, 135); + player.Name = "player"; + player.Size = new Size(100, 50); + player.TabIndex = 0; + player.TabStop = false; + player.Click += player_Click; + // + // gameTimer + // + gameTimer.Enabled = true; + gameTimer.Interval = 20; + gameTimer.Tick += gameTimer_Tick; + // + // Form1 + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(player); + Name = "Form1"; + Text = "Form1"; + Load += Form1_Load; + KeyDown += Form1_KeyDown; + KeyUp += Form1_KeyUp; + ((System.ComponentModel.ISupportInitialize)player).EndInit(); + ResumeLayout(false); + } + + #endregion + + private PictureBox player; + private System.Windows.Forms.Timer gameTimer; + } +} diff --git a/Spaceshooter/Form1.cs b/Spaceshooter/Form1.cs new file mode 100644 index 0000000..844ecd6 --- /dev/null +++ b/Spaceshooter/Form1.cs @@ -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ä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) + { + + } + } +} diff --git a/Spaceshooter/Form1.resx b/Spaceshooter/Form1.resx new file mode 100644 index 0000000..c395ae1 --- /dev/null +++ b/Spaceshooter/Form1.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/Spaceshooter/Program.cs b/Spaceshooter/Program.cs new file mode 100644 index 0000000..df731ea --- /dev/null +++ b/Spaceshooter/Program.cs @@ -0,0 +1,17 @@ +namespace Spaceshooter +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + Application.Run(new Form1()); + } + } +} \ No newline at end of file diff --git a/Spaceshooter/Spaceshooter.csproj b/Spaceshooter/Spaceshooter.csproj new file mode 100644 index 0000000..663fdb8 --- /dev/null +++ b/Spaceshooter/Spaceshooter.csproj @@ -0,0 +1,11 @@ + + + + WinExe + net8.0-windows + enable + true + enable + + + \ No newline at end of file