如何在按下按键时使矩形“玩家”连续移动?

时间:2019-01-16 12:07:24

标签: python pygame

我在寻找其他问题,并试图将解决方案应用于我的代码,但无济于事。我试图使我的黄色矩形上下移动,而不必多次按下该键。我想持续运动。

我已经尝试过使用get_pressed()了,但是似乎没有用。

import pygame, sys, random
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS
import pygame.time as GAME_TIME
#General window things here
pygame.init()

StartImage = pygame.image.load("Assets/Start-Screen.png")
GameOverImage = pygame.image.load("Assets/Game-Over-Screen.png")

#Window details are here
windowWidth = 1000
windowHeight = 400

surface = pygame.display.set_mode((windowWidth, windowHeight))
pygame.display.set_caption('GAME NAME HERE')

upDown = False
downDown = False
oneDown = False

gameStarted = False
gameEnded = False
hitPlatform = False

gameBeganAt = 0
timer = 0

player = {
    "x" : 10,
    "y" : 200,
    "height" : 25,
    "width" : 10,
    "vy" : 5

}

def drawingPlayer():
    pygame.draw.rect(surface, (248,255,6), (player["x"], player["y"], player["width"], player["height"]))

def movingPlayer():
    global upDown, downDown, pressedKey, hitPlatform
    pressedKey = pygame.key.get_pressed()
    while hitPlatform = False:
        if pressedKey[pygame.K_UP]:
            player["y"] -= 5
        elif pressedKey[pygame.K_DOWN]:
            player["y"] += 5



def gameOver():
    global gameStarted, gameEnded, platformSpeed

    platformSpeed = 0
    gameStarted = False
    gameEnded = True

def quitGame():
    pygame.quit()
    sys.exit()

def gameStart():
    global gameStarted
    gameStarted = True

while True:

    surface.fill((95,199,250))

    for event in GAME_EVENTS.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                upDown = True
            if event.key == pygame.K_DOWN:
                downDown = True
            # Event key for space should initiate sound toggle
            if event.key == pygame.K_1:
                oneDown = True
                gameStart()

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                upDown = False
            if event.key == pygame.K_DOWN:
                downDown = False
            if event.key == pygame.K_1:
                oneDown = False


                #KEYUP for the space bar


        if event.type == GAME_GLOBALS.QUIT:
            quitGame()



        if gameStarted is True:

            timer = GAME_TIME.get_ticks() - gameBeganAt



            drawingPlayer()
            movingPlayer()

        elif gameEnded is True:
            surface.blit(GameOverImage, (0, 0))

        else:
            surface.blit(StartImage, (0, 0))



        pygame.display.update()

0 个答案:

没有答案