Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace SnakeWinForm1a
- {
- public enum Direction { Left, Right, Up, Down}
- public partial class Form1 : Form
- {
- private Direction direction;
- private int positionX;
- private int positionY;
- public Form1()
- {
- InitializeComponent();
- timer1.Interval = 100;
- timer1.Tick += UpdateScreen;
- timer1.Start();
- }
- private void UpdateScreen(object sender, EventArgs e)
- {
- pbGame.Invalidate();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void pbGame_Paint(object sender, PaintEventArgs e)
- {
- Graphics canvas = e.Graphics;
- switch (direction) {
- case Direction.Left:
- positionX--;
- break;
- case Direction.Right:
- positionX++;
- break;
- case Direction.Up:
- positionY--;
- break;
- case Direction.Down:
- positionY++;
- break;
- }
- canvas.FillEllipse(Brushes.Red, new Rectangle(positionX, positionY, 20, 20));
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- switch (e.KeyCode)
- {
- case Keys.Right:
- direction = Direction.Right;
- break;
- case Keys.Left:
- direction = Direction.Left;
- break;
- case Keys.Up:
- direction = Direction.Up;
- break;
- case Keys.Down:
- direction = Direction.Down;
- break;
- case Keys.Escape:
- // this.Close();
- Application.Exit();
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement