Advertisement
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.Timers;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace UselessV1
- {
- public partial class Form1 : Form
- {
- System.Timers.Timer timer;
- Rectangle character;
- Rectangle block;
- SizeF charSize;
- Boolean left, right, up, down, gameover;
- int hp, gameoverSize;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- this.KeyDown += Form1_KeyDown;
- this.KeyUp += Form1_KeyUp;
- //Create the first position and size
- character.X = 10;
- character.Y = 10;
- charSize = new SizeF(10, 10);
- character.Height = (int)charSize.Height;
- character.Width = (int)charSize.Width;
- hp = 100;
- gameoverSize = 0;
- //block
- block.X = 100;
- block.Y = 100;
- block.Height = 20;
- block.Width = 20;
- //Add timer
- timer = new System.Timers.Timer(33);
- timer.Elapsed += update;
- timer.Start();
- }
- private void Form1_KeyDown(object source, KeyEventArgs e){
- switch (e.KeyCode)
- {
- case Keys.Left:
- left = true;
- right = false;
- break;
- case Keys.Right:
- right = true;
- left = false;
- break;
- case Keys.Up:
- up = true;
- down = false;
- break;
- case Keys.Down:
- down = true;
- up = false;
- break;
- }
- }
- private void Form1_KeyUp(object source, KeyEventArgs e)
- {
- switch (e.KeyCode)
- {
- case Keys.Left:
- left = false;
- break;
- case Keys.Right:
- right = false;
- break;
- case Keys.Up:
- up = false;
- break;
- case Keys.Down:
- down = false;
- break;
- }
- }
- private void update(object source, EventArgs e)
- {
- if (gameover != true)
- {
- if (up)
- {
- character.Y -= 10;
- }
- if (down)
- {
- character.Y += 10;
- }
- if (left)
- {
- character.X -= 10;
- }
- if (right)
- {
- character.X += 10;
- }
- if (character.IntersectsWith(block))
- {
- hp -= 5;
- }
- if (hp <= 0)
- {
- gameover = true;
- }
- this.draw();
- }
- else
- {
- gameoverSize++;
- this.endGame();
- if (gameoverSize >= 25) {
- timer.Stop();
- }
- }
- }
- private void draw()
- {
- Graphics g = this.CreateGraphics();
- g.Clear(Color.FromName("Control"));
- Font font = new Font(FontFamily.GenericSansSerif, 12);
- Brush blackBrush = new SolidBrush(Color.Black);
- Pen blackPen = new Pen(blackBrush);
- Brush redBrush = new SolidBrush(Color.Red);
- Pen redPen = new Pen(redBrush);
- g.DrawString("Health: " + hp + "%", font, blackBrush, new PointF(5, 5));
- g.DrawRectangle(blackPen, character);
- g.DrawRectangle(redPen, block);
- g.Dispose();
- }
- private void endGame()
- {
- Graphics g = this.CreateGraphics();
- g.Clear(Color.FromName("Control"));
- Font font = new Font(FontFamily.GenericSansSerif, gameoverSize);
- Brush blackBrush = new SolidBrush(Color.Black);
- Pen blackPen = new Pen(blackBrush);
- string text = "Game Over";
- g.DrawString(text, font, blackBrush, new PointF((this.Width / 2) - (this.Width / 4), ((this.Height / 2) - font.Height) - 5));
- g.Dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement