Advertisement
wingman007

SnakeWinForm1a

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