首页 新闻 会员 周边 捐助

C#编写贪吃蛇时遇到的问题,纠结死了,帮帮我啊

0
悬赏园豆:50 [已解决问题] 解决于 2009-12-15 20:09

主要是利用arraylist来存储蛇身,删除蛇尾的方块,再根据方向加上蛇头的方块

或者把前一个方块的坐标传给后一个方块,在按方向加上蛇头

但是不管哪个方法结果都是一样,就是除了蛇头以外,其后面的方块依次消失,最后就剩下一个蛇头在移动了

我想了半天还是不知道哪里出问题了,纠结死了啊!!!~~

我是初学者,请各位大哥大姐帮帮忙~~

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WindowsFormsApplication74
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Snake snake
=new Snake();
int direct = 0;
bool key = false;
private void timer1_Tick(object sender, EventArgs e)
{
switch(direct)
{
case 1: snake.snake_moveup(); break;
case 2: snake.snake_movedown(); break;
case 3: snake.snake_moveleft(); break;
case 4: snake.snake_moveright(); break;
}
Refresh();
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if(key)
{
Graphics g
= e.Graphics;
snake.snake_draw(g);
}
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up) { direct = 1; }
if (e.KeyCode == Keys.Down) { direct = 2; }
if (e.KeyCode == Keys.Left) { direct = 3; }
if (e.KeyCode == Keys.Right) { direct = 4; }
}

private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
{
key
= true;
timer1.Enabled
= true;
direct
= 3;
Refresh();
}

}
class Block
{
public Point block_point;
Size size
= new Size(5,5);
public void block_draw(Graphics g)
{
Rectangle rec
= new Rectangle(block_point,size);
SolidBrush sb
=new SolidBrush(Color.Red);
g.FillRectangle(sb,rec);
}
}
class Snake
{
public ArrayList snake_block = new ArrayList(4);//用于存储方块的数组
public Snake() //构造函数
{
int x = 400, y = 100;
for (int i = 0; i < 4;i++ )
{
Block block
= new Block();
block.block_point
= new Point(x,y);
snake_block.Add(block);
x
+= 5;
}
}
public void snake_draw(Graphics g)//调用Block中的Draw方法绘制Snake
{
for (int i = 0; i < snake_block.Count;i++ )
{
Block block
=(Block)snake_block[i];
block.block_draw(g);
}

}
public void snake_moveup()
{
for (int i = snake_block.Count - 1; i >= 1; i--)
{
snake_block[i]
= snake_block[i - 1];
}
Block block_temp
= (Block)snake_block[0];
block_temp.block_point
= new Point(block_temp.block_point.X, block_temp.block_point.Y - 5);
snake_block[
0] = block_temp;
}
public void snake_movedown()
{
for (int i = snake_block.Count - 1; i >= 1; i--)
{
snake_block[i]
= snake_block[i - 1];
}
Block block_temp
= (Block)snake_block[0];
block_temp.block_point
= new Point(block_temp.block_point.X , block_temp.block_point.Y+5);
snake_block[
0] = block_temp;
}
public void snake_moveleft()
{
for (int i = snake_block.Count - 1; i >= 1;i-- )
{
snake_block[i]
= snake_block[i - 1];
}
Block block_temp
=(Block)snake_block[0];
block_temp.block_point
= new Point(block_temp.block_point.X-5,block_temp.block_point.Y);
snake_block[
0] = block_temp;
}
public void snake_moveright()
{
for (int i = snake_block.Count - 1; i >= 1; i--)
{
snake_block[i]
= snake_block[i - 1];
}
Block block_temp
= (Block)snake_block[0];
block_temp.block_point
= new Point(block_temp.block_point.X +5, block_temp.block_point.Y);
snake_block[
0] = block_temp;
}
}
class Food { }
}
整个程序的源代码在这里

谢谢了啊~~~~

月关的主页 月关 | 初学一级 | 园豆:150
提问于:2009-12-15 11:29
< >
分享
最佳答案
0

snake_block[i].block_point = snake_block[i - 1].block_point;
应该把相应节点的点坐标信息相等就好了

简单的说,snake_block[i]是类对象,直接赋值的话是引用赋值,那样最后,数组里节点全是头的引用

收获园豆:30
沐浴橙光 | 菜鸟二级 |园豆:255 | 2009-12-15 13:04
确实是引用赋值的问题 即使用arraylist[i]去赋值给一个新的block类仍旧是引用赋值,修改这个新的BLOCK类还是会对arraylist里面的数据产生影响,再加上我是从arraylist的末尾开始循环的~~ 谢谢了,给了我很大的帮助!!
月关 | 园豆:150 (初学一级) | 2009-12-15 20:06
snake_block[i].block_point = snake_block[i - 1].block_point; 但是好像无法像这个样子进行赋值的啊~~
月关 | 园豆:150 (初学一级) | 2009-12-15 20:08
其他回答(1)
0

arraylist 这种数据结构 好像不太合适做这个吧

弄一个单链表应该好些  类似这样

public class Node

{

  public Node PreNode{get;set;}

  public Node NextNode{get;set;}

}

具体方法懒得写了,楼主自己补充哈。

收获园豆:20
Cheese | 园豆:509 (小虾三级) | 2009-12-15 12:41
链表也很好,但是我想练习一下关于arraylist的相关知识,谢谢你的帮忙了~~
支持(0) 反对(0) 月关 | 园豆:150 (初学一级) | 2009-12-15 20:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册