Advertisement
cafreak

First time playing with moving graphics and intersects

Jan 12th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.78 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.Timers;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace UselessV1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         System.Timers.Timer timer;
  17.         Rectangle character;
  18.         Rectangle block;
  19.  
  20.         SizeF charSize;
  21.         Boolean left, right, up, down, gameover;
  22.  
  23.         int hp, gameoverSize;
  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.         }
  28.  
  29.         private void Form1_Load(object sender, EventArgs e)
  30.         {
  31.             this.KeyDown += Form1_KeyDown;
  32.             this.KeyUp += Form1_KeyUp;
  33.            
  34.  
  35.             //Create the first position and size
  36.             character.X = 10;
  37.             character.Y = 10;
  38.             charSize = new SizeF(10, 10);
  39.  
  40.             character.Height = (int)charSize.Height;
  41.             character.Width = (int)charSize.Width;
  42.  
  43.             hp = 100;
  44.             gameoverSize = 0;
  45.             //block
  46.  
  47.             block.X = 100;
  48.             block.Y = 100;
  49.             block.Height = 20;
  50.             block.Width = 20;
  51.  
  52.             //Add timer
  53.             timer = new System.Timers.Timer(33);
  54.             timer.Elapsed += update;
  55.             timer.Start();
  56.         }
  57.  
  58.         private void Form1_KeyDown(object source, KeyEventArgs e){
  59.             switch (e.KeyCode)
  60.             {
  61.                 case Keys.Left:
  62.                     left = true;
  63.                     right = false;
  64.                     break;
  65.                 case Keys.Right:
  66.                     right = true;
  67.                     left = false;
  68.                     break;
  69.                 case Keys.Up:
  70.                     up = true;
  71.                     down = false;
  72.                     break;
  73.                 case Keys.Down:
  74.                     down = true;
  75.                     up = false;
  76.                     break;
  77.             }
  78.         }
  79.  
  80.         private void Form1_KeyUp(object source, KeyEventArgs e)
  81.         {
  82.             switch (e.KeyCode)
  83.             {
  84.                 case Keys.Left:
  85.                     left = false;
  86.                     break;
  87.                 case Keys.Right:
  88.                     right = false;
  89.                     break;
  90.                 case Keys.Up:
  91.                     up = false;
  92.                     break;
  93.                 case Keys.Down:
  94.                     down = false;
  95.                     break;
  96.             }
  97.         }
  98.  
  99.         private void update(object source, EventArgs e)
  100.         {
  101.             if (gameover != true)
  102.             {
  103.                 if (up)
  104.                 {
  105.                     character.Y -= 10;
  106.                 }
  107.  
  108.                 if (down)
  109.                 {
  110.                     character.Y += 10;
  111.                 }
  112.  
  113.                 if (left)
  114.                 {
  115.                     character.X -= 10;
  116.                 }
  117.  
  118.                 if (right)
  119.                 {
  120.                     character.X += 10;
  121.                 }
  122.  
  123.                 if (character.IntersectsWith(block))
  124.                 {
  125.                     hp -= 5;
  126.                 }
  127.  
  128.                 if (hp <= 0)
  129.                 {
  130.                     gameover = true;
  131.                 }
  132.  
  133.                 this.draw();
  134.             }
  135.             else
  136.             {
  137.                 gameoverSize++;
  138.                 this.endGame();
  139.  
  140.                 if (gameoverSize >= 25) {
  141.                     timer.Stop();
  142.                 }
  143.             }
  144.         }
  145.  
  146.         private void draw()
  147.         {
  148.            
  149.             Graphics g = this.CreateGraphics();
  150.  
  151.             g.Clear(Color.FromName("Control"));
  152.  
  153.             Font font = new Font(FontFamily.GenericSansSerif, 12);
  154.            
  155.  
  156.             Brush blackBrush = new SolidBrush(Color.Black);
  157.             Pen blackPen = new Pen(blackBrush);
  158.  
  159.             Brush redBrush = new SolidBrush(Color.Red);
  160.             Pen redPen = new Pen(redBrush);
  161.  
  162.             g.DrawString("Health: " + hp + "%", font, blackBrush, new PointF(5, 5));
  163.             g.DrawRectangle(blackPen, character);
  164.             g.DrawRectangle(redPen, block);
  165.  
  166.             g.Dispose();
  167.         }
  168.  
  169.         private void endGame()
  170.         {
  171.  
  172.            
  173.             Graphics g = this.CreateGraphics();
  174.  
  175.             g.Clear(Color.FromName("Control"));
  176.  
  177.             Font font = new Font(FontFamily.GenericSansSerif, gameoverSize);
  178.  
  179.  
  180.             Brush blackBrush = new SolidBrush(Color.Black);
  181.             Pen blackPen = new Pen(blackBrush);
  182.             string text = "Game Over";
  183.  
  184.             g.DrawString(text, font, blackBrush, new PointF((this.Width / 2) - (this.Width / 4), ((this.Height / 2) - font.Height) - 5));
  185.  
  186.             g.Dispose();
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement