当前位置:编程学习 > C#/ASP.NET >>

跪求大神帮忙,刚学c#,学着作一个贪吃蛇的小游戏,但是出现下列情况,跪求大神帮忙啊


              
                
                
--------------------编程问答--------------------

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

namespace 小黄人贪吃蛇
{
    public partial class Form1 : Form
    {
        private Palette p;
        public Form1()
        {
            InitializeComponent();
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (p != null)
            {
                p.PaintPalette(e.Graphics);
            }
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.W || e.KeyCode == Keys.Up) && p.Direction != Direction.Down)
            {
                p.Direction = Direction.Up;
                return;
            }
            if ((e.KeyCode == Keys.D || e.KeyCode == Keys.Right) && p.Direction != Direction.Left)
            {
                p.Direction = Direction.Right;
                return;
            }

            if ((e.KeyCode == Keys.A || e.KeyCode == Keys.Left) && p.Direction != Direction.Right)
            {
                p.Direction = Direction.Left;
                return;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void Form1_Load_1(object sender, EventArgs e)
        {
        }
        private void 重置ToolStripMenuItem_Click(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();
        }
        private void 开始ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            p.StartStop();
        }

        private void 结束游戏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            p.Stop();
            int a = p._blocks.Count - 1;
            System.Windows.Forms.MessageBox.Show("得分:" + a + "\n\n退出游戏!", "提示");
            this.Close();
        }
--------------------编程问答-------------------- p你定义的是全局变量,你实例的却是局部实例化,所以到这个方法里它就会报错没有实例化,你看看我的猜想对不对 --------------------编程问答-------------------- 上面的是代码 --------------------编程问答-------------------- 我都说了你才贴代码,看来我的猜想是对的了,你把你的p的实例化放到上面去就行了 --------------------编程问答-------------------- 大神能具体点吗?我有点晕 --------------------编程问答-------------------- public partial class Form1 : Form
    {
        private Palette p;
        public Form1()
        {
            InitializeComponent();
        }
就是你这里定义的p是全局变量,但是你实例化却只是在
   private void 重置ToolStripMenuItem_Click(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();
        }
这里局部实例化,所以别的地方调用就会报未实例化
你要在这
public partial class Form1 : Form
    {
        private Palette p;
        public Form1()
        {
            InitializeComponent();
        }
实例化你这个p下面调用才不会报错的!不知道这样够清楚了没 --------------------编程问答-------------------- 比如具体如何实例化呢? --------------------编程问答--------------------
 private Palette p = new Palette(width, height, size, this.pictureBox1.BackColor, Graphics.FromHwnd(this.pictureBox1.Handle), 5);
就是把你下面的实例话P的那句话调到上面而已!下面那句就删了! --------------------编程问答-------------------- --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 小黄人贪吃蛇
{
    public partial class Form1 : Form
    {
        private Palette p = new Palette(width, height, size, this.pictureBox1.BackColor, Graphics.FromHwnd(this.pictureBox1.Handle), 5);
        public Form1()
        {
            InitializeComponent();
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (p != null)
            {
                p.PaintPalette(e.Graphics);
            }
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.W || e.KeyCode == Keys.Up) && p.Direction != Direction.Down)
            {
                p.Direction = Direction.Up;
                return;
            }
            if ((e.KeyCode == Keys.D || e.KeyCode == Keys.Right) && p.Direction != Direction.Left)
            {
                p.Direction = Direction.Right;
                return;
            }

            if ((e.KeyCode == Keys.A || e.KeyCode == Keys.Left) && p.Direction != Direction.Right)
            {
                p.Direction = Direction.Left;
                return;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void Form1_Load_1(object sender, EventArgs e)
        {
        }
        private void 重置ToolStripMenuItem_Click(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.Start();
        }
        private void 开始ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            p.StartStop();
        }

        private void 结束游戏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            p.Stop();
            int a = p._blocks.Count - 1;
            System.Windows.Forms.MessageBox.Show("得分:" + a + "\n\n退出游戏!", "提示");
            this.Close();
        }

        private void 暂停ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            p.Stop();
            int a = p._blocks.Count - 1;
            System.Windows.Forms.MessageBox.Show("得分:" + a, "游戏暂停!");
        }
    }
}

        --------------------编程问答-------------------- 大神,还是不对
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,