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

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

问题描述

我有一个小问题,试图在一个基本的瓷砖引擎创建一个简单的蛇游戏。我从来没有与C#或Windows窗体和目前正处于学习阶段。我倒是AP preciate任何帮助,我从这里得到。

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 },
                           };

目前,所有的砖都是空的因此关闭。如果我想可以说添加图像,所有我需要做的就是把0到1。我的目标是把0-1与键盘的方向键。

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();
    }

有了这个,我试图把0比1通过方向键,但它目前是行不通的。

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.

我附上了完整的项目,因为它不长读... http://www.mediafire.com/?hz3h2job28y9lfb

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

编辑: 这个问题也被张贴在这里,因为在stackexchange答案的一个建议,张贴在这里进行调试的问题:<一href="http://programmers.stackexchange.com/questions/188285/simple-gameproblem-with-key-mapping">http://programmers.stackexchange.com/questions/188285/simple-gameproblem-with-key-mapping

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

随意删除其他的一个而不是两个!

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

在此先感谢!

推荐答案

要移动你的蛇,你应该把 0 其previous位置(因为现在它只是在单元长),计算出新的位置,并把 1 在新位置。所以,你的计时器的滴答hanlder可能是这样的:

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();
}

还增加了在油漆PictureBoxes 事件是完全错误的。您应该绘制电路板,使用 图形 类。看来你想这样做,但你为什么放弃?

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?

有很多要说一下你的code。下面是其中的几个:

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

    除了使用幻数 6 处处为板的宽度或高度,把板的varaibles大小,并用他们无处不在的
  1. 。这样你就可以很容易地改变电路板的尺寸,而不会影响整个源$ C ​​$ C(或只是忘了更新的单层 6 并引入难以发现的bug)

  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).

加载从文件中的图像每次的相反(评论code。在 panel1_Paint ),加载一次图像,调整大小,如果需要,并保持中的一个领域。使用它每次需要。这增强了性能大幅提升。

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.

使用 Image.FromFile 加载图像格式的文件。

Use Image.FromFile to load an image form file.

您永远不希望你的蛇出去的板子,所以当你wnat移动蛇权利(例如),您可以递增 _x 只有当 _x&LT; _boardWidth-1 。你知道啥子我的意思是,当我说 _boardWidth ,不是吗? : - )

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? :-)

您应该画在油漆事件的整个董事会。不只是一个单一的细胞。 油漆事件被称为每当您的形式必须是平局,不只是当你调用的Invalidate 方法。

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.

您有一个名为位置枚举。它应该是实际方向

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

快乐学习。

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

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