蛇博弈算法问题 [英] Snake Game Algorithm Problem

查看:55
本文介绍了蛇博弈算法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我试图在c ++中创建一个蛇程序(我使用devC ++环境).我已经初始化了蛇.但是我不知道如何移动蛇.我正在使用来自borland的graphics.h标头...

我想知道您是否可以帮助我解决蛇移动算法???谢谢!

来自OP的代码,已从评论中删除.

谢谢!感谢您对帮助的兴趣:)我创建了这个试用版,并提供了以下代码段...

Hi!


I was trying to create a snake program in c++ (i''m using devC++ environment). I had already initialized the snake. But I have no idea as to how to move the snake. I''m using the graphics.h header from borland...

I was wondering if you could help me out on the snake movement algorithm??? thanks!

Code from OP, removed from comment.

Thanks!for ur interest in helping out :) i''ve created this trial class and have this snippet...

void snake::createSnake() 
{ 
  while (cnt==0){ 
    for (i;i<=800;i++){ 
      cnt++; 
      rectangle(10+i,300,i,310); 
      rectangle(20+i,300,10+i,310); 
      rectangle(30+i,300,20+i,310); 
      rectangle(40+i,300,30+i,310); 
      rectangle(50+i,300,40+i,310); 
      rectangle(60+i,300,50+i,310); 
      rectangle(70+i,300,60+i,310); 
      Sleep(10); 
      cleardevice(); 
    } 
  } 
} 


我正在尝试操纵此代码段...这只是一个试验,而不是最终代码.我只是想尝试一下,如果我可以移动矩形.谢谢...我真的是从零开始...这并不容易...大声笑...但是非常感谢!
[/Edit]


i''m trying to manipulate this code snippet...this is just a trial and not the final codes yet. i just want to try it out if i could move the rectangles. thanks... i''m really starting from scratch... and it isn''t easy... lol... but thanks a lot!
[/Edit]

推荐答案

选中此
http://www.cplusplus.happycodings.com/Computer_Graphics/code24.html [ ^ ]

Google [ ^ ]).
Check this
http://www.cplusplus.happycodings.com/Computer_Graphics/code24.html[^]

Google[^] for more.


一些问题:

1.我看不到cnti(或其他任何东西)的任何变量声明.即使它们可能是在外部,某个地方声明的,这也是不好的设计. C ++全部与封装有关,这意味着您应该声明变量尽可能接近它们所使用的范围.将声明正确放在您的Create 函数中.

2. cnti都没有初始化-不清楚它们从什么值开始,因此无法确定Create函数的行为.始终初始化变量,最好是在声明时进行初始化,并且可能(再次)在实际使用它们之前进行初始化.初始化很便宜.忘记初始化并花费大量时间来追逐错误是很昂贵的!

3.您正在将蛇创建为一系列矩形(或者实际上是正方形),但是如果您想让蛇游戏成为我想起的那种游戏,那么矩形的总数会随着时间而变化!因此,将图形硬编码为固定的一系列矩形命令是一个坏主意.相反,您应该制作一个受蛇的当前形状(和长度)控制的循环.

4.该函数声明为创建",但它的作用远不止于它遍历i并因此在屏幕上移动"蛇.干净地分开您的功能!否则,您只会迷惑自己并创建无法维护的代码.

5.您在其中输入了睡眠"命令,以控制移动速度!那是一个非常非常糟糕的主意!控制游戏流程应该只在一个中央位置进行,而不是在每个实用程序功能中单独进行!

6.更糟糕的是您打给cleardevice的电话!该函数指出您创建"了某些东西,但是最后一件事是再次销毁它?它不仅会破坏自身,还会破坏屏幕上可能可见的其他任何东西!如果要删除蛇的最后一幅画,则仅删除那条,而不是整个屏幕.并为此做一个单独的功能!并且不要在您的创建"函数中调用它,除非一开始就清理所有先前存在的蛇.实际上,甚至不需要擦除整条蛇-如果您希望它移动",则仅擦除尾部的最后一点和重画"部分的效率要高得多.然后只需添加一个新的头.

还有一件事:您的创建"功能甚至都不会画任何东西!绘制蛇是一种应该单独使用的功能,它应该能够以当前形状绘制蛇.为此,您当然需要以某种方式存储蛇的当前形状-而这正是创建"功能还应该做的:存储蛇的形状.没什么.
Some issues:

1. I don''t see any variable declaration for either cnt or i (or anything else). Even though they may be declared outside, somewhere, that is bad design. C++ is all about encapsulation, and that means you should declare variables as close to the scope they''re used in as possible. Put the declarations right in your Create function.

2. Neither cnt nor i were initialized - it is not clear what values they''re starting at and therefore it cannot be determined how your Create function will behave. Always initialize your variables, preferably right at the point of declaration, and possibly (again) right before they''re actually used. Initialization is cheap. Forgetting initialization and spending hours to chase errors is expensive!

3. You''re creating your snakes as a series of rectangles (or, in fact, squares), but if your snake game is supposed to be that kind of game I have in mind then the total number of rectangles will vary over time! Hardcoding the drawing as a fixed series of rectangle commands therefore is a bad idea. Instead you should make a loop that is controlled by the current shape (and length) of the snake.

4. The function states ''Create'', but it does much more than that as it iterates over i and thus ''moves'' the snake over the screen. Do seperate your functions cleanly! Otherwise you''re just confusing yourself and create unmaintainable code.

5. You put in a Sleep command right in there, to control the speed of movement! That is a very, very bad idea! Controlling the flow of your game should happen in just one central location and not in every single utility function independently!

6. Even worse is your call to cleardevice! The function states that you ''create'' something, but the very last thing is to destroy it again? And not only does it destroy itself, it also destroys anything else that might be visible on the screen! If you want to erase your last drawing of the snake, then erase that and that only, not the entire screen. And make a separate function for it! And don''t call it in your ''create'' function except maybe right at the beginning to clean up any previously exxisting snake(s). In fact, it isn''t even neccessary to erase the entire snake - if you want it to ''move'' it''s much more efficient to just erase the last bit of the tail and the ''redraw'' part then just has to append a new head.

One more thing: your ''create'' function shouldn''t even draw anything! Drawing your snake is one functionality that should go in a separate function, and it should be able to draw the snake in it''s current shape. For that to work you of course need to somehow store the current shape of your snake - and that, exactly is what your ''create'' function also should do: store the shape of your snake. And nothing else.


对于蛇,您必须存储蛇的身体位置.即:每条蛇都会占据.蛇具有头部和尾部-要移动蛇,您可以在蛇头周围添加新的随机位置并删除尾部位置.旧位置(+)新头(-)旧路径是您必须存储的新蛇体.
祝你好运.
For the snake you have to store the snakes body position. i.e: every rect the snake occupies. the snake has a head and a trail - to move the snake you can add a new random position around the snakes head and remove the trail position. the old positions (+) new head (-) old trail is the new snake body you have to store.
Good luck.


这篇关于蛇博弈算法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆