airfuzion

space shooter game

Dec 8th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.52 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using WMPLib;
  11.  
  12. namespace SpaceShooterGame
  13. {
  14.     public partial class EndlessMode : Form
  15.     {
  16.         WindowsMediaPlayer gameMedia;
  17.         WindowsMediaPlayer shootgMedia;
  18.         WindowsMediaPlayer explosion;
  19.  
  20.  
  21.  
  22.         PictureBox[] stars;
  23.  
  24.         int backgroundSpeed;
  25.         int playerSpeed;
  26.  
  27.         PictureBox[] munitions;
  28.         int MunitionSpeed;
  29.  
  30.         Random random;
  31.         Timer backgroundTimer;
  32.  
  33.         PictureBox[] enemies;
  34.         int enemiesSpeed;
  35.  
  36.         PictureBox[] enemiesMunition;
  37.         int enemiesMunitionSpeed;
  38.  
  39.         int score;
  40.         int level;
  41.         int difficulty;
  42.         bool pause;
  43.         bool gameIsOver;
  44.  
  45.  
  46.  
  47.         public EndlessMode()
  48.         {
  49.             InitializeComponent();
  50.             // Initialize the Timer for background movement
  51.             backgroundTimer = new Timer();
  52.             backgroundTimer.Interval = 10;
  53.             backgroundTimer.Tick += MoveBackground_Tick;
  54.             backgroundTimer.Start();
  55.         }
  56.         //Background, enemies, players, sound
  57.         private void SpaceShooter_Load(object sender, EventArgs e)
  58.         {
  59.             //instantiation of variables
  60.             backgroundSpeed = 4;
  61.             playerSpeed = 10;
  62.             stars = new PictureBox[15];
  63.             MunitionSpeed = 20;
  64.             munitions = new PictureBox[3];
  65.             enemies = new PictureBox[7];
  66.             enemiesSpeed = 2;
  67.             enemiesMunitionSpeed = 2;
  68.             random = new Random();
  69.  
  70.  
  71.             EnemiesMunitionTimer = new Timer();
  72.             // Set the interval for the timer
  73.             EnemiesMunitionTimer.Tick += EnemiesMunitionTimer_Tick; // Link it to the Tick event
  74.             EnemiesMunitionTimer.Start(); // Start it initially
  75.  
  76.  
  77.             pause = false;
  78.             gameIsOver = false;
  79.             score = 0;
  80.             level = 1;
  81.             difficulty = 9;
  82.  
  83.  
  84.             Image munition = Image.FromFile("asserts/munition.png");
  85.             for (int i = 0; i < munitions.Length; i++)
  86.             {
  87.                 munitions[i] = new PictureBox();
  88.                 munitions[i].Size = new Size(8, 8);
  89.                 munitions[i].Image = munition;
  90.                 munitions[i].SizeMode = PictureBoxSizeMode.Zoom;
  91.                 munitions[i].BorderStyle = BorderStyle.None;
  92.                 this.Controls.Add(munitions[i]);
  93.             }
  94.  
  95.  
  96.             for (int i = 0; i < stars.Length; i++)
  97.             {
  98.                 stars[i] = new PictureBox();
  99.                 stars[i].BorderStyle = BorderStyle.None;
  100.                 stars[i].Location = new Point(random.Next(20, 580), random.Next(-10, 400));
  101.  
  102.                 if (i % 2 == 1)
  103.                 {
  104.                     stars[i].Size = new Size(2, 2);
  105.                     stars[i].BackColor = Color.Wheat;
  106.                 }
  107.                 else
  108.                 {
  109.                     stars[i].Size = new Size(3, 3);
  110.                     stars[i].BackColor = Color.DarkGray;
  111.                 }
  112.                 this.Controls.Add(stars[i]);
  113.             }
  114.  
  115.             Image enemy1 = Image.FromFile("asserts\\E1.png");
  116.             Image enemy2 = Image.FromFile("asserts\\E2.png");
  117.             Image enemy3 = Image.FromFile("asserts\\E3.png");
  118.             Image boss1 = Image.FromFile("asserts\\Boss1.png");
  119.             Image boss2 = Image.FromFile("asserts\\Boss2.png");
  120.             Image alien1 = Image.FromFile("asserts\\alien1.gif");
  121.             Image alien2 = Image.FromFile("asserts\\alien2.gif");
  122.  
  123.  
  124.  
  125.             for (int i = 0; i < enemies.Length; i++)
  126.             {
  127.                 enemies[i] = new PictureBox();
  128.                 enemies[i].Size = new Size(40, 40);
  129.                 enemies[i].SizeMode = PictureBoxSizeMode.Zoom;
  130.                 enemies[i].BorderStyle = BorderStyle.None;
  131.                 enemies[i].Visible = false;
  132.                 this.Controls.Add((enemies[i]));
  133.                 enemies[i].Location = new Point((i + 1) * 50, -50);
  134.  
  135.                 if (i == 0) // For alien1
  136.                 {
  137.                     enemies[i].Image = alien1;
  138.                     enemies[i].Size = new Size(60, 60); // Set size for alien1
  139.                 }
  140.                 else if (i == 6) // For alien2
  141.                 {
  142.                     enemies[i].Image = alien2;
  143.                     enemies[i].Size = new Size(50, 50); // Set size for alien2
  144.                 }
  145.                 else // For other enemies
  146.                 {
  147.                     // Set default enemy images and sizes
  148.                     enemies[i].Image = (i == 1) ? enemy2 : (i == 2 || i == 3 || i == 5) ? enemy3 : enemy1;
  149.                     enemies[i].Size = new Size(30, 30); // Set default size for other enemies
  150.                 }
  151.  
  152.             }
  153.             enemies[1].Image = enemy2;
  154.             enemies[2].Image = enemy3;
  155.             enemies[3].Image = enemy3;
  156.             enemies[4].Image = enemy1;
  157.             enemies[5].Image = enemy3;
  158.  
  159.  
  160.             //gamebg, shootbg, explosion
  161.             gameMedia = new WindowsMediaPlayer();
  162.             shootgMedia = new WindowsMediaPlayer();
  163.             explosion = new WindowsMediaPlayer();
  164.  
  165.             gameMedia.URL = "songs\\GameSong.mp3";
  166.             shootgMedia.URL = "songs\\shoot.mp3";
  167.             explosion.URL = "songs\\boom.mp3";
  168.  
  169.  
  170.             gameMedia.settings.setMode("loop", true);
  171.             gameMedia.settings.volume = 10;
  172.             shootgMedia.settings.volume = 5;
  173.  
  174.             gameMedia.controls.play();
  175.  
  176.             //enemy ammunition
  177.             enemiesMunition = new PictureBox[10];
  178.             for (int i = 0; i < enemiesMunition.Length; i++)
  179.             {
  180.                 enemiesMunition[i] = new PictureBox();
  181.                 enemiesMunition[i].Size = new Size(2, 25);
  182.                 enemiesMunition[i].Visible = false;
  183.                 enemiesMunition[i].BackColor = Color.Yellow;
  184.                 int x = random.Next(0, enemies.Length);
  185.                 enemiesMunition[i].Location = new Point(enemies[x].Location.X, enemies[x].Location.Y - 20);
  186.                 this.Controls.Add(enemiesMunition[i]);
  187.             }
  188.  
  189.         }
  190.  
  191.         private void MoveBackground_Tick(object sender, EventArgs e)
  192.         {
  193.             for (int i = 0; i < stars.Length / 2; i++)
  194.             {
  195.                 stars[i].Top += backgroundSpeed;
  196.                 if (stars[i].Top >= this.Height)
  197.                 {
  198.                     stars[i].Top = -stars[i].Height;
  199.                 }
  200.             }
  201.             for (int i = stars.Length / 2; i < stars.Length; i++)
  202.             {
  203.                 stars[i].Top += backgroundSpeed - 2;
  204.  
  205.                 if (stars[i].Top >= this.Height)
  206.                 {
  207.                     stars[i].Top = -stars[i].Height;
  208.                 }
  209.             }
  210.         }
  211.  
  212.         private void LeftTimer_Tick(object sender, EventArgs e)
  213.         {
  214.             if (Player.Left > 20)
  215.             {
  216.                 Player.Left -= playerSpeed;
  217.             }
  218.         }
  219.  
  220.         private void RightTimer_Tick(object sender, EventArgs e)
  221.         {
  222.             if (Player.Right < this.Width - 20)
  223.             {
  224.                 Player.Left += playerSpeed;
  225.             }
  226.         }
  227.  
  228.         private void UpTimer_Tick(object sender, EventArgs e)
  229.         {
  230.             if (Player.Top > 10)
  231.             {
  232.                 Player.Top -= playerSpeed;
  233.             }
  234.         }
  235.  
  236.         private void DownTimer_Tick(object sender, EventArgs e)
  237.         {
  238.             if (Player.Bottom < this.Height - 50)
  239.             {
  240.                 Player.Top += playerSpeed;
  241.             }
  242.         }
  243.  
  244.  
  245.  
  246.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  247.         {
  248.             if (!pause)
  249.             {
  250.                 if (e.KeyCode == Keys.Right)
  251.                 {
  252.                     RightTimer.Start();
  253.                 }
  254.                 if (e.KeyCode == Keys.Left)
  255.                 {
  256.                     LeftTimer.Start();
  257.                 }
  258.                 if (e.KeyCode == Keys.Down)
  259.                 {
  260.                     DownTimer.Start();
  261.                 }
  262.                 if (e.KeyCode == Keys.Up)
  263.                 {
  264.                     UpTimer.Start();
  265.                 }
  266.             }
  267.         }
  268.  
  269.         private void Form1_KeyUp(object sender, KeyEventArgs e)
  270.         {
  271.             RightTimer.Stop();
  272.             LeftTimer.Stop();
  273.             DownTimer.Stop();
  274.             UpTimer.Stop();
  275.  
  276.             if (e.KeyCode == Keys.Space)
  277.             {
  278.                 if (!gameIsOver)
  279.                 {
  280.                     if (pause)
  281.                     {
  282.                         StartTimers(); // This resumes all timers, including enemy ammunition
  283.                         label.Visible = false;
  284.                         gameMedia.controls.play();
  285.                         pause = false;
  286.                     }
  287.                     else
  288.                     {
  289.                         label.Location = new Point(this.Width / 2 - 120, 150);
  290.                         label.Text = "PAUSED";
  291.                         label.Visible = true;
  292.                         gameMedia.controls.pause();
  293.                         StopTimers();
  294.                         pause = true;
  295.                     }
  296.                 }
  297.             }
  298.  
  299.         }
  300.  
  301.         private void MoveMunitionTimer_Tick(object sender, EventArgs e)
  302.         {
  303.             for (int i = 0; i < munitions.Length; i++)
  304.             {
  305.                 if (munitions[i].Top > 0)
  306.                 {
  307.                     munitions[i].Visible = true;
  308.                     munitions[i].Top -= MunitionSpeed;
  309.  
  310.                     Collision();
  311.                 }
  312.                 else
  313.                 {
  314.                     munitions[i].Visible = false;
  315.                     munitions[i].Location = new Point(Player.Location.X + 20, Player.Location.Y - i * 30);
  316.                 }
  317.                 shootgMedia.controls.play();
  318.             }
  319.         }
  320.  
  321.         private void MoveEnemiesTimer_Tick(object sender, EventArgs e)
  322.         {
  323.             MoveEnemies(enemies, enemiesSpeed);
  324.         }
  325.  
  326.  
  327.         private void MoveEnemies(PictureBox[] array, int speed)
  328.         {
  329.             for (int i = 0; i < array.Length; i++)
  330.             {
  331.                 if (array[i].Top < this.Height) // Only make visible if they are on screen
  332.                 {
  333.                     array[i].Visible = true;
  334.                     array[i].Top += speed;
  335.                 }
  336.                 else
  337.                 {
  338.                     array[i].Visible = false; // Set to invisible when off-screen
  339.                     array[i].Location = new Point((i + 1) * 50, -200);
  340.                 }
  341.             }
  342.         }
  343.  
  344.         private void Collision()
  345.         {
  346.             for (int i = 0; i < enemies.Length; i++)
  347.             {
  348.                 if (enemies[i].Visible) // Only check visible enemies
  349.                 {
  350.                     // Check for munition collision
  351.                     for (int j = 0; j < munitions.Length; j++)
  352.                     {
  353.                         if (munitions[j].Visible && munitions[j].Bounds.IntersectsWith(enemies[i].Bounds))
  354.                         {
  355.                             explosion.controls.play();
  356.                             score++;
  357.                             scorelbl.Text = "Score: " + (score < 10 ? "0" + score.ToString() : score.ToString());
  358.  
  359.                             if (score % 30 == 0)
  360.                             {
  361.                                 level += 1;
  362.                                 levellbl.Text = "Level: " + (level < 10 ? "0" + level.ToString() : level.ToString());
  363.  
  364.                             }
  365.                             enemies[i].Location = new Point((i + 1) * 50, -100); // Reset enemy position
  366.                         }
  367.                     }
  368.  
  369.                     // Check for player collision
  370.                     if (Player.Bounds.IntersectsWith(enemies[i].Bounds))
  371.                     {
  372.                         explosion.settings.volume = 30;
  373.                         explosion.controls.play();
  374.                         Player.Visible = false;
  375.                         GameOver("Game Over");
  376.                     }
  377.                 }
  378.             }
  379.         }
  380.         private void GameOver(String str)
  381.         {
  382.             gameIsOver = true; // Mark game as over
  383.             label.Location = new Point(this.Width / 2 - 190, 150);
  384.             str = "GAME OVER";
  385.             label.Text = str;
  386.             label.Visible = true;
  387.             ReplayBtn.Visible = true;
  388.             ExitBtn.Visible = true;
  389.             gameMedia.controls.stop(); // Stop background music
  390.             StopTimers();
  391.         }
  392.  
  393.         private void StopTimers()
  394.         {
  395.             MoveBackground.Stop();
  396.             MoveEnemiesTimer.Stop();
  397.             MoveMunitionTimer.Stop();
  398.             EnemiesMunitionTimer.Stop();
  399.         }
  400.  
  401.         private void StartTimers()
  402.         {
  403.             MoveBackground.Start();
  404.             MoveEnemiesTimer.Start(); // Only start this once
  405.             MoveMunitionTimer.Start();
  406.             EnemiesMunitionTimer.Start();
  407.  
  408.         }
  409.  
  410.         private void EnemiesMunitionTimer_Tick(object sender, EventArgs e)
  411.         {
  412.             if (pause || gameIsOver) return;
  413.  
  414.             for (int i = 0; i < enemiesMunition.Length; i++)
  415.             {
  416.                 if (enemiesMunition[i].Top < this.Height)
  417.                 {
  418.                     enemiesMunition[i].Visible = true;
  419.                     enemiesMunition[i].Top += enemiesMunitionSpeed;
  420.                     CollisionWithEnemiesMunition();
  421.                 }
  422.                 else
  423.                 {
  424.                     enemiesMunition[i].Visible = false;
  425.                     int x = random.Next(0, enemies.Length);
  426.                     enemiesMunition[i].Location = new Point(enemies[x].Location.X + 20, enemies[x].Location.Y + 30);
  427.                 }
  428.  
  429.             }
  430.  
  431.         }
  432.  
  433.         private void CollisionWithEnemiesMunition()
  434.         {
  435.             for (int i = 0; i < enemiesMunition.Length; i++)
  436.             {
  437.                 if (enemiesMunition[i].Bounds.IntersectsWith(Player.Bounds))
  438.                 {
  439.                     enemiesMunition[i].Visible = false;
  440.                     explosion.settings.volume = 30;
  441.                     explosion.controls.play();
  442.                     Player.Visible = false;
  443.                     GameOver("Game Over");
  444.                 }
  445.  
  446.             }
  447.  
  448.         }
  449.  
  450.         private void ReplayBtn_Click(object sender, EventArgs e)
  451.         {
  452.             StopTimers(); // Stop all timers
  453.             this.Controls.Clear(); // Clear all controls
  454.  
  455.             // Reinitialize the form and reset necessary values
  456.             InitializeComponent(); // Reinitialize the form
  457.             SpaceShooter_Load(sender, e); // Load the game again
  458.  
  459.             // Reset all speed-related variables to initial values
  460.             backgroundSpeed = 4;
  461.             playerSpeed = 10;
  462.             MunitionSpeed = 20;
  463.             enemiesSpeed = 2;
  464.             enemiesMunitionSpeed = 2;
  465.  
  466.             // Optionally, reset the score and level
  467.             score = 0;
  468.             level = 1;
  469.             difficulty = 9;
  470.  
  471.             // Restart the background music
  472.             gameMedia.controls.play();
  473.  
  474.             // Ensure that the timers are correctly started
  475.             StartTimers();
  476.         }
  477.         private void ExitBtn_Click(object sender, EventArgs e)
  478.         {
  479.             Environment.Exit(1);
  480.         }
  481.     }
  482. }
  483.  
  484.  
Add Comment
Please, Sign In to add comment