| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 515 人关注过本帖
标题:求助ing~哪位大虾帮我看一下这个贪食蛇代码
只看楼主 加入收藏
上帝活在心中
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-7-21
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
求助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
}
搜索更多相关主题的帖子: head star public Random direct 
2012-07-22 18:16
上帝活在心中
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-7-21
收藏
得分:0 
求助啊  报错好多~  来个高手帮忙改一下  感激不尽
2012-07-22 18:17
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:20 
说实话,这个写的真的有点2,运行起来到是很容易,不过BUG很多。
1、首先窗体设计器上要有三个东西: 一个是Lable控件,该控件的Name需要改为Levellabel;第二个是PictureBox,该控件的Name为gamePictureBox;第三个是Timer,该控件的Name为timer1
2、关联事件,从代码中可以看出,总共有两个事件,一个是Form1_KeyDown,在窗体Form1的事件中,找到KeyDown,在下拉框里选就可以了;同样Timer也有一个事件,timer1_Tick
3、所有map变量报错的地方都改为map[i, j]就可以了,因为map是个二维数组,要指定下标,从上面代码来看,下标都是i和j
程序运行起来以后,按回车就可以了
2012-07-22 22:13
上帝活在心中
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-7-21
收藏
得分:0 
回复 3楼 yhlvht
隔了这么久才回来看一下 真是惭愧==
谢谢啦
2013-03-17 21:02
快速回复:求助ing~哪位大虾帮我看一下这个贪食蛇代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027775 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved