计时器受按键影响 [英] Timer affected by key presses

查看:73
本文介绍了计时器受按键影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为我的一个学校项目制作蛇游戏.到目前为止,除了计时器受游戏过程中按键的影响外,没有太多困难.

我的游戏包含一个倒数计时器,每次按下键来改变蛇的方向时,我都会注意到它暂停了(即使所按下的键没有功能).

任何有关如何解决此问题的帮助都将非常棒.

谢谢:)

I''m currently making a game of snake for a school project of mine. So far there haven''t been too many difficulties apart from the timers being affected by key presses during the game.

My game consists of a countdown timer which i have noticed pause every time there is a key press to change the snakes direction (even if the key pressed does not have a function).

Any help on how to fix this would be awesome.

Thank you :)

Public Enum SnakeDirection
    Left
    Right
    Down
    Up
End Enum







Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    Select Case e.KeyCode
        Case Keys.Up
            If Not direction = SnakeDirection.Down Then
                direction = SnakeDirection.Up
            End If
        Case Keys.Right
            If Not direction = SnakeDirection.Left Then
                direction = SnakeDirection.Right
            End If
        Case Keys.Down
            If Not direction = SnakeDirection.Up Then
                direction = SnakeDirection.Down
            End If
        Case Keys.Left
            If Not direction = SnakeDirection.Right Then
                direction = SnakeDirection.Left
            End If
    End Select
End Sub







Private Sub tmrMove_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMove.Tick
    Select Case direction
        Case SnakeDirection.Up
            If y = 1 Then
                y = 36
            ElseIf Not y = 1 Then
                y -= 1
            End If
            CreateTimer(x, y)
            field(x, y).Image = imlPictures.Images(0)
        
        Case SnakeDirection.Right
            If x = 64 Then
                x = 1
            ElseIf Not x = 64 Then
                x += 1
            End If
            CreateTimer(x, y)
            field(x, y).Image = imlPictures.Images(0)

        Case SnakeDirection.Down
            If y = 36 Then
                y = 1
            ElseIf Not y = 36 Then
                y += 1
            End If
            CreateTimer(x, y)
            field(x, y).Image = imlPictures.Images(0)

        Case SnakeDirection.Left
            If x = 1 Then
                x = 64
            ElseIf Not x = 1 Then
                x -= 1
            End If
            CreateTimer(x, y)
            field(x, y).Image = imlPictures.Images(0)
    End Select
End Sub



我希望这可以使它更清晰.在按下按键后,不仅计时器会暂停一段时间,而且蛇的动作也会暂停...



I hope this makes it clearer. Not only do the timers pause for a period after the keypress but also the snakes movement...

推荐答案

好吧,我决定蛇通过一系列图片框(由X和Y引用)的运动.createtimer的作用是在单个盒子上创建一个计时器,以便在一定的时间内启用单个盒子(取决于蛇的长度).如果i''我不能很好地解释它,我可能会通过电子邮件将vb.net文件发送给您,以便您可以自己查看.让我知道最适合您的情况.非常感谢您的帮助.

还有你的问题!

不要那样做.您正在各处创建计时器,这会浪费时间(没有双关语!)请考虑一下:每当您收到计时器刻度时,便会创建一个新计时器... Gulp.

相反,只需要一个计时器(您已经有一个计时器,因此请使用它).
设置一个二维整数数组.
当您在屏幕上放置一条蛇时,将适当的整数设置为应保留的计时器刻度数.
在Tick事件中,扫描数组,然后:
1)如果元素为零,则不执行任何操作.
2)如果元素不为零,则将其减少一.
3)如果将其减小为零,请清除相关的PictureBox-蛇尾需要变短.

您应该会发现这会阻止它放慢速度!

理想情况下,我自己在Paint事件中绘制图像,因为它比使用Pictureboxes更快(特别是如果您有很多的话),但这对您来说可能有点高级.
不过,如果您想稍后再试,只需询问即可!
"Ok, i''ve decided to make the snake movement through an array of pictureboxes (referenced by X and Y). What the createtimer does is it creates a timer on individual boxes so that individual boxes are enabled for a certain duration (depending on the snakes length). If i''m not explaining it very well i could possibly email you the vb.net files so you can see it work for yourself. Let me know whats best for you. Thankyou very much for your help"

And there is your problem!

Don''t do it that way. Your are creating timers all over the place, which wastes time (no pun intended!) Think about it: every time you get a timer tick, you create a new timer... Gulp.

Instead, have just the one timer (you have one already, so use that).
Set up a two dimensional array of integers.
When you put a piece of a snake on the screen, set the appropriate integer to the number of timer ticks it should stay live for.
In the Tick event, scan through the array, and:
1) If the element is zero, do nothing.
2) If the element in not zero, reduce it by one.
3) If reducing it made it zero, clear the relevant PictureBox - the snakes tail needs to get shorter.

You should find this stops it slowing things down!

Ideally, I would draw the images myself in the Paint event, as it can be a lot quicker than using Pictureboxes (particularly if you have a lot of them) but that may be a little advanced for you.
If you want to try later, though, just ask!


像执行移动和图像处理的例程之类的声音都比计时器的刻度间隔长.

您可能要考虑的是将所有代码移出计时器刻度,然后移到独立的后台线程中.

然后在您的计时器中,将移动添加到由后台工作人员处理的队列中,或者如果后台工作人员仍在忙于工作,请跳过节拍直到下一个刻度事件并重新检查.使用标志来确定工作人员是否忙碌.

如果您不能使用线程,那么请考虑一下如何确保代码的顺序性质,并使用标志跳过计时器,直到从上一个滴答声开始完成所有事情为止.
Sounds like the routines to do the moving and image manipulation are longer than the timer tick interval.

What you may want to think about is move all the code out of the timer tick and into a stand alone background thread.

Then in your timer, add the move to a queue which is processed by the background worker, or if the background worker is still busy working skip a beat until the next tick event and recheck. Use a flag to determine if worker is busy.

If you can''t use threads, then maybe think about how you can ensure the sequential nature of the code, and just use a flag to skip the timer until everything is finished from the previous tick.


这篇关于计时器受按键影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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