求助ing~哪位大虾帮我看一下这个贪食蛇代码
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 贪食蛇
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int d = 0;
public class Snake
{
public int snake_long;
public Point direct;
public Point head;
public Map[,] map=new Map[20,20];
public int level=1;
public Random rand=new Random();
public Snake()
{
}
public void start()
{
for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
map=new Map(0,false,false);
snake_long=3;
direct.X=-1;
direct.Y=0;
head.X=10;
head.Y=10;
map[10,10].count=1;
map[11,10].count =2;
map[12,10].count =3;
}
public void GameOver()
{
level = 1;
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
map = new Map(0, false, false);
}
public bool is_move()
{
Point t = new Point();
bool flag = false;
t.X = head.X + direct.X;
t.Y = head.Y + direct.Y;
if (t.X > 19 || t.X < 0 || t.Y < 0 || t.Y > 19)
flag = false;
else if (!map[t.X,t.Y].tone&&(map[t.X, t.Y].count == 0 || map[t.X, t.Y].count == snake_long))
flag = true;
if (flag)
return true;
else
return false;
}
public void move()
{
Point t=new Point();
t.X = head.X + direct.X;
t.Y = head.Y + direct.Y;
if (map[t.X, t.Y].food)
{
snake_long++;
map[t.X, t.Y].food = false;
head.X = t.X;
head.Y = t.Y;
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
if (map.count >0)
{
map.count++;
if (map.count > snake_long)
map.count = 0;
}
map[t.X, t.Y].count = 1;
}
else if(map[t.X,t.Y].count==0||map[t.X,t.Y].count==snake_long)
{
head.X = t.X;
head.Y = t.Y;
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
if (map.count != 0)
{
map.count++;
if (map.count > snake_long)
map.count = 0;
}
map[t.X, t.Y].count = 1;
}
}
public bool is_chuangedix(int x, int y)
{
bool flag = false;
if (x == 0)
if (direct.X != 0)
flag = true;
else
flag = false;
else if (direct.Y == 0)
flag = false;
else
flag = true;
if (flag)
{
direct.X = x;
direct.Y = y;
return true;
}
else
return false;
}
public void set_foodtone()
{
int i, j;
i = rand.Next(0, 20);
j = rand.Next(0, 20);
if (!map.tone && map.count==0)
map.food = true;
i = rand.Next(0, 20);
j = rand.Next(0, 20);
if (!map.food && map.count==0)
map.tone = true;
}
}//end Snake
public class Map
{
public int count;
public bool food;
public bool tone;
public Map(int co,bool fo,bool to)
{
count=co;
food=fo;
tone=to;
}
}//end Map
Snake snake = new Snake();
public void draw()
{
Graphics g=gamePictureBox.CreateGraphics();
g.Clear(Color.White);
g.DrawRectangle(new Pen(Color.Black), 0,0,220,220);
g.DrawRectangle(new Pen(Color.Blue),10,10,200,200);
for(int i=0;i<20;i++)
for (int j = 0; j < 20; j++)
{
if (snake.map.count != 0)
if(snake.map.count==1)
g.FillEllipse(new SolidBrush(Color.Red),10+i*10,10+j*10,10,10);
else
g.FillEllipse(new SolidBrush (Color.Gold ), 10 + i * 10, 10 + j * 10, 10, 10);
else if (snake.map.food)
g.FillRectangle(new SolidBrush(Color.Violet ), 10 + i * 10, 10 + j * 10, 10, 10);
else if (snake.map.tone)
g.FillRectangle(new SolidBrush(Color.Black ), 10 + i * 10, 10 + j * 10, 10, 10);
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch ((int)e.KeyCode)
{
case 38: if (snake.is_chuangedix(0, -1)) snake.move(); break;//up
case 37: if (snake.is_chuangedix(-1,0)) snake.move(); break;//leaf
case 39: if (snake.is_chuangedix(1,0)) snake.move(); break;//right
case 40: if (snake.is_chuangedix(0, 1)) snake.move(); break;//down
case 13: snake.start(); timer1.Enabled = true ; break; //enter
}
draw();
}
private void timer1_Tick(object sender, EventArgs e)
{
d++;
if (snake.snake_long > 5)
{
snake.level++;
snake.start();
}
if (timer1.Interval > 40)
timer1.Interval = 200 - snake.level * 40;
else
timer1.Interval = 1;
Levellabel.Text = snake.level.ToString();
if (snake.is_move())
{
snake.move();
if (d % 10 == 0)
snake.set_foodtone();
}
else
{
snake.GameOver();
timer1.Enabled = false;
timer1.Interval = 300;
}
draw();
}
}//end Form
}