问题描述
是一个贪吃蛇游戏出问题的是snake类和Obstacle类usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.IO;namespacetheSnake{publicpartialclassForm1:Form{publicstructnode{publicintx;publicinty;}//方向枚举enumdirect:int{up=1,down,left,right}publicclassSettings{publicstaticstringfloorType{get;set;}publicstaticintheadx{get;set;}publicstaticintheady{get;set;}publicstaticintdirection{get;set;}}Snakesnake;Obstacleobstacle;publicLabel[,]lbl=newLabel[20,20];intinterval=500;//timer间隔-->控制蛇的速度intdirection=Settings.direction;//移动方向,右intcurrentLength=3;//当前蛇长intcntGrid=20;//格子数intgridWidth=15;boolisGameOver=true;//游戏是否结束//boolisOkInput=true;publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){initGrid();comboBox1.Text=comboBox1.Items[0].ToString();initSettings(comboBox1.Text);snake=newSnake(lbl);snake.initSnake(lbl);obstacle=newObstacle(lbl);//obstacle.initObstacle(lbl);}//初始化设置voidinitSettings(stringmapType){stringsPath=Application.StartupPath;stringline="";string[]parts;StreamReadersr=newStreamReader(sPath+"\maze.txt",Encoding.Default);//初始化setsSettings.headx=Settings.heady=Settings.direction=0;Settings.floorType="";while(!sr.EndOfStream){line=sr.ReadLine();parts=line.Split(',');if(parts[0]==("""+mapType+"""))//记得转义字符!{Settings.floorType=mapType;Settings.heady=int.Parse(parts[1]);Settings.headx=int.Parse(parts[2]);Settings.direction=int.Parse(parts[3]);}}sr.Close();}//初始化格子privatevoidinitGrid(){this.panel1.Controls.Clear();for(inti=0;i<cntGrid;i++){for(intj=0;j<cntGrid;j++){//主要方法:system.drawinglbl[i,j]=newSystem.Windows.Forms.Label();lbl[i,j].Location=newSystem.Drawing.Point(i*(gridWidth+3)+10,j*(gridWidth+3)+10);lbl[i,j].Size=newSystem.Drawing.Size(gridWidth,gridWidth);lbl[i,j].BackColor=Color.Silver;//背景格子颜色this.panel1.Controls.Add(lbl[i,j]);//主要方法:在panel里画label}}lbl[1,2].BackColor=Color.Red;}privatevoidcomboBox1_TextChanged(objectsender,EventArgse){initSettings(comboBox1.Text);obstacle=newObstacle(lbl);//snake=newSnake(lbl);如果在这初始化就没事obstacle.initObstacle(lbl);snake.initSnake(lbl);//此处出错}privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse){initSettings(comboBox1.Text);obstacle=newObstacle(lbl);snake=newSnake(lbl);//obstacle.initObstacle(lbl);snake.initSnake(lbl);}}publicclassSnake{//蛇身节点publicstructnode{publicintx;publicinty;}publicintlength{get;set;}//蛇长度publicLinkedList<node>snake;//蛇身所有节点publicLinkedListNode<node>searchNode;//查找节点(指针!)publicnodenewNode;//新节点,加入头部publicLinkedListNode<node>tailNode;//尾节点,用于删除Randomrnd;//随机函数publicintcntGrid;//格子个数publicboolisSnakeAlive;//蛇是否活着publicLinkedList<node>obstacle;//所有障碍物publicnodenewObstacle;//新障碍物publicnodestartHead;//蛇初始化位置//蛇头坐标publicclasshead{publicstaticintx{get;set;}//设置属性读取{get;set;}publicstaticinty{get;set;}}//食物类publicclassfood{publicstaticintx;//食物的x坐标publicstaticinty;//食物的y坐标publicstaticboolisFoodAte;//是否被吃}//蛇类主构造函数publicSnake(Label[,]lbl){cntGrid=20;//格子数cnGrid*cnGridlength=3;//蛇长度rnd=newRandom();//随机数food.isFoodAte=true;//蛇身链表snake=newLinkedList<node>();//障碍物链表obstacle=newLinkedList<node>();//初始化3个新的节点nodes1,s2,s3;s1.y=s2.y=s3.y=Form1.Settings.heady;s1.x=Form1.Settings.headx-2;s2.x=Form1.Settings.headx-1;s3.x=Form1.Settings.headx;//加入蛇身链表snake.AddFirst(s1);snake.AddFirst(s2);snake.AddFirst(s3);lbl[s1.x,s1.y].BackColor=Color.FromArgb(150,206,84);//画蛇身lbl[s2.x,s2.y].BackColor=Color.FromArgb(150,206,84);head.x=s3.x;head.y=s3.y;lbl[head.x,head.y].BackColor=Color.FromArgb(12,137,24);//画蛇头isSnakeAlive=true;}publicvoidinitSnake(Label[,]lbl){//清除已有的蛇、食物searchNode=snake.First;//找到头结点while(searchNode!=snake.Last){lbl[searchNode.Value.x,searchNode.Value.y].BackColor=Color.Silver;searchNode=searchNode.Next;}lbl[searchNode.Value.x,searchNode.Value.y].BackColor=Color.Silver;snake.Clear();lbl[food.x,food.y].BackColor=Color.Silver;//创建新的蛇、食物food.isFoodAte=true;length=5;//初始时蛇身长度为3nodes1,s2,s3;s1.y=s2.y=s3.y=Form1.Settings.heady;s1.x=Form1.Settings.headx-2;s2.x=Form1.Settings.headx-1;s3.x=Form1.Settings.headx;//s1.y=s2.y=s3.y=startHead.y;//s1.x=startHead.x-2;s2.x=startHead.x-1;s3.x=startHead.x;snake.AddFirst(s1);snake.AddFirst(s2);snake.AddFirst(s3);lbl[s1.x,s1.y].BackColor=Color.FromArgb(150,206,84);//画蛇身lbl[s2.x,s2.y].BackColor=Color.FromArgb(150,206,84);head.x=s3.x;head.y=s3.y;lbl[head.x,head.y].BackColor=Color.FromArgb(12,137,24);//画蛇头isSnakeAlive=true;}}publicclassObstacle{publicstructnode{publicintx;publicinty;}publicLinkedList<node>obstacle;//障碍物链表publicLinkedListNode<node>searchNode;//查找节点(指针!)publicnodenewNode;//新节点,加入头部publicLinkedListNode<node>tailNode;//查找尾节点publicObstacle(Label[,]lbl){obstacle=newLinkedList<node>();initObstacle(lbl);//初始化障碍物}//初始化障碍物publicvoidinitObstacle(Label[,]lbl){stringsPath=Application.StartupPath;stringline="";string[]parts;//清空原地图searchNode=obstacle.First;//找到头结点while(searchNode!=obstacle.Last){lbl[searchNode.Value.x,searchNode.Value.y].BackColor=Color.Silver;searchNode=searchNode.Next;}if(searchNode!=null){lbl[searchNode.Value.x,searchNode.Value.y].BackColor=Color.Silver;}obstacle.Clear();//清空障碍物链表//画地图StreamReadersr=newStreamReader(sPath+"\maze.txt",Encoding.Default);while(!sr.EndOfStream){line=sr.ReadLine();parts=line.Split(',');if(parts[0]==("""+Form1.Settings.floorType+"""))//记得转义字符!{for(inti=0;i<20;i++){stringline2=sr.ReadLine();string[]parts2=line2.Split(',');for(intj=0;j<20;j++){if(parts2[j]=="1"){//为障碍物lbl[j,i].BackColor=Color.FromArgb(178,93,37);//画障碍物newNode.x=j;newNode.y=i;obstacle.AddFirst(newNode);}}}}}sr.Close();}}}
解决方案
本帖最后由 sinat_32612651 于 2015-12-23 16:32:59 编辑
解决方案二:
comboBox1.Text=comboBox1.Items[0].ToString();这句丢到初始化后面执行
解决方案三:
未将对象引用设置到对象的实例,无外乎有个东东为null呗。目测窗体载入事件,comboBox1的TextChanged事件触发时,snake还为实例化(也就是为null)。
解决方案四:
窗体构造调用InitializeComponent()方法时会触发comboBox1_SelectedIndexChanged这个事件,此时你有些对象还未初始化就会导致null引用,另外你这句comboBox1.Text=comboBox1.Items[0].ToString();有点醉,是不是太绕了?
解决方案五:
你在最外面这么声明就可以了Snakesnake=newSnake();因为你一开始只是Snakesnake这个等价于Snakesnake=null而null并不是实例化的意思