Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using WMPLib;
- namespace SpaceShooterGame
- {
- public partial class EndlessMode : Form
- {
- WindowsMediaPlayer gameMedia;
- WindowsMediaPlayer shootgMedia;
- WindowsMediaPlayer explosion;
- PictureBox[] stars;
- int backgroundSpeed;
- int playerSpeed;
- PictureBox[] munitions;
- int MunitionSpeed;
- Random random;
- Timer backgroundTimer;
- PictureBox[] enemies;
- int enemiesSpeed;
- PictureBox[] enemiesMunition;
- int enemiesMunitionSpeed;
- int score;
- int level;
- int difficulty;
- bool pause;
- bool gameIsOver;
- public EndlessMode()
- {
- InitializeComponent();
- // Initialize the Timer for background movement
- backgroundTimer = new Timer();
- backgroundTimer.Interval = 10;
- backgroundTimer.Tick += MoveBackground_Tick;
- backgroundTimer.Start();
- }
- //Background, enemies, players, sound
- private void SpaceShooter_Load(object sender, EventArgs e)
- {
- //instantiation of variables
- backgroundSpeed = 4;
- playerSpeed = 10;
- stars = new PictureBox[15];
- MunitionSpeed = 20;
- munitions = new PictureBox[3];
- enemies = new PictureBox[7];
- enemiesSpeed = 2;
- enemiesMunitionSpeed = 2;
- random = new Random();
- EnemiesMunitionTimer = new Timer();
- // Set the interval for the timer
- EnemiesMunitionTimer.Tick += EnemiesMunitionTimer_Tick; // Link it to the Tick event
- EnemiesMunitionTimer.Start(); // Start it initially
- pause = false;
- gameIsOver = false;
- score = 0;
- level = 1;
- difficulty = 9;
- Image munition = Image.FromFile("asserts/munition.png");
- 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]);
- }
- for (int i = 0; i < stars.Length; i++)
- {
- stars[i] = new PictureBox();
- stars[i].BorderStyle = BorderStyle.None;
- stars[i].Location = new Point(random.Next(20, 580), random.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]);
- }
- Image enemy1 = Image.FromFile("asserts\\E1.png");
- Image enemy2 = Image.FromFile("asserts\\E2.png");
- Image enemy3 = Image.FromFile("asserts\\E3.png");
- Image boss1 = Image.FromFile("asserts\\Boss1.png");
- Image boss2 = Image.FromFile("asserts\\Boss2.png");
- Image alien1 = Image.FromFile("asserts\\alien1.gif");
- Image alien2 = Image.FromFile("asserts\\alien2.gif");
- for (int i = 0; i < enemies.Length; i++)
- {
- enemies[i] = new PictureBox();
- enemies[i].Size = new Size(40, 40);
- enemies[i].SizeMode = PictureBoxSizeMode.Zoom;
- enemies[i].BorderStyle = BorderStyle.None;
- enemies[i].Visible = false;
- this.Controls.Add((enemies[i]));
- enemies[i].Location = new Point((i + 1) * 50, -50);
- if (i == 0) // For alien1
- {
- enemies[i].Image = alien1;
- enemies[i].Size = new Size(60, 60); // Set size for alien1
- }
- else if (i == 6) // For alien2
- {
- enemies[i].Image = alien2;
- enemies[i].Size = new Size(50, 50); // Set size for alien2
- }
- else // For other enemies
- {
- // Set default enemy images and sizes
- enemies[i].Image = (i == 1) ? enemy2 : (i == 2 || i == 3 || i == 5) ? enemy3 : enemy1;
- enemies[i].Size = new Size(30, 30); // Set default size for other enemies
- }
- }
- enemies[1].Image = enemy2;
- enemies[2].Image = enemy3;
- enemies[3].Image = enemy3;
- enemies[4].Image = enemy1;
- enemies[5].Image = enemy3;
- //gamebg, shootbg, explosion
- gameMedia = new WindowsMediaPlayer();
- shootgMedia = new WindowsMediaPlayer();
- explosion = new WindowsMediaPlayer();
- gameMedia.URL = "songs\\GameSong.mp3";
- shootgMedia.URL = "songs\\shoot.mp3";
- explosion.URL = "songs\\boom.mp3";
- gameMedia.settings.setMode("loop", true);
- gameMedia.settings.volume = 10;
- shootgMedia.settings.volume = 5;
- gameMedia.controls.play();
- //enemy ammunition
- enemiesMunition = new PictureBox[10];
- for (int i = 0; i < enemiesMunition.Length; i++)
- {
- enemiesMunition[i] = new PictureBox();
- enemiesMunition[i].Size = new Size(2, 25);
- enemiesMunition[i].Visible = false;
- enemiesMunition[i].BackColor = Color.Yellow;
- int x = random.Next(0, enemies.Length);
- enemiesMunition[i].Location = new Point(enemies[x].Location.X, enemies[x].Location.Y - 20);
- this.Controls.Add(enemiesMunition[i]);
- }
- }
- 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 LeftTimer_Tick(object sender, EventArgs e)
- {
- if (Player.Left > 20)
- {
- Player.Left -= playerSpeed;
- }
- }
- private void RightTimer_Tick(object sender, EventArgs e)
- {
- if (Player.Right < this.Width - 20)
- {
- Player.Left += playerSpeed;
- }
- }
- private void UpTimer_Tick(object sender, EventArgs e)
- {
- if (Player.Top > 10)
- {
- Player.Top -= playerSpeed;
- }
- }
- private void DownTimer_Tick(object sender, EventArgs e)
- {
- if (Player.Bottom < this.Height - 50)
- {
- Player.Top += playerSpeed;
- }
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if (!pause)
- {
- if (e.KeyCode == Keys.Right)
- {
- RightTimer.Start();
- }
- if (e.KeyCode == Keys.Left)
- {
- LeftTimer.Start();
- }
- if (e.KeyCode == Keys.Down)
- {
- DownTimer.Start();
- }
- if (e.KeyCode == Keys.Up)
- {
- UpTimer.Start();
- }
- }
- }
- private void Form1_KeyUp(object sender, KeyEventArgs e)
- {
- RightTimer.Stop();
- LeftTimer.Stop();
- DownTimer.Stop();
- UpTimer.Stop();
- if (e.KeyCode == Keys.Space)
- {
- if (!gameIsOver)
- {
- if (pause)
- {
- StartTimers(); // This resumes all timers, including enemy ammunition
- label.Visible = false;
- gameMedia.controls.play();
- pause = false;
- }
- else
- {
- label.Location = new Point(this.Width / 2 - 120, 150);
- label.Text = "PAUSED";
- label.Visible = true;
- gameMedia.controls.pause();
- StopTimers();
- pause = true;
- }
- }
- }
- }
- private void MoveMunitionTimer_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;
- Collision();
- }
- else
- {
- munitions[i].Visible = false;
- munitions[i].Location = new Point(Player.Location.X + 20, Player.Location.Y - i * 30);
- }
- shootgMedia.controls.play();
- }
- }
- private void MoveEnemiesTimer_Tick(object sender, EventArgs e)
- {
- MoveEnemies(enemies, enemiesSpeed);
- }
- private void MoveEnemies(PictureBox[] array, int speed)
- {
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i].Top < this.Height) // Only make visible if they are on screen
- {
- array[i].Visible = true;
- array[i].Top += speed;
- }
- else
- {
- array[i].Visible = false; // Set to invisible when off-screen
- array[i].Location = new Point((i + 1) * 50, -200);
- }
- }
- }
- private void Collision()
- {
- for (int i = 0; i < enemies.Length; i++)
- {
- if (enemies[i].Visible) // Only check visible enemies
- {
- // Check for munition collision
- for (int j = 0; j < munitions.Length; j++)
- {
- if (munitions[j].Visible && munitions[j].Bounds.IntersectsWith(enemies[i].Bounds))
- {
- explosion.controls.play();
- score++;
- scorelbl.Text = "Score: " + (score < 10 ? "0" + score.ToString() : score.ToString());
- if (score % 30 == 0)
- {
- level += 1;
- levellbl.Text = "Level: " + (level < 10 ? "0" + level.ToString() : level.ToString());
- }
- enemies[i].Location = new Point((i + 1) * 50, -100); // Reset enemy position
- }
- }
- // Check for player collision
- if (Player.Bounds.IntersectsWith(enemies[i].Bounds))
- {
- explosion.settings.volume = 30;
- explosion.controls.play();
- Player.Visible = false;
- GameOver("Game Over");
- }
- }
- }
- }
- private void GameOver(String str)
- {
- gameIsOver = true; // Mark game as over
- label.Location = new Point(this.Width / 2 - 190, 150);
- str = "GAME OVER";
- label.Text = str;
- label.Visible = true;
- ReplayBtn.Visible = true;
- ExitBtn.Visible = true;
- gameMedia.controls.stop(); // Stop background music
- StopTimers();
- }
- private void StopTimers()
- {
- MoveBackground.Stop();
- MoveEnemiesTimer.Stop();
- MoveMunitionTimer.Stop();
- EnemiesMunitionTimer.Stop();
- }
- private void StartTimers()
- {
- MoveBackground.Start();
- MoveEnemiesTimer.Start(); // Only start this once
- MoveMunitionTimer.Start();
- EnemiesMunitionTimer.Start();
- }
- private void EnemiesMunitionTimer_Tick(object sender, EventArgs e)
- {
- if (pause || gameIsOver) return;
- for (int i = 0; i < enemiesMunition.Length; i++)
- {
- if (enemiesMunition[i].Top < this.Height)
- {
- enemiesMunition[i].Visible = true;
- enemiesMunition[i].Top += enemiesMunitionSpeed;
- CollisionWithEnemiesMunition();
- }
- else
- {
- enemiesMunition[i].Visible = false;
- int x = random.Next(0, enemies.Length);
- enemiesMunition[i].Location = new Point(enemies[x].Location.X + 20, enemies[x].Location.Y + 30);
- }
- }
- }
- private void CollisionWithEnemiesMunition()
- {
- for (int i = 0; i < enemiesMunition.Length; i++)
- {
- if (enemiesMunition[i].Bounds.IntersectsWith(Player.Bounds))
- {
- enemiesMunition[i].Visible = false;
- explosion.settings.volume = 30;
- explosion.controls.play();
- Player.Visible = false;
- GameOver("Game Over");
- }
- }
- }
- private void ReplayBtn_Click(object sender, EventArgs e)
- {
- StopTimers(); // Stop all timers
- this.Controls.Clear(); // Clear all controls
- // Reinitialize the form and reset necessary values
- InitializeComponent(); // Reinitialize the form
- SpaceShooter_Load(sender, e); // Load the game again
- // Reset all speed-related variables to initial values
- backgroundSpeed = 4;
- playerSpeed = 10;
- MunitionSpeed = 20;
- enemiesSpeed = 2;
- enemiesMunitionSpeed = 2;
- // Optionally, reset the score and level
- score = 0;
- level = 1;
- difficulty = 9;
- // Restart the background music
- gameMedia.controls.play();
- // Ensure that the timers are correctly started
- StartTimers();
- }
- private void ExitBtn_Click(object sender, EventArgs e)
- {
- Environment.Exit(1);
- }
- }
- }
Add Comment
Please, Sign In to add comment