主要是利用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 { }
}
谢谢了啊~~~~
snake_block[i].block_point = snake_block[i - 1].block_point;
应该把相应节点的点坐标信息相等就好了
简单的说,snake_block[i]是类对象,直接赋值的话是引用赋值,那样最后,数组里节点全是头的引用
arraylist 这种数据结构 好像不太合适做这个吧
弄一个单链表应该好些 类似这样
public class Node
{
public Node PreNode{get;set;}
public Node NextNode{get;set;}
}
具体方法懒得写了,楼主自己补充哈。