Advertisement
wingman007

SnakeWinForm1b

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