永发信息网

我想用C#做个游戏,其中人物要会移动,怎么做到,哪里有相关的代码下载吗

答案:3  悬赏:30  手机版
解决时间 2021-02-15 10:20
  • 提问者网友:感性作祟
  • 2021-02-14 10:23
我想用C#做个游戏,其中人物要会移动,怎么做到,哪里有相关的代码下载吗
最佳答案
  • 五星知识达人网友:等灯
  • 2021-02-14 11:27
你这涉及到图像编程领域了,去微软的网站看关于DirectX的文档吧,那里还有一些示例下载。
全部回答
  • 1楼网友:舍身薄凉客
  • 2021-02-14 14:14
改变“人物”的坐标
  • 2楼网友:傲气稳了全场
  • 2021-02-14 12:40
贪吃蛇 using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; using system.collections; namespace windowsapplication1 { public partial class form1 : form { public form1() { initializecomponent(); } private bool i;//开关 snake a_snake = new snake(5);//实例化个长度为5的蛇 food afood = new food();//实例化一个食物 private void picturebox1_paint(object sender, painteventargs e) { graphics g = e.graphics; if (i)//点击了开始button { a_snake.drawsnake(g);//画出蛇 afood.drawfood(g);//画出来食物 } if (a_snake.deadsnake())//如果蛇死亡事件为真 { timer1.enabled = false;//timer控件停止 if (dialogresult.yes == messagebox.show("game over", "是否重新开始?", messageboxbuttons.yesno)) //messagebox消息 { //点击确定后重新开始游戏 button1.enabled = true; a_snake = new snake(5);//初始化蛇 afood = new food();//初始化食物 i = false;//开关为假 g.clear(picturebox1.backcolor);//清理picturebox } else application.exit();//关闭程序 } } private void button1_click(object sender, eventargs e) { i = true; //开关为真 afood.f_point = afood.getpoint();//产生一个食物的随机坐标 picturebox1.refresh();//刷新picturebox timer1.enabled = true;//开启timer控件 timer1.interval = 100; //时间间隔为0.1秒 button1.enabled = false; } private void timer1_tick(object sender, eventargs e) { picturebox1.refresh();//刷新picturebox a_snake.extendsnake();//蛇伸长一节 if (a_snake.headpoint == afood.f_point) afood.f_point = afood.getpoint();//蛇头坐标与食物相同 //就只伸长 else a_snake.contractsnake();//没吃到食物就缩短一节 } private void form1_keydown(object sender, keyeventargs e) { if (e.keycode == keys.w && a_snake.way != 2) { a_snake.way = 0; } if (e.keycode == keys.d && a_snake.way != 3) { a_snake.way = 1; } if (e.keycode == keys.s && a_snake.way != 0) { a_snake.way = 2; } if (e.keycode == keys.a && a_snake.way != 1) { a_snake.way = 3; } //设置keydown事件,用按件控制方向 } } public class segment//[一节蛇]类 { private int number;//私有成员 public int number//[一节蛇]的编号 { get { return number; } set { number = value; } } private point orign; public point orign//[一节蛇]的坐标 { get { return orign; } set { orign = value; } } public void drawpart(graphics g)//画[一节蛇] { pen p = new pen(color.red); g.drawellipse(p, orign.x, orign.y, 10, 10); //画红色圆圈代表一节蛇 } } public class snake//蛇类 { arraylist alist;//定义一个先进先出的数据表 public point headpoint;//蛇头坐标 private int way = 1;//初始方向为右 public int way { get { return way; } set { way = value; } } public snake(int count)//蛇的构造函数 { segment apart; point apoint = new point(30, 30);//初始位置 alist = new arraylist(count);//初始化数据表 for (int i = 1; i <= count; i++) { apoint.x = apoint.x + 10; apart = new segment(); apart.number = i; apart.orign = apoint; alist.add(apart);//将没节蛇存在表里面 if (i == count)//将最后的一节蛇定为蛇头 headpoint = apoint; } } public void drawsnake(graphics g)//画蛇 { for (int i = 0; i < alist.count; i++) { segment seg = (segment)alist[i]; seg.drawpart(g); } } public void extendsnake()//伸长一节蛇 { segment seg = new segment(); seg.number = alist.count + 1; point p; if (way == 0) p = new point(headpoint.x, headpoint.y - 10); else if (way == 2) p = new point(headpoint.x, headpoint.y + 10); else if (way == 3) p = new point(headpoint.x - 10, headpoint.y); else p = new point(headpoint.x + 10, headpoint.y); seg.orign = p; alist.add(seg);//将新的一节蛇添加到表尾 headpoint = seg.orign;//重新设蛇头 } public void contractsnake()//蛇缩短一节 { alist.remove(alist[0]);//删除表的第一个元素 } public bool deadsnake()//射死亡事件 { if (headpoint.x < 0 || headpoint.y < 0 || headpoint.x > 350 || headpoint.y > 270) //判断是否撞墙了 return true; for (int i = 0; i < alist.count - 1; i++) { segment seg = (segment)alist[i]; if (seg.orign == headpoint)//判断是否咬到自己 return true; } return false; } } public class food//食物类 { private point f_point; public point f_point//食物的坐标 { get { return f_point; } set { f_point = value; } } public void drawfood(graphics g)//画食物 { solidbrush b = new solidbrush(color.blue); rectangle rtg = new rectangle(f_point.x, f_point.y, 10, 10); g.fillrectangle(b, rtg); //实心的蓝色方块 } public point getpoint()//获得食物坐标[随机数point] { int i = 10; random rdm = new random(system.datetime.now.millisecond + i); i = rdm.next(0, 27); int j = rdm.next(0, 27); point newp = new point(i * 10, j * 10); return newp; } } } 下一百层 using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; using system.collections; namespace windowsapplication1 { public partial class form1 : form { public form1() { initializecomponent(); } lift alift = new lift();//梯子实例化 people man = new people();//人物实例化 bool i;//开关 int j =1;//人物移动方向 private void picturebox1_paint(object sender, painteventargs e) { graphics g = e.graphics; if (i) { alift.drawlift(g);//画梯子 man.drawpeople(g);//画人物 } if (man.mandead())//人物死亡为真 { timer1.enabled = false; timer2.enabled = false; timer3.enabled = false; timer4.enabled = false; if (dialogresult.yes == messagebox.show("game over", "重新开始游戏?", messageboxbuttons.yesno)) { //重新开始游戏 button1.enabled = true; man.footpoint = new point(alift.downmost.x + 50, alift.downmost.y - 20); } else application.exit();//退出游戏 } } private void button1_click(object sender, eventargs e) { i = true;//打开开关 picturebox1.refresh();//刷新 timer1.interval = 2; timer1.enabled = true; timer3.interval = 1; timer3.enabled = true; man.footpoint = new point(alift.downmost.x + 50, alift.downmost.y -20); //初始化任务的坐标 button1.enabled = false;//button1被锁 } private void timer1_tick(object sender, eventargs e) { alift.liftrise();//伸梯子 if (alift.downmost.y <= 260) alift.addstep();//加梯子 picturebox1.refresh(); } private void form1_keydown(object sender, keyeventargs e) { //用a控制向左,s控制向右 if (e.keycode == keys.a) { timer2.interval = 1; timer2.enabled = true; j = 2; } if (e.keycode == keys.s) { timer2.interval = 1; timer2.enabled = true; j = 3; } } private void form1_keyup(object sender, keyeventargs e) { //keyup事件控制人物左右移动的停止 if (e.keycode == keys.a) { timer2.enabled = false ; j = 1; } if (e.keycode == keys.s) { timer2.enabled = false ; j = 1; } } private void timer2_tick(object sender, eventargs e) { if (j == 2) man.moveleft();//人物向左移动 else if (j == 3) man.moveright();//人物向右移动 } private void timer3_tick(object sender, eventargs e) { man.movedown();//人物下落 if (alift.manland(man)) { timer3.enabled = false; timer4.interval = 2; timer4.enabled = true; } } private void timer4_tick(object sender, eventargs e) { man.moverise();//人物随梯子上升 if (alift.manout(man)) { timer3.enabled = true; timer4.enabled = false; } } } public class step//台阶类是梯子的一个单位 { private point safep;//台阶的坐标 public point safep { get { return safep; } set { safep = value; } } public void drawstep(graphics g)//画台阶[实心长方形] { solidbrush b = new solidbrush(color.darkslateblue); rectangle rect = new rectangle(safep.x, safep.y, 100, 10); g.fillrectangle(b, rect); } public int getpointx(int i)//台阶的随机x坐标 { random rdm = new random(system.datetime.now.millisecond+i); return rdm.next(0,240); } public point risepoint()//新伸起来的台阶坐标 { random rdm = new random(system.datetime.now.millisecond*123456 ); int x= rdm.next(0, 240); point p = new point(x, 340); return p; } } public class lift//梯子类 { public arraylist alist = new arraylist(5);//先进先出表 public point downmost;//最下面台阶的坐标 public lift()//构造函数 { step astep; int x=1,y=10; for (int i = 0; i < 5; i++) { astep = new step(); x = astep.getpointx(x); astep = new step(); astep.safep =new point(x, y); alist.add(astep); y = y + 80; if (i == 4) downmost = astep.safep; } } public void drawlift(graphics g)//画梯子 { for (int i=0;i<5;i++) { step astep=(step) alist[i]; astep.drawstep (g); } } public void liftrise()//梯子上升 { //表中的每个y坐标加1 //并把新的台阶存在表的尾部 for (int i = 0; i < 5; i++) { step astep = (step)alist[i]; point p = new point(astep.safep.x, astep.safep.y - 1); astep.safep = p; alist.add(astep); if (i == 4) downmost = astep.safep; } for (int i = 0; i < 5; i++)//删除表的前5个数据 { alist.remove(alist[i]); } } public void addstep()//伸起来的一节梯子 { step astep=new step (); astep.safep=astep.risepoint(); alist.add(astep); alist.remove(alist[0]); downmost = astep.safep; //始终保证最下面的一节为downmost } public bool manland(people man)//人物登陆事件 { step s; for (int a = 0; a < 5; a++) { s = (step)alist[a]; if (math.abs( s.safep.y -(man.footpoint.y+10))<2&& s.safep.x <= man.footpoint.x+10 && man.footpoint.x <= s.safep.x + 100) return true; } return false; } public bool manout(people man)//人物冲出事件 { step s; for (int a = 0; a < 5; a++) { s = (step)alist[a]; if (math.abs(s.safep.y - (man.footpoint.y + 10)) < 3) { if (s.safep.x-10 > man.footpoint.x || man.footpoint.x > s.safep.x + 100) return true; } } return false; } } public class people//人物类 { private point footpoint;//人物的坐标 public point footpoint { get { return footpoint; } set { footpoint = value; } } public void drawpeople(graphics g)//画个实心圆代表人物 { solidbrush b = new solidbrush(color.indianred); rectangle rect = new rectangle(footpoint.x, footpoint.y, 10, 10); g.fillellipse(b, rect); } public void moveleft()//人物向左移动 { point p = new point(footpoint.x - 2, footpoint.y); footpoint = p; } public void moveright()//人物向右移动 { point p = new point(footpoint.x + 2, footpoint.y); footpoint = p; } public void moverise()//人物随梯子上升 { point p = new point(footpoint.x, footpoint.y-1); footpoint = p; } public void movedown()//人物下落 { point p = new point(footpoint.x, footpoint.y + 1); footpoint = p; } public bool mandead()//人物死亡 { if ( footpoint.y<0 ||footpoint.y>340) return true ; return false ; } } } 发两个玩玩吧。 都测试过可行的
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯