Advertisement
Marshal_Ananas

6.1

Apr 22nd, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 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.  
  11. namespace spaceinvaiders
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         bool goleft;
  16.         bool goright;
  17.         int speed = 5;
  18.         int score = 0;
  19.         bool isPressed;
  20.         int totalEnemies = 14;
  21.         int playerSpeed = 6;
  22.  
  23.         public Form1()
  24.         {
  25.             InitializeComponent();
  26.         }
  27.  
  28.         private void keyisdown(object sender, KeyEventArgs e)
  29.         {
  30.             if (e.KeyCode == Keys.Left)
  31.             {
  32.                 goleft = true;
  33.             }
  34.             if (e.KeyCode == Keys.Right)
  35.             {
  36.                 goright = true;
  37.             }
  38.             if (e.KeyCode == Keys.Space && !isPressed)
  39.             {
  40.                 isPressed = true;
  41.                 Shot();
  42.             }
  43.         }
  44.  
  45.         private void keyisup(object sender, KeyEventArgs e)
  46.         {
  47.             if (e.KeyCode == Keys.Left)
  48.             {
  49.                 goleft = false;
  50.             }
  51.             if (e.KeyCode == Keys.Right)
  52.             {
  53.                 goright = false;
  54.             }
  55.             if (isPressed)
  56.             {
  57.                 isPressed = false;
  58.  
  59.             }
  60.         }
  61.  
  62.         private void timer1_Tick(object sender, EventArgs e)
  63.         {
  64.             if (goleft)
  65.             {
  66.                 player.Left -= playerSpeed;
  67.                
  68.             }
  69.             else if (goright)
  70.             {
  71.                 player.Left += playerSpeed;
  72.  
  73.             }
  74.  
  75.             foreach(Control x in this.Controls)
  76.             {
  77.                 if(x is PictureBox && x.Tag == "invaders")
  78.                 {
  79.                     if (((PictureBox)x).Bounds.IntersectsWith(player.Bounds))
  80.                         {
  81.                         gameOver();
  82.                         }
  83.                     ((PictureBox)x).Left += speed;
  84.  
  85.                     if(((PictureBox)x).Left>720)
  86.                     {
  87.                         ((PictureBox)x).Top += ((PictureBox)x).Height + 10;
  88.  
  89.                         ((PictureBox)x).Left = -50;
  90.                     }
  91.                 }
  92.             }
  93.  
  94.             foreach(Control y in this.Controls)
  95.             {
  96.                 if(y is PictureBox && y.Tag == "laser")
  97.                 {
  98.                     y.Top -= 20;
  99.                     if(((PictureBox)y).Top < this.Height - 490)
  100.                     {
  101.                         this.Controls.Remove(y);
  102.                     }
  103.                 }
  104.             }
  105.  
  106.  
  107.             foreach (Control i in this.Controls)
  108.             {
  109.                foreach(Control j in this.Controls)
  110.                 {
  111.                     if(i is PictureBox && i.Tag == "invaders")
  112.                     {
  113.                         if (j is PictureBox && j.Tag == "laser")
  114.                         {
  115.  
  116.                             if(i.Bounds.IntersectsWith(j.Bounds))
  117.                             {
  118.                                 score++;
  119.                                 this.Controls.Remove(i);
  120.                                 this.Controls.Remove(j);
  121.                             }
  122.                         }
  123.                     }
  124.                 }
  125.             }
  126.  
  127.  
  128.  
  129.         }
  130.  
  131.        
  132.  
  133.         private void Shot()
  134.         {
  135.             PictureBox laser = new PictureBox();
  136.             laser.Image = Properties.Resources.laser;
  137.             laser.Size = new Size(5, 20);
  138.             laser.Tag = "laser";
  139.             laser.Left = player.Left + player.Width / 2;
  140.             laser.Top = player.Top - 20;
  141.             this.Controls.Add(laser);
  142.             laser.BringToFront();
  143.  
  144.             label1.Text = "Score :" + score;
  145.  
  146.             if(score > totalEnemies -1)
  147.             {
  148.                 gameOver();
  149.                 MessageBox.Show("You win!");
  150.             }
  151.         }
  152.  
  153.         private void gameOver()
  154.         {
  155.             timer1.Stop();
  156.             label1.Text += " Game Over";
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement