【问题标题】:How to make the snake continue in that direction until another arrow key is pressed如何让蛇继续朝那个方向前进,直到按下另一个箭头键
【发布时间】:2021-03-02 06:28:51
【问题描述】:

我尝试了很多想法,比如导入 pynput,但它一直在崩溃,让我的电脑变得一团糟。我试过把它放在一个循环中,但效果不佳并一直崩溃

import sys
import pygame
import random
import os
import time

pygame.init()

#assigns thingys to window adjustments
size = width, height = 750, 750
white = 255, 255, 255
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Snake Game")

#The coordinates and size of the snake square thingy
x = 350
y = 350
w  = 25
h = 25
ax = range(30,700)
ay = range(40,700)
aw = 15 
ah = 15
vel = 10
vertical1 = 9000
vertical2 = 0 

score_value = 0 
font = pygame.font.Font('freesansbold.ttf', 32)
textx = 10
testy = 10

def show_score(x, y): 
    score = font.render("Score: "+ str(score_value), True, (255,0,0))
    screen.blit(score, (x, y))


def divisible_random(a,b,n):
    if b-a < n:
      raise Exception('{} is too big'.format(n))
    result = random.randint(a, b)
    while result % n != 0:
      result = random.randint(a, b)
    return result

def apple(x,y):
   pygame.draw.rect(screen, (255, 0, 0),  (arx, ary, aw, ah))



arx = divisible_random(30,700,10)
ary = divisible_random(40,700,10)



    

while 1:
    
    #Delays the movement so you can see snake thingy fps
    pygame.time.delay(25)

    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    #gets the presses to move the snake 
    keys = pygame.key.get_pressed() 
    
    if keys[pygame.K_LEFT]:
        while True:
            x= x - vel
            if keys[pygame.K_RIGHT] or keys[pygame.K_DOWN] or keys[pygame.K_UP]:
                break

    if keys[pygame.K_RIGHT]:
        while True:
            x= x + vel
            if keys[pygame.K_LEFT] or keys[pygame.K_DOWN] or keys[pygame.K_UP]:
                break


    if keys[pygame.K_DOWN]:
        while True:
            y = y + vel
            if keys[pygame.K_LEFT] or keys[pygame.K_RIGHT] or keys[pygame.K_UP]:
                break

    if keys[pygame.K_UP]:
        while True:
            y = y - vel
            if keys[pygame.K_LEFT] or keys[pygame.K_RIGHT] or keys[pygame.K_DOWN]:
                break

    screen.fill(white)

    #The snake thingy and apple thingy
    apple(arx, ary)
    snake = pygame.draw.rect(screen, (0, 0, 0), (x, y, w, h))
    diffrencex = abs(arx-x)
    diffrencey = abs(ary-y)

    if ((x == arx and y == ary) or ((diffrencex == 10 and x < arx) and (diffrencey == 10 and y < ary)) or ((diffrencex==10 and x< arx) and (y == ary)) or ((x == arx) and (diffrencey==10 and y< ary))):
        score_value += 1
        arx = divisible_random(30,700,10)
        ary = divisible_random(40,700,10)

    #Borders for the frame thingy 
    pygame.draw.line(screen, (0,0,0), (14, 10), (14, 800), 30)
    pygame.draw.line(screen, (0,0,0), (734, 10), (734, 800), 30) 
    pygame.draw.line(screen, (0,0,0), (9000, 734), (0 , 734), 30) 
    pygame.draw.line(screen, (0,0,0), (9000, 24), (0 , 24), 30) 
  
    if x < 30:
        x = 700
    
    if x > 700:
        x = 30
    
    if y > 700:
        y = 40
    
    if y < 40:
        y = 700

    show_score(textx, testy)
    

    print(arx,ary,x,y)

    pygame.display.update()  
pygame.quit

我知道代码不整洁,没有条理,但我会在完成游戏后立即着手处理

    keys = pygame.key.get_pressed() 
    
    if keys[pygame.K_LEFT]:
        while True:
            x= x - vel
            if keys[pygame.K_RIGHT] or keys[pygame.K_DOWN] or keys[pygame.K_UP]:
                break

    if keys[pygame.K_RIGHT]:
        while True:
            x= x + vel
            if keys[pygame.K_LEFT] or keys[pygame.K_DOWN] or keys[pygame.K_UP]:
                break


    if keys[pygame.K_DOWN]:
        while True:
            y = y + vel
            if keys[pygame.K_LEFT] or keys[pygame.K_RIGHT] or keys[pygame.K_UP]:
                break

    if keys[pygame.K_UP]:
        while True:
            y = y - vel
            if keys[pygame.K_LEFT] or keys[pygame.K_RIGHT] or keys[pygame.K_DOWN]:
                break

确切地说,我在这里遇到了问题

【问题讨论】:

  • 摆脱所有那些 while True 循环来处理密钥。您只想处理一次按下的键,并根据键设置一个方向变量。现在,只要按下一个键,您就会运行一个非常快速的增量循环,没有屏幕更新或任何东西。肯定会发生崩溃。

标签: python pygame 2d-games


【解决方案1】:

使用键盘事件(KEYDOWN、KEYUP - 请参阅pygame.event)而不是pygame.key.get_pressed()。添加一个变量direction,改变变量在LEFT、RIGHT、UP分别为DOWN时的状态按下。根据方向改变玩家的位置:

direction = (0, 0)

while 1:
    
    #Delays the movement so you can see snake thingy fps
    pygame.time.delay(25)

    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
            sys.exit()

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

    x += vel * direction[0]
    y += vel * direction[1]

您想要连续移动,但又不想按住按钮。即使没有按键,你也想移动蛇。因此,您需要将运动方向保存在一个变量中,并在按下某个键时一次改变方向。键盘事件在按下某个键时发生一次,是改变方向的正确选择。

【讨论】:

  • 赞成,因为这是一个比我更好的答案。
【解决方案2】:

使用变量来存储运动方向。让按键影响该变量。然后,在每次循环迭代时检查该变量,并根据方向移动蛇。

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 2021-02-01
    相关资源
    最近更新 更多