【问题标题】:How to force the player to keyup?如何强制玩家按键?
【发布时间】:2017-04-19 23:27:56
【问题描述】:

在我的程序中,我有一个跳过汽车的正方形。但是玩家可以只按住空间而不触地。这意味着这不能。我想知道如何让它在设定的时间后迫使立方体回到地面。

这是我的代码:

import pygame
import time

pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Traffic Jumper')

orange = (255, 100, 0)
black = (0,0,0)
grey = (128,128,128)
yellow = (255,255,0)
green = (0,128,0)
red=(255,0,0)
blue = (64,224,208)
brown = (139,69,19)
magenta = (255,0,255)
olive = (128,128,0)
clock = pygame.time.Clock()

def options():
    op = True
    while op:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.QUIT()
            screen.fill(magenta)
            largeText = pygame.font.Font('freesansbold.ttf',115)
            Textsurf, TextRect = text_objects("Get Hit Bro!", largeText)
            TextRect.center = ((400),(300))
            screen.blit(Textsurf, TextRect)
            smallText = pygame.font.Font('freesansbold.ttf',30)
            Textsurf, TextRect1 = text_objects("Press Y to restart and N to\  leave", smallText)
            TextRect1.center = (400,(400))
            screen.blit(Textsurf, TextRect1)
            pygame.display.update()
            for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_y:
                    game_loop()
                elif event.key == pygame.K_n:
                    pygame.quit()

def pause():
    paused = True
    while paused:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                paused=False
                pygame.QUIT()
                quit()
        screen.fill(olive)
        largeText = pygame.font.Font('freesansbold.ttf',115)
        Textsurf, TextRect = text_objects("Paused", largeText)
        TextRect.center = ((400),(300))
        screen.blit(Textsurf, TextRect)
        smallText = pygame.font.Font('freesansbold.ttf',30)
        Textsurf, TextRect1 = text_objects("Press r To Resume", smallText)
        TextRect1.center = (400,(400))
        screen.blit(Textsurf, TextRect1)
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    paused = False



def text_objects(text, font):
    textsurface = font.render(text,True,red)
    return textsurface, textsurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    Textsurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((400),(300))
    screen.blit(Textsurf, TextRect)
    pygame.display.update()
    time.sleep(2)
    #game_loop()

def game_loop():
    paused = False
    score = 0
    x = 20
    y = 520
    carx = 1000
    cary = 490
    carspeed = 7
    done = False
    while not done:
            screen.fill((255, 255, 255))
            sky=pygame.draw.rect(screen,blue,pygame.Rect(0,0,800,400))
            grass=pygame.draw.rect(screen,green,pygame.Rect(0,400,800,100))
            floor1=pygame.draw.rect(screen,black,pygame.Rect(0,500,800,10))
            floor2=pygame.draw.rect(screen,black,pygame.Rect(0,590,800,10))
            road=pygame.draw.rect(screen,grey,pygame.Rect(0,510,800,80))
            paint1=pygame.draw.rect(screen,yellow,pygame.Rect(10,540,100,\ 20))
            paint2=pygame.draw.rect(screen,yellow,pygame.Rect(200,540,100,\ 20))
            paint3=pygame.draw.rect(screen,yellow,pygame.Rect(400,540,100,\ 20))
            paint4=pygame.draw.rect(screen,yellow,pygame.Rect(600,540,100,\ 20))
            paint5=pygame.draw.rect(screen,yellow,pygame.Rect(780,540,100,\ 20))
            player=pygame.draw.rect(screen, orange, pygame.Rect(x, y, 60,\ 60))
            car=pygame.draw.rect(screen,black,pygame.Rect(carx,cary,80,\ 95))
            if x > 800 - 60:
                x -= 3
            elif x < 0:
                x += 3
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    #done = True
                    quit()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                y -= 200   
            if event.type == pygame.KEYUP and event.key == pygame.K_SPACE:
                y += 200
            if event.type == pygame.KEYDOWN and event.key == pygame.K_p:
                pause()


            pressed = pygame.key.get_pressed()
            #if pressed[pygame.K_UP]: y -= 3
            #if pressed[pygame.K_DOWN]: y += 3
            if pressed[pygame.K_LEFT]: x -= 3
            if pressed[pygame.K_RIGHT]: x += 3
            carx -= carspeed
            if carx < -100:
                score += 1
                carspeed += 0.2
                carx = 1000

            if x > carx - 55 and x < carx + 55 and y > cary:
                #message_display('Get Hit Bro!')
                options()
                font = pygame.font.SysFont(None,25)
                text = font.render("Dodged: " + str(score) ,True,black)
                screen.blit(text,(0,0))

        pygame.display.flip()
        clock.tick(60)
game_loop()

【问题讨论】:

  • 你用的是什么库? event 是什么?
  • 我只是把我所有的代码都放进去

标签: pygame python-3.5


【解决方案1】:

将这些变量添加到 game_loop:

    hasJumped = False
    jumpCooldown = 0.0

在您的循环中,在“未完成”中添加

    if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            if hasJumped == False:
                y -= 200
                hasJumped = True
    if hasJumped == True:        
        jumpCooldown += clock.get_time()
    if jumpCooldown > [Jump Cooldown Limit]: # Set this number to the amount of time the car can jump at once. You'll have to experiment.
        hasJumped = False
        jumpCooldown = 0.0

PS:我不明白您为什么使用 pygame.KEYDOWN/KEYUP 来检测一组键,而使用 pygame.key.get_pressed() 来检测另一组。看起来很乱

【讨论】:

    【解决方案2】:

    您可以在玩家每次按下空格时设置一个计数器。这个计数器可以对每次迭代进行倒计时,当它到达零时,y 位置将被设置回来:

    delta_y = 0
    if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
        counter = 10   
    if event.type == pygame.KEYUP and event.key == pygame.K_SPACE:
        counter = 0
    if counter > 0:
        delta_y = 200
        counter -= 1
    else:
        delta_y = 0
    

    您必须在 while 循环之外设置 counter = 0。我在垂直位置添加了一个delta_y,因为这是一种更简洁的方法。您必须将此行更新为:

    player=pygame.draw.rect(screen, orange, pygame.Rect(x, y + delta_y, 60, 60))
    

    计数器从 10 开始倒计时 - 您只需尝试一下即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-18
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      相关资源
      最近更新 更多