问题描述
为什么在keydown里,keys.up,keys.down等不能使用
解决方案
解决方案二:
可以的,可能是其他原因,再说详细点吧.
解决方案三:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespacesnake{publicpartialclassForm1:Form{intsnakedirection=2;intx=1;inty=1;intx1=1;inty1=1;publicintwidth=10;List<Point>postions=newList<Point>();List<Point>foodpostions=newList<Point>();publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){this.BackColor=Color.Black;//postions.Add(newPoint(x,y));timer1.Enabled=true;postions.Add(newPoint(x,y));foodpostions.Add(newPoint(x,y));}privatevoidbutton1_Click(objectsender,EventArgse){//postions.Add(newPoint(1,1));Randomnum=newRandom();for(inti=1;i<=foodpostions.Count;i++){x1=num.Next(1500/width);y1=num.Next(1600/width);}Graphicsg=CreateGraphics();Penp2=newPen(Color.Pink);Brushbrush1=p2.Brush;g.FillRectangle(brush1,x1,y1,width,width);}privatevoidtimer1_Tick(objectsender,EventArgse){timer1.Interval=500;if(Enabled==true){Graphicsg1=CreateGraphics();Penp1=newPen(Color.Red);Penp2=newPen(Color.Black);Brushbrush1=p1.Brush;Brushbrush2=p2.Brush;postions.Add(newPoint(x,y));switch(snakedirection){case1:y=postions[postions.Count-1].Y-width;break;case2:x=postions[postions.Count-1].X+width;break;case3:y=postions[postions.Count-1].Y+width;break;case4:x=postions[postions.Count-1].X-width;break;default:break;}g1.FillRectangle(brush2,postions[postions.Count-2].X,postions[postions.Count-2].Y,width,width);g1.FillRectangle(brush1,postions[postions.Count-1].X,postions[postions.Count-1].Y,width,width);}}privatevoidForm1_KeyDown(objectsender,KeyEventArgse){switch(e.KeyCode){caseKeys.Up://向上走snakedirection=1;break;caseKeys.Down://向下走snakedirection=3;break;caseKeys.Left://;向左走snakedirection=4;break;caseKeys.Right://向右走snakedirection=2;break;default:break;}}}
解决方案四:
现在是按上下左右键没反应呀
解决方案五:
对的,我也遇到这个问题了,求解释。。。
解决方案六:
好像把KeyDown事件改为KeyUp事件就好了
解决方案七:
确认:Form1的“Keypreview”属性是否为true!
解决方案八:
看到你自创建ID来共29篇帖子无一结贴,我得到以下结论:一个只知道索取不知道回报的家伙!
解决方案九:
确认一下你的窗口事件里KeyDown事件是否有加上不。。。