设置基本游戏和调试基本问题 [英] Setting up basic game and debugging fundamental problems

查看:110
本文介绍了设置基本游戏和调试基本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,试图在一个基本的瓦片引擎中创建一个简单的蛇游戏。我从来没有使用过C#或Windows Forms,目前正处于学习阶段。我很高兴能从这里得到任何帮助。

  int [,] level = {
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0, 0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
};

目前,所有的tile都是空的。如果我想让我们说添加图像,我所要做的只是将0转换为1.我的目标是通过键盘上的箭头键将0变为1。

  private void tmrMov_Tick(object sender,EventArgs e)
{

if(_objPosition == Position.Right)
{

if(_x< 6& _x> = 0)
_x + = 1;

}



//等等




Invalidate();
}

有了这个,我想通过箭头键将0变为1,但是目前不工作。

  public Form1()
{
InitializeComponent();
_x = rand.Next(0,5 + 1);
_y = rand.Next(0,5 + 1);
_k = 1;
级[_x,_y] = _k;
_objPosition = Position.Right;
}

这是我如何调用上面的数组...



请让我知道我做错了什么我知道我必须最终重新开始,或者拿起另一个项目去工作,但是我至少要知道我没有完全错误。



我附上了完整的项目,因为不久以后阅读...
http://www.mediafire.com/? hz3h2job28y9lfb



编辑:
这个问题也发贴在这里,因为stackexchange的一个答案建议在这里发布调试问题: https://softwareengineering.stackexchange.com/questions/188285/simple-gameproblem-with-key-mapping



随意删除其中一个但不能同时删除!



提前感谢!

解决方案

要移动你的蛇,你应该把 0 位置(因为现在只是在细胞长),计算在新的位置,并将 1 在新的位置。所以你的计时器的tick hanlder可能看起来像这样:

  private void tmrMov_Tick(object sender,EventArgs e)
{
级[_x,_y] = 0;
// ...
//其他东西根据位置计算新的位置

级别[_x,_y] = 1;

Invalidate();
}

还在 Paint 事件是完全错误的。您应该使用 <$ c $的各种方法绘制板c>图形 类。似乎你想这样做,但是为什么要放弃?



有很多关于你的代码。以下是其中的几个:


  1. 而不是使用魔术数字 6 无处不在的板宽或高度,将板的尺寸放在可变量中,并将其用于各处。这样,您可以轻松地更改电路板尺寸,而不会影响整个源代码(或者只是忘记更新一个sinlge 6 并引入难以发现的错误)。 p>


  2. 而不是每次从文件( panel1_Paint 中的注释代码)加载图像,请加载图像一次,调整大小如果需要,并保留在一个领域。每次需要使用它。这样可以显着提高性能。


  3. 使用 Image.FromFile 来加载图像表单文件。


  4. 你永远不希望你的蛇走出董事会,所以当你想要向右移动蛇(例如)时,你可以增加 _x 只有当 _x< _boardWidth-1 。你知道我是说当我说 _boardWidth ,不是吗? : - )


  5. 您应该在 Paint 事件中绘制整板。不只是单个单元格。 绘制事件在您的表单需要绘制时调用,而不仅仅是当您调用 Invalidate 方法时。


  6. 您有一个名为 Position 的枚举。实际上应该是方向


快乐学习/ p>

I have a little problem trying to create a simple snake game in a basic tile engine. I have never worked with C# or Windows Forms and currently in a learning stage. I'd appreciate any help i get from here.

        int[,] level = {
                               { 0, 0, 0, 0, 0 ,0 },
                               { 0, 0, 0, 0, 0 ,0 },
                               { 0, 0, 0, 0, 0 ,0 },
                               { 0, 0, 0, 0, 0 ,0 },
                               { 0, 0, 0, 0, 0 ,0 },
                               { 0, 0, 0, 0, 0 ,0 },
                           };

Currently, all the tiles are empty thus off. If i want to lets say add the image, all i have to do is turn 0 to 1. My goal is to turn 0 to 1 with the arrow keys from keyboard.

private void tmrMov_Tick(object sender, EventArgs e)
    {

        if (_objPosition == Position.Right) 
        {

            if(_x<6 && _x>=0)
            _x += 1;

         }
        .
        .
        .
         //and so on
        .
        .
        .

        Invalidate();
    }

With this, i am trying to turn 0 to 1 through arrow keys but it currently does not work.

        public Form1()
    {
            InitializeComponent();
            _x = rand.Next(0,5 + 1);
            _y = rand.Next(0, 5 + 1);
            _k = 1;
            level[_x, _y] = _k;
            _objPosition = Position.Right;
    }

This is how i am calling the above array...

Please let me know what i am doing wrong. I know i have to eventually start over or pick up another project to work on but i want to at least know that i was not completely wrong.

I am attaching the complete project since it's not long to read... http://www.mediafire.com/?hz3h2job28y9lfb

EDIT: This question is also posted here since one of the answers on stackexchange suggested to post here for debugging questions: https://softwareengineering.stackexchange.com/questions/188285/simple-gameproblem-with-key-mapping

Feel free to delete one of the other but not both!

Thanks in advance!

解决方案

To move your snake, you should put 0 in its previous location (since for now it is only on cell long), calculate the new location, and put 1 in the new location. So your timer's tick hanlder could look like this:

private void tmrMov_Tick(object sender, EventArgs e)
{
    level[_x, _y] = 0;
    //...
    // other stuff to calculate new location based on Position

    level[_x, _y] = 1;

    Invalidate();
}

Also adding PictureBoxes in the Paint event is completely wrong. You should draw the board, using various methods of the Graphics class. It seems that you wanted to do that, but why did you give it up?

There is a lot to say about your code. Here are a few of them:

  1. Instead of using the magic number 6 everywhere for the board width or height, put the size of the board in varaibles and use them everywhere. This way you can easily change the board size without affecting the whole source code (or just forgetting to update a sinlge 6 and introduce a hard-to-find bug).

  2. Instead of loading the image everytime from file (commented code in panel1_Paint), load the image once, resize it if required, and keep it in a field. Use it everytime required. This enhances the performance dramatically.

  3. Use Image.FromFile to load an image form file.

  4. You never want your snake to go out of the board, so when you wnat to move the snake right (for example), you can increment the _x only when _x < _boardWidth-1. You know waht I mean when I say _boardWidth, don't you? :-)

  5. You should paint the whole board in the Paint event. No just a single cell. Paint event is called whenever your form needs to be draw, not just when you call the Invalidate method.

  6. You have an enumeration called Position. It should be actually Direction.

Happy learning.

这篇关于设置基本游戏和调试基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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