Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Media;
- using System.Windows.Forms;
- namespace BetterFormMovement
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- int xRes = Screen.PrimaryScreen.Bounds.Width;
- int yRes = Screen.PrimaryScreen.Bounds.Height;
- bool aHeld, dHeld;
- bool gameover = false;
- List<Label> bullets = new List<Label>();
- List<int> offscreenBullets = new List<int>();
- SoundPlayer soundShoot = new SoundPlayer(Properties.Resources.shoot);
- Random rng = new Random();
- //yeah this is an inside joke. you would never get to 255 anyways.
- byte score = 0;
- //change these for a fun time
- int playerSize = 50;
- int enemySize = 100;
- int bulletSize = 10;
- int playerSpeed = 20;
- int enemyStartSpeed = 1;
- int enemySpeed;
- int bulletSpeed = 50;
- Color bulletColor = Color.FromArgb(191, 191, 191);
- int panicCount = 0;
- void Form1_Load(object sender, EventArgs e)
- {
- Cursor.Hide();
- this.Width = xRes;
- this.Height = yRes;
- player.Width = player.Height = playerSize;
- player.Location = new Point((xRes / 2) - (player.Width / 2), yRes - playerSize);
- badguy.Width = badguy.Height = enemySize;
- Respawn();
- enemySpeed = enemyStartSpeed;
- lblScore.SendToBack();
- fps.Enabled = true;
- }
- void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- //some of these link to void methods because it looks nicer
- switch (e.KeyCode)
- {
- case Keys.A:
- aHeld = true;
- break;
- case Keys.D:
- dHeld = true;
- break;
- case Keys.P:
- Panic();
- break;
- case Keys.Space:
- Shoot();
- break;
- case Keys.Escape:
- Escape();
- break;
- }
- }
- void Form1_KeyUp(object sender, KeyEventArgs e)
- {
- switch (e.KeyCode)
- {
- case Keys.A:
- aHeld = false;
- break;
- case Keys.D:
- dHeld = false;
- break;
- }
- }
- void fps_Tick(object sender, EventArgs e)
- {
- offscreenBullets.Clear();
- //player movement
- if (aHeld && player.Left >= 0)
- {
- player.Location = new Point(player.Location.X - playerSpeed, player.Location.Y);
- }
- if (dHeld && player.Right <= xRes)
- {
- player.Location = new Point(player.Location.X + playerSpeed, player.Location.Y);
- }
- //bullet movement
- for (int i = 0; i < bullets.Count; i++)
- {
- bullets[i].Location = new Point(bullets[i].Location.X, bullets[i].Location.Y - bulletSpeed);
- if (bullets[i].Location.Y <= -bulletSize)
- {
- offscreenBullets.Add(i);
- }
- }
- //remove offscreen bullets to cut lag
- for (int i = 0; i < offscreenBullets.Count; i++)
- {
- Controls.Remove(bullets[offscreenBullets[i]]);
- bullets.RemoveAt(offscreenBullets[i]);
- }
- //bad guy movement
- badguy.Location = new Point(badguy.Location.X, badguy.Location.Y + enemySpeed);
- //bad guy hit detection
- for (int i = 0; i < bullets.Count; i++)
- {
- if (bullets[i].Top <= badguy.Bottom && bullets[i].Right > badguy.Left && bullets[i].Left < badguy.Right)
- {
- Controls.Remove(bullets[i]);
- bullets.RemoveAt(i);
- score += 1;
- lblScore.Text = score.ToString();
- enemySpeed = enemyStartSpeed + (score / 2);
- Respawn();
- }
- }
- //detecting if you suck
- if (badguy.Bottom >= (yRes - playerSize))
- {
- GameOver("You have been killed.");
- }
- //detecting if you have too many points
- if (score >= 255)
- {
- GameOver("Score limit reached. Nice.");
- }
- }
- void Shoot()
- {
- //because before you could shoot on the game over screen. woops.
- if (!gameover)
- {
- soundShoot.Play();
- bullets.Add(new Label());
- Controls.Add(bullets[bullets.Count - 1]);
- bullets[bullets.Count - 1].Location = new Point(player.Location.X + (playerSize / 2) - (bulletSize / 2),
- player.Location.Y - bulletSize);
- bullets[bullets.Count - 1].Width = bullets[bullets.Count - 1].Height = bulletSize;
- bullets[bullets.Count - 1].BackColor = bulletColor;
- bullets[bullets.Count - 1].BringToFront();
- }
- }
- void Panic()
- {
- if (panicCount == 0)
- {
- badguy.Location = new Point(rng.Next(xRes - enemySize), 0);
- panicCount++;
- }
- }
- void Respawn()
- {
- badguy.Location = new Point(rng.Next(this.Width - enemySize), 0);
- }
- void Escape()
- {
- Cursor.Show();
- fps.Enabled = false;
- if (MessageBox.Show("Are you sure you want to close?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
- {
- Close();
- }
- else
- {
- Cursor.Hide();
- fps.Enabled = true;
- }
- }
- void GameOver(string additionalText)
- {
- fps.Enabled = false;
- gameover = true;
- soundShoot.Stop();
- lblScore.BringToFront();
- lblScore.Text = ("GAME OVER" + Environment.NewLine + Environment.NewLine +
- additionalText + Environment.NewLine +
- "You eliminated " + score.ToString());
- //i hate it when stuff says "1 things" instead of "1 thing" so i did this
- lblScore.Text += (score != 1) ? " Space Cookies." : " Space Cookie.";
- lblScore.Text += Environment.NewLine + "Press Esc to close.";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement