Pygame:如何解决PyGame上随机生成的平台在错误的屏幕上朝错误的方向移动的问题?

时间:2019-01-23 11:45:11

标签: python pygame

所以我一直在遵循一些指南,并采取了一些自己的主动行动,但是现在我陷入了困境。我正在随机生成平台(是)并在屏幕上出现(两次),但是从屏幕底部到顶部,而不是我想要的从右到左。我发现很难理解如何修改它。

我(愚蠢地)试图更改变量名。 我尝试更改randint和附加部分中的内容。但是,例如,我不太想像“ pos”那样修改,因为我不太确定它到底在发生什么。

# For the program, it was necessary to import the following.
import pygame, sys, random
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS
import pygame.time as GAME_TIME

pygame.init() # To initialise the program, we need this command. Else nothing will get started.

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')

oneDown = False

gameStarted = False
gameEnded = False

gamePlatforms = []
platformSpeed = 3
platformDelay = 4000
lastPlatform = 0


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():
    pressedKey = pygame.key.get_pressed()
    if pressedKey[pygame.K_UP]:
        player["y"] -= 5
    elif pressedKey[pygame.K_DOWN]:
        player["y"] += 5


def creatingPlatform():
    global lastPlatform, platformDelay
    platformY = windowWidth
    gapPosition = random.randint(0, windowWidth - 100)
    gamePlatforms.append({"pos": [0, platformY], "gap": gapPosition})
    lastPlatform = GAME_TIME.get_ticks()

def movingPlatform():
    for idx, platform in enumerate(gamePlatforms):
        platform["pos"][1] -= platformSpeed
        if platform["pos"][1] < -10:
            gamePlatforms.pop(idx)

def drawingPlatform():
    global platform
    for platform in gamePlatforms:
        pygame.draw.rect(surface, (214, 200, 253), (platform["gap"], platform["pos"][1], 40, 10))


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))
    pressedKey = pygame.key.get_pressed()
    for event in GAME_EVENTS.get():
        if event.type == pygame.KEYDOWN:
            # 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_1:
                oneDown = False
                #KEYUP for the space bar
        if event.type == GAME_GLOBALS.QUIT:
            quitGame()

    if gameStarted is True:
        drawingPlayer()
        movingPlayer()
        creatingPlatform()
        movingPlatform()
        drawingPlatform()

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

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



    pygame.display.update()

预期结果:平台从屏幕右侧到左侧接近黄色矩形,并且该矩形变高而不是变宽。

实际结果:平台从屏幕底部到顶部,并且平台很宽。但是我可能可以解决后者,我只是想先解决这个问题。

1 个答案:

答案 0 :(得分:0)

好的,这是我所做的更改: 在creatingPlatform()中,我创建了一个变量来保存平台的垂直位置。我也将您的platformY重命名为platformX,因为它是随机的x位置,而不是随机的y位置。 我将新的垂直位置用作平台的“ pos”属性的一部分,并将其置于常规的(x,y)顺序中。这是修改后的函数的代码:

def creatingPlatform():
    global lastPlatform, platformDelay
    platformX = windowWidth
    gapPosition = random.randint(0, windowWidth - 100)
    verticalPosition = random.randint(0, windowHeight)
    gamePlatforms.append({"pos": [platformX, verticalPosition], "gap": gapPosition})
    lastPlatform = GAME_TIME.get_ticks()

接下来,我必须修改movingPlatform(),以便它更新x位置,而不是y位置。只需将platform["pos"]的索引从1更改为0:

def movingPlatform():
    for idx, platform in enumerate(gamePlatforms):
        platform["pos"][0] -= platformSpeed
        if platform["pos"][0] < -10:
            gamePlatforms.pop(idx)

最后,我只是将平台位置传递给了draw函数:

def drawingPlatform():
    global platform
    for platform in gamePlatforms:
        pygame.draw.rect(surface, (214, 200, 253), (platform["pos"][0], platform["pos"][1], 40, 10))

这产生了从右向左移动的平台,它们又宽又不高!