Advertisement
wingman007

SnakeWinForm2b

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