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;namespace Happy_Snake
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private palette p;
        private void Form2_Load(object sender, EventArgs e)
        {
            //定义画布长宽和每个蛇块的大小
            int width, height, size;
            width = height = 20;
            size = 15;
            //设置游戏窗口大小
            this.pictureBox1.Width = width * size;
            this.pictureBox1.Height = height * size;
            this.Width = pictureBox1.Width + 30;
            this.Height = pictureBox1.Height + 60;
            //定义一个新画布(宽度,高度,单位大小,背景色,绘图句柄,游戏等级)
            p = new palette(width, height, size, this.pictureBox1.BackColor, Graphics.FromHwnd(this.pictureBox1.Handle), 5);
            p.Start();//开始游戏
        }
        //改变方向 不能反方向移动 90°转弯
        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.W || e.KeyCode == Keys.Up) && p.dir == Direction.Down)
            {
                p.dir = Direction.Up;
                return;
            }
            if ((e.KeyCode == Keys.D || e.KeyCode == Keys.Right) && p.dir == Direction.Left)
            {
                p.dir = Direction.Right;
                return;
            }
            if ((e.KeyCode == Keys.S || e.KeyCode == Keys.Down) && p.dir == Direction.Up)
            {
                p.dir = Direction.Down;
                return;
            }
            if ((e.KeyCode == Keys.A || e.KeyCode == Keys.Left) && p.dir == Direction.Right)
            {
                p.dir = Direction.Left;
                return;
            }        }
        //重绘
        public void pictureBox1_Paint(object sender, PaintEventArgs e)
        {            if (p != null)
            {
                p.PaintPalette(e.Graphics);
            }
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
      }    }using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;namespace Happy_Snake
{
    //记录每个蛇小块的坐标,颜色,大小
    class Snake
    {
        private Color color;//颜色
        private int sizi;//单位大小
        private Point point;//坐标
      
        public Snake(Color color,int sizi,Point p)//point p记录血块坐标
        {
            this.color = color;
            this.sizi = sizi;
            this.point = p;
        }        public Point Point
        {
            get { return this.point; }
        }
        //绘制自身到画幕
        public virtual void Paint(Graphics g)
        {
            SolidBrush sb = new SolidBrush(color);
            lock (g)
            {
                try
                {
                    g.FillRectangle(sb,
                        this.Point.X * this.sizi,
                        this.Point.Y* this.sizi,
                        this.sizi - 1,
                        this.sizi - 1);
                }
                catch
                { }
            }
        }    }
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Timers;namespace Happy_Snake
{
    //枚举四个方向
    public enum Direction
    {
        Left,
        Right,
        Up,
        Down
    }    //定义画布的大小,背景色,血块列表,游戏速度,移动方向
    class palette
    {
        private int width = 20;//宽度
        private int height = 20;//高度
        private Color bgColor;//背景色
        private Graphics gpPalette;//画布
        private ArrayList snakes;//蛇块列表
        private Direction direction;// 前进方向
        private Timer timerSnake;//更新器
        private Snake Food;//当前食物
        private int sizi=20;//单位大小
        private int level=1;//游戏等级
        private bool isGameOver = false;        private int[] speed = new int[] { 500, 450, 400, 350, 300, 250, 200, 150, 100, 50 };//游戏速度
        //构造函数
        public palette(int width, int height, int sizi, Color byColor, Graphics g, int lvl)
        {
            this.width = width;
            this.height = height;
            //this.bgcolor = bgcolor;
            this.gpPalette = g;
            this.sizi = sizi;
            this.level = lvl;
            this.snakes = new ArrayList();
            this.snakes.Insert(0, (new Snake(Color.Red, this.sizi,new Point(width / 2, height / 2))));//画布中央画出一个只有一节的蛇
            this.direction = Direction.Right;
        }        public Direction dir;        //开始游戏
        public void Start()
        {
            this.Food = GetFood();//生成一个吃的食物
            timerSnake = new System.Timers.Timer(speed[this.level]);
            timerSnake.Elapsed += new System.Timers.ElapsedEventHandler(onBlockTimedEvent);
            timerSnake.AutoReset = true;
            timerSnake.Start();        }
        //定时更新 onBlockTimedEvent函数更新血块信息列表
        private void onBlockTimedEvent(object source, ElapsedEventArgs e)
        {
            this.Move();//前进一个单位
            if (this.CheckDead())
            {
                this.timerSnake.Stop();
                this.timerSnake.Dispose();
                System.Windows.Forms.MessageBox.Show("Score" + this.snakes.Count, "游戏结束");
            }
        }
        //检查是否游戏结束
        private bool CheckDead()
        {
            Snake head = (Snake)(this.snakes[0]);//取蛇块列表的第一个当蛇头
            //检测是否超出画布范围
            if (head.Point.X < 0 || head.Point.Y < 0 || head.Point.X >= this.width || head.Point.Y >= this.height)
                return true;
            for (int i = 1; i < this.snakes.Count; i++)//检测是否撞到自己
            {
                Snake S = (Snake)this.snakes[i];
                if (head.Point.X == S.Point.X && head.Point.Y == S.Point.Y)
                {
                    this.isGameOver = true;
                    return true;
                }
            }
            this.isGameOver = false;
            return false;
        }
        //生成下个血块,也就是下一个蛇块
        private Snake GetFood()
        {
            Snake food = null;
            Random r = new Random();
            bool redo = false;
            while (true)
            {
                redo = false;
                int x = r.Next(this.width);
                int y = r.Next(this.height);
                for (int i = 0; i < this.snakes.Count; i++)//用for循环检测食物所在坐标是否和蛇块冲突
                {
                    Snake s = (Snake)(this.snakes[i]);
                    if (s.Point.X == x && s.Point.Y == y)//如果冲突在随机找个坐标
                    {
                        redo = true;
                    }
                }
                if (redo == false)
                {
                    food = new Snake(Color.Black, this.sizi,new  Point(x, y));
                    break;
                }
            }
            return food;
        }
        //前进一节
        private void Move()
        {
            Point p;//下一个坐标位置
            Snake head = (Snake)(this.snakes[0]);
            if (this.direction == Direction.Up)
                p = new Point(head.Point.X, head.Point.Y - 1);
            else if (this.direction == Direction.Down)
                p = new Point(head.Point.X, head.Point.Y + 1);
            else if (this.direction == Direction.Left)
                p = new Point(head.Point.X - 1, head.Point.Y);
            else
                p = new Point(head.Point.X + 1, head.Point.Y);
            //生成新坐标将来成为蛇头
            Snake b = new Snake(Color.Red, this.sizi,p);
            //如果下个下一个坐标不是当前食物坐标,那么从蛇块信息列表中删除最后一个蛇块
            if (this.Food.Point != p)
                this.snakes.RemoveAt(this.snakes.Count - 1);
            //如果下个坐标和食物坐标重合,那么就生成一个新食物
            else
                this.Food = this.GetFood();
            //把下一个坐标插入蛇块信息的第一个,将其称为蛇头
            this.snakes.Insert(0, p);
            this.PaintPalette(this.gpPalette);//更新画板        }
        //PaintPalette函数 更新画板
        public void PaintPalette(Graphics gp)
        {
            gp.Clear(this.bgColor);
            this.Food.Paint(gp);
            foreach (Snake s in this.snakes)
            {
                s.Paint(gp);
            }
        }
       
   }
}
 
求大神帮帮我解决哈代码要抛异常求解答谢谢。

解决方案 »

  1.   

    为什么你解决不了问题,是因为你解决问题的方式有问题。
    你贴出那么多代码,却无法说出问题在哪里,甚至连一个大致的范围你也无法确定,而且连问题是什么也描述不清楚,这说明你根本没有动脑筋。
      

  2.   

    这、、你应该下几个资源自己改改,然后你发现你差不多就自己解决一些问题了/
      

  3.   

    明确异常在哪,不想看代码,乱