Created
May 7, 2016 09:36
-
-
Save davidkevork/285f25abec55ce39f400a47387536d5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Timers; | |
using System.Windows.Forms; | |
namespace WindowsFormsApplication1 | |
{ | |
public partial class Form1 : Form | |
{ | |
private bool left; | |
private bool right; | |
private bool up; | |
private bool down; | |
private static int game_width = 300; | |
private static int game_height = 300; | |
private int snake_x; | |
private int snake_y; | |
private int apple_x; | |
private int apple_y; | |
public Bitmap bmp; | |
public Graphics graph; | |
private Brush black_color = Brushes.Black; | |
private Brush snake_body_color = Brushes.Blue; | |
private Brush apple_color = Brushes.Red; | |
private float snake_x_size = 5; | |
private float snake_y_size = 5; | |
private float snake_default_size = 5; | |
private float snake_max_size = 5; | |
public Form1() | |
{ | |
InitializeComponent(); | |
KeyUp += new KeyEventHandler(OnKeyUp); | |
this.Width = Convert.ToInt32(game_width + 40); | |
this.Height = Convert.ToInt32(game_height + 60); | |
Bitmap bmp = new Bitmap(game_width, game_height); | |
pictureBox1.Width = game_height; | |
pictureBox1.Height = game_height; | |
pictureBox1.Image = bmp; | |
pictureBox1.Location = new Point(10, 10); | |
pictureBox1.Size = new Size(game_width, game_height); | |
Graphics graph = Graphics.FromImage(pictureBox1.Image); | |
graph.FillRectangle(black_color, 0, 0, game_width, game_height); | |
snake_x = (pictureBox1.Size.Width / 2); | |
snake_y = (pictureBox1.Size.Height / 2); | |
this.LocateApple(); | |
Draw_Snake(snake_x, snake_y); | |
System.Windows.Forms.Timer time = new System.Windows.Forms.Timer(); | |
time.Tick += new EventHandler(run); | |
time.Interval = 500; | |
time.Start(); | |
} | |
public void run(Object sender, object e) | |
{ | |
if (left == true) | |
{ | |
snake_x = snake_x - 5; | |
snake_x_size = snake_max_size; | |
snake_y_size = snake_default_size; | |
} | |
else if (right == true) | |
{ | |
snake_x = snake_x + 5; | |
snake_x_size = snake_max_size; | |
snake_y_size = snake_default_size; | |
} | |
else if (up == true) | |
{ | |
snake_y = snake_y - 5; | |
snake_y_size = snake_max_size; | |
snake_x_size = snake_default_size; | |
} | |
else if (down == true) | |
{ | |
snake_y = snake_y + 5; | |
snake_y_size = snake_max_size; | |
snake_x_size = snake_default_size; | |
} | |
this.Draw_Snake(snake_x, snake_y); | |
} | |
public void LocateApple() | |
{ | |
Graphics graph = Graphics.FromImage(pictureBox1.Image); | |
SolidBrush brush = new SolidBrush(Color.Red); | |
Pen pen = new Pen(brush); | |
Random rand = new Random(); | |
apple_x = rand.Next(game_width/5)*5; | |
apple_y = rand.Next(game_height/5)*5; | |
graph.FillRectangle(apple_color, apple_x, apple_y, 5, 5); | |
pictureBox1.Refresh(); | |
graph.Dispose(); | |
} | |
public void Draw_Snake(int x, int y) | |
{ | |
Graphics graph = Graphics.FromImage(pictureBox1.Image); | |
SolidBrush brush = new SolidBrush(Color.Blue); | |
Pen pen = new Pen(brush); | |
graph.Clear(Color.Black); | |
if (left == true) | |
{ | |
graph.FillRectangle(snake_body_color, x, y, snake_x_size, snake_y_size); | |
} | |
else if (right == true) | |
{ | |
graph.FillRectangle(snake_body_color, x, y, -snake_x_size, snake_y_size); | |
} | |
else if (up == true) | |
{ | |
graph.FillRectangle(snake_body_color, x, y, snake_x_size, -snake_y_size); | |
} | |
else if (down == true) | |
{ | |
graph.FillRectangle(snake_body_color, x, y, snake_x_size, snake_y_size); | |
} | |
graph.FillRectangle(snake_body_color, x, y, snake_x_size, snake_y_size); | |
graph.FillRectangle(apple_color, apple_x, apple_y, 5, 5); | |
pictureBox1.Refresh(); | |
graph.Dispose(); | |
if (snake_x == apple_x && snake_y == apple_y) | |
{ | |
snake_max_size = snake_max_size + 5; | |
if (left == true || right == true) | |
{ | |
snake_x_size = snake_max_size; | |
snake_y_size = snake_default_size; | |
} | |
else if (up == true || down == true) | |
{ | |
snake_y_size = snake_max_size; | |
snake_x_size = snake_default_size; | |
} | |
this.LocateApple(); | |
this.Draw_Snake(snake_x, snake_y); | |
} | |
} | |
private void OnKeyUp(object sender, KeyEventArgs e) | |
{ | |
int key = (int)e.KeyCode; | |
if ((key == (int)Keys.Left) && (!right)) | |
{ | |
left = true; | |
up = false; | |
down = false; | |
} | |
if ((key == (int)Keys.Right) && (!left)) | |
{ | |
right = true; | |
up = false; | |
down = false; | |
} | |
if ((key == (int)Keys.Up) && (!down)) | |
{ | |
up = true; | |
right = false; | |
left = false; | |
} | |
if ((key == (int)Keys.Down) && (!up)) | |
{ | |
down = true; | |
right = false; | |
left = false; | |
} | |
if (((key == (int)Keys.Up) || (key == (int)Keys.Down)) && (left == true || right == true)) | |
{ | |
snake_y_size = snake_x_size; | |
snake_x_size = snake_default_size; | |
} | |
if (((key == (int)Keys.Left) || (key == (int)Keys.Right)) && (up == true || down == true)) | |
{ | |
snake_x_size = snake_y_size; | |
snake_y_size = snake_default_size; | |
} | |
Draw_Snake(snake_x, snake_y); | |
} | |
private void pictureBox1_Click(object sender, EventArgs e) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment