Advertisement
Guest User

eh (2)

a guest
May 25th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Media;
  5. using System.Windows.Forms;
  6.  
  7. namespace BetterFormMovement
  8. {
  9.     public partial class Form1 : Form
  10.     {
  11.         public Form1()
  12.         {
  13.             InitializeComponent();
  14.         }
  15.  
  16.         int xRes = Screen.PrimaryScreen.Bounds.Width;
  17.         int yRes = Screen.PrimaryScreen.Bounds.Height;
  18.  
  19.         bool aHeld, dHeld;
  20.         bool gameover = false;
  21.  
  22.         List<Label> bullets = new List<Label>();
  23.         List<int> offscreenBullets = new List<int>();
  24.        
  25.         SoundPlayer soundShoot = new SoundPlayer(Properties.Resources.shoot);
  26.        
  27.         Random rng = new Random();
  28.        
  29.         //yeah this is an inside joke. you would never get to 255 anyways.
  30.         byte score = 0;
  31.        
  32.         //change these for a fun time
  33.         int playerSize = 50;
  34.         int enemySize = 100;
  35.         int bulletSize = 10;
  36.         int playerSpeed = 20;
  37.         int enemyStartSpeed = 1;
  38.         int enemySpeed;
  39.         int bulletSpeed = 50;
  40.         Color bulletColor = Color.FromArgb(191, 191, 191);
  41.        
  42.         int panicCount = 0;
  43.  
  44.         void Form1_Load(object sender, EventArgs e)
  45.         {
  46.             Cursor.Hide();
  47.  
  48.             this.Width = xRes;
  49.             this.Height = yRes;
  50.            
  51.             player.Width = player.Height = playerSize;
  52.             player.Location = new Point((xRes / 2) - (player.Width / 2), yRes - playerSize);
  53.            
  54.             badguy.Width = badguy.Height = enemySize;
  55.            
  56.             Respawn();
  57.            
  58.             enemySpeed = enemyStartSpeed;
  59.            
  60.             lblScore.SendToBack();
  61.            
  62.             fps.Enabled = true;
  63.         }
  64.  
  65.         void Form1_KeyDown(object sender, KeyEventArgs e)
  66.         {
  67.             //some of these link to void methods because it looks nicer
  68.             switch (e.KeyCode)
  69.             {
  70.                 case Keys.A:
  71.                     aHeld = true;
  72.                     break;
  73.                 case Keys.D:
  74.                     dHeld = true;
  75.                     break;
  76.                    
  77.                 case Keys.P:
  78.                     Panic();
  79.                     break;
  80.                 case Keys.Space:
  81.                     Shoot();
  82.                     break;
  83.                 case Keys.Escape:
  84.                     Escape();
  85.                     break;
  86.             }
  87.         }
  88.  
  89.         void Form1_KeyUp(object sender, KeyEventArgs e)
  90.         {
  91.             switch (e.KeyCode)
  92.             {
  93.                 case Keys.A:
  94.                     aHeld = false;
  95.                     break;
  96.                 case Keys.D:
  97.                     dHeld = false;
  98.                     break;              
  99.             }
  100.         }
  101.  
  102.         void fps_Tick(object sender, EventArgs e)
  103.         {
  104.             offscreenBullets.Clear();
  105.            
  106.             //player movement
  107.             if (aHeld && player.Left >= 0)
  108.             {
  109.                 player.Location = new Point(player.Location.X - playerSpeed, player.Location.Y);
  110.             }
  111.             if (dHeld && player.Right <= xRes)
  112.             {
  113.                 player.Location = new Point(player.Location.X + playerSpeed, player.Location.Y);
  114.             }
  115.  
  116.             //bullet movement
  117.             for (int i = 0; i < bullets.Count; i++)
  118.             {
  119.                 bullets[i].Location = new Point(bullets[i].Location.X, bullets[i].Location.Y - bulletSpeed);
  120.                
  121.                 if (bullets[i].Location.Y <= -bulletSize)
  122.                 {
  123.                     offscreenBullets.Add(i);
  124.                 }
  125.             }
  126.            
  127.             //remove offscreen bullets to cut lag
  128.             for (int i = 0; i < offscreenBullets.Count; i++)
  129.             {
  130.                 Controls.Remove(bullets[offscreenBullets[i]]);
  131.                 bullets.RemoveAt(offscreenBullets[i]);
  132.             }
  133.            
  134.             //bad guy movement
  135.             badguy.Location = new Point(badguy.Location.X, badguy.Location.Y + enemySpeed);
  136.            
  137.             //bad guy hit detection
  138.             for (int i = 0; i < bullets.Count; i++)
  139.             {
  140.                 if (bullets[i].Top <= badguy.Bottom && bullets[i].Right > badguy.Left && bullets[i].Left < badguy.Right)
  141.                 {
  142.                     Controls.Remove(bullets[i]);
  143.                     bullets.RemoveAt(i);
  144.  
  145.                     score += 1;
  146.                     lblScore.Text = score.ToString();
  147.  
  148.                     enemySpeed = enemyStartSpeed + (score / 2);
  149.  
  150.                     Respawn();
  151.                 }
  152.             }
  153.            
  154.             //detecting if you suck
  155.             if (badguy.Bottom >= (yRes - playerSize))
  156.             {
  157.                 GameOver("You have been killed.");
  158.             }
  159.            
  160.             //detecting if you have too many points
  161.             if (score >= 255)
  162.             {
  163.                 GameOver("Score limit reached. Nice.");
  164.             }
  165.         }
  166.        
  167.         void Shoot()
  168.         {
  169.             //because before you could shoot on the game over screen. woops.
  170.             if (!gameover)
  171.             {
  172.                 soundShoot.Play();
  173.                
  174.                 bullets.Add(new Label());
  175.                 Controls.Add(bullets[bullets.Count - 1]);
  176.                    
  177.                 bullets[bullets.Count - 1].Location = new Point(player.Location.X + (playerSize / 2) - (bulletSize / 2),
  178.                                                                 player.Location.Y - bulletSize);
  179.                
  180.                 bullets[bullets.Count - 1].Width = bullets[bullets.Count - 1].Height = bulletSize;
  181.                 bullets[bullets.Count - 1].BackColor = bulletColor;
  182.                    
  183.                 bullets[bullets.Count - 1].BringToFront();
  184.             }
  185.         }
  186.        
  187.         void Panic()
  188.         {
  189.             if (panicCount == 0)
  190.             {
  191.                 badguy.Location = new Point(rng.Next(xRes - enemySize), 0);
  192.                 panicCount++;
  193.             }
  194.         }
  195.        
  196.         void Respawn()
  197.         {
  198.             badguy.Location = new Point(rng.Next(this.Width - enemySize), 0);
  199.         }
  200.        
  201.         void Escape()
  202.         {
  203.             Cursor.Show();
  204.                    
  205.             fps.Enabled = false;
  206.  
  207.             if (MessageBox.Show("Are you sure you want to close?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
  208.             {
  209.                 Close();
  210.             }
  211.             else
  212.             {
  213.                 Cursor.Hide();
  214.                        
  215.                 fps.Enabled = true;
  216.             }
  217.         }
  218.        
  219.         void GameOver(string additionalText)
  220.         {
  221.             fps.Enabled = false;           
  222.             gameover = true;
  223.            
  224.             soundShoot.Stop();
  225.            
  226.             lblScore.BringToFront();
  227.            
  228.             lblScore.Text = ("GAME OVER" + Environment.NewLine + Environment.NewLine +
  229.                             additionalText + Environment.NewLine +
  230.                             "You eliminated " + score.ToString());
  231.  
  232.             //i hate it when stuff says "1 things" instead of "1 thing" so i did this
  233.             lblScore.Text += (score != 1) ? " Space Cookies." : " Space Cookie.";
  234.            
  235.             lblScore.Text += Environment.NewLine + "Press Esc to close.";
  236.         }
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement