public class BodyCell : PictureBox
{
public Direction direct
{ get; set; }
public BodyCell()
: base()
{
this.Width = 10;
this.Height = 10;
this.BackColor = Color.Black;
direct = Direction.Right;
}
}
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 Snake
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int bodyCount = 5;
public Direction direct = Direction.Right;
//public BodyCell[] cells;
public List<BodyCell> cells;
public PictureBox food;
private void Form1_Load(object sender, EventArgs e)
{
cells = new List<BodyCell>();
for (int i = 0; i < bodyCount; i++)
{
BodyCell cell = new BodyCell();
cell.Left = i * 10;
cell.Top = panel1.Height / 2;
cells.Add(cell);
panel1.Controls.Add(cell);
}
food = new PictureBox();
food.BackColor = Color.Black;
food.Height = 10;
food.Width = 10;
Random rY = new Random();
food.Top = rY.Next(0, 30) * 10;
Random rX = new Random();
food.Left = rX.Next(0, 30) * 10;
panel1.Controls.Add(food);
}
private void Timer_Process_Tick(object sender, EventArgs e)
{
SnakeProcess();
if (GameOver())
{
Timer_Process.Enabled = false;
MessageBox.Show("Game Over");
}
}
private void SnakeProcess()
{
#region 移动
for (int i = bodyCount - 1; i >= 0; i--)
{
if (cells[i].direct == Direction.Right)
{
cells[i].Left += 10;
}
else if (cells[i].direct == Direction.Left)
{
cells[i].Left -= 10;
}
else if (cells[i].direct == Direction.Up)
{
cells[i].Top -= 10;
}
else if (cells[i].direct == Direction.Down)
{
cells[i].Top += 10;
}
}
for (int i = 0; i < bodyCount - 1; i++)
{
cells[i].direct = cells[i + 1].direct;
}
#endregion
#region 吃食物
EatFood();
#endregion
}
private bool GameOver()
{
bool isOver = false;
//撞墙
if (cells[bodyCount - 1].Top == 0 && cells[bodyCount - 1].direct == Direction.Up)
{
isOver = true;
}
if (cells[bodyCount - 1].Top == 300 && cells[bodyCount - 1].direct == Direction.Down)
{
isOver = true;
}
if (cells[bodyCount - 1].Left == 0 && cells[bodyCount - 1].direct == Direction.Left)
{
isOver = true;
}
if (cells[bodyCount - 1].Left == 300 && cells[bodyCount - 1].direct == Direction.Right)
{
isOver = true;
}
//撞自己
for (int i = bodyCount - 2; i >= 0; i--)
{
if (cells[i].Top == cells[bodyCount - 1].Top && cells[i].Left == cells[bodyCount - 1].Left)
{
isOver = true;
break;
}
}
return isOver;
}
private void EatFood()
{
BodyCell head = cells[bodyCount - 1];
if (cells[bodyCount - 1].Top == food.Top && cells[bodyCount - 1].Left == food.Left)
{
BodyCell cell = new BodyCell();
panel1.Controls.Add(cell);
cell.direct = head.direct;
if (head.direct == Direction.Up)
{
cell.Top = head.Top - 10;
cell.Left = head.Left;
}
else if (head.direct == Direction.Down)
{
cell.Top = head.Top + 10;
cell.Left = head.Left;
}
else if (head.direct == Direction.Left)
{
cell.Top = head.Top;
cell.Left = head.Left - 10;
}
else if (head.direct == Direction.Right)
{
cell.Top = head.Top;
cell.Left = head.Left + 10;
}
cells.Add(cell);
bodyCount++;
panel1.Controls.Remove(food);
Random rY = new Random();
food.Top = rY.Next(0, 30) * 10;
Random rX = new Random();
food.Left = rX.Next(0, 30) * 10;
panel1.Controls.Add(food);
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up && cells[bodyCount - 1].direct != Direction.Down)
{
//direct = Direction.Up;
cells[bodyCount - 1].direct = Direction.Up;
}
else if (e.KeyCode == Keys.Left && cells[bodyCount - 1].direct != Direction.Right)
{
//direct = Direction.Left;
cells[bodyCount - 1].direct = Direction.Left;
}
else if (e.KeyCode == Keys.Down && cells[bodyCount - 1].direct != Direction.Up)
{
//direct = Direction.Down;
cells[bodyCount - 1].direct = Direction.Down;
}
else if (e.KeyCode == Keys.Right && cells[bodyCount - 1].direct != Direction.Left)
{
//direct = Direction.Right;
cells[bodyCount - 1].direct = Direction.Right;
}
}
}
public enum Direction
{
Up,
Down,
Left,
Right
}
}