Advertisement
wingman007

SnakeWinForm2a

Oct 14th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 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 SnakeWinForm2a
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         private int positionX = 10;
  16.  
  17.         private int positionY = 10;
  18.  
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.             timer1.Interval = 100;
  23.             timer1.Tick += UpdateScreen;
  24.             timer1.Start();
  25.         }
  26.  
  27.         private void UpdateScreen(object source, EventArgs e)
  28.         {
  29.             pbGame.Invalidate();
  30.         }
  31.  
  32.         private void Form1_Load(object sender, EventArgs e)
  33.         {
  34.  
  35.         }
  36.  
  37.         private void pbGame_Paint(object sender, PaintEventArgs e)
  38.         {
  39.             Graphics canvas = e.Graphics;
  40.             canvas.FillEllipse(Brushes.Red, new Rectangle(positionX, positionY, 20, 20));
  41.         }
  42.  
  43.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  44.         {
  45.             switch (e.KeyCode) {
  46.                 case Keys.Right :
  47.                     positionX++;
  48.                     break;
  49.                 case Keys.Left:
  50.                     positionX--;
  51.                     break;
  52.                 case Keys.Up:
  53.                     positionY--;
  54.                     break;
  55.                 case Keys.Down:
  56.                     positionY++;
  57.                     break;
  58.                 case Keys.Escape:
  59.                     Application.Exit();
  60.                     break;
  61.             }
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement