简体   繁体   English

按下一个键会创建一个循环,直到您按下另一个键

[英]Pressing a key creates a loop until you press another key

I'm creating a Snake game in Python. After pressing a direction key the snake will move in that direction at a slow pace until the user presses another key.我正在 Python 中创建一个贪吃蛇游戏。按下方向键后,蛇将以缓慢的速度朝该方向移动,直到用户按下另一个键。 I was able to make the snake move after pressing a key, but I could not stop it by pressing another key: (I'm using a pygame)按下一个键后我能够让蛇移动,但我无法通过按下另一个键来停止它:(我正在使用 pygame)

if event.type == pygame.KEYDOWN:

    if event.key == pygame.K_LEFT:

        while i==False:
            time.sleep(0.2)
            X_had1=X_had1 - 1

            #Print operation

            if event.type == pygame.KEYDOWN:
                if event.key != pygame.K_LEFT:
                    i=True

No. Never try to control the application with an additional loop in the application loop.不可以。切勿尝试在应用程序循环中使用附加循环来控制应用程序。 You have the application loop use it.您让应用程序循环使用它。 Add a variable that controls direction and change the value of the variable when you press a key.添加一个控制方向的变量,并在按下某个键时更改变量的值。 Use the direction to change the position ( X_had1 , Y_had1 ):使用direction改变 position ( X_had1 , Y_had1 ):

direction = None
speed = 1

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                direction = (-1, 0)
            if event.key == pygame.K_RIGHT:
                direction = (1, 0)
            if event.key == pygame.K_UP:
                direction = (0, -1)
            if event.key == pygame.K_DOWN:
                direction = (0, 1)

    if direction:
        X_had1 += direction[0] * speed
        Y_had1 += direction[1] * speed

This is what I did in the end-这就是我最后所做的——

    if event.type == pygame.KEYDOWN:
    x=event.key
    y=x
    print(x)

while b == False:


    if y1 != 275:
        if y == 276:

            hadX1=hadX1 -1
            IMAGEsnake=r"C:\תמונות פיתון\‏‏ראש הנחש קטן שמאלה.png"
            y1=y
            print(nums)
            b=True
    elif y1==275:
        if y == 276:
            y=275





    if y1 != 273:
        if y == 274:
            hadY1=hadY1 + 1
            IMAGEsnake=r"C:\תמונות פיתון\‏‏ראש הנחש קטן למטה.png"
            y1=y
            print(nums)
            b=True
    elif y1==273:
        if y == 274:
            y=273


    if y1 !=276:
        if y == 275:
            hadX1=hadX1 + 1
            IMAGEsnake=r"C:\תמונות פיתון\ראש הנחש קטן ימינה.png"
            y1=y
            print(nums)
            b=True
    elif y1==276:
        if y == 275:
            y=276



    if y1 != 274:
        if y == 273:
            hadY1=hadY1 - 1
            IMAGEsnake=r"C:\תמונות פיתון\‏‏ראש הנחש קטן למעלה.png"
            y1=y
            print(nums)
            b=True
    elif y1==274:
        if y == 273:
            y=274

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM