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

2024-10-03 23:26:01 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我一直遵循一些指导,并采取了一些我自己的主动,但我现在陷入困境。我正处在一个平台被随机生成(yay)并出现在屏幕上(double yay)的点上,但是从屏幕的底部到顶部,而不是我想要的从右到左。我发现很难理解如何修改这个。你知道吗

我(愚蠢地)尝试过更改变量名。 我试着改变randint和append部分中的内容。但是,我并不想去修补像“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()

预期结果:平台从屏幕右侧向左侧靠近黄色矩形,矩形也不是宽而是高。你知道吗

实际结果:平台从屏幕底部到顶部,平台很宽。但我也许可以解决后者,我只想先研究方向。你知道吗


Tags: posimporteventgamefalsetruegetif
1条回答
网友
1楼 · 发布于 2024-10-03 23:26:01

好的,下面是我所做的修改: 在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))

这就产生了从右向左移动的平台,而且它们也很宽而不高!你知道吗

相关问题 更多 >