Pygame如何将随机生成的平台平均隔开?

2024-10-03 23:18:22 发布

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

所以,在我之前的问题中,我得到了一个答案,这个答案有助于确定平台的方向。然而,有一个新问题我无法回避。如何使平台之间有足够的空间? (按1开始游戏)

# 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 = 2
platformDelay = 2000
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
    platformX = windowWidth
    gapPosition = random.randint(0, windowWidth)
    verticalPosition = random.randint(0, windowHeight)
    gamePlatforms.append({"pos": [platformX, verticalPosition], "gap": gapPosition}) # creating platforms
    lastPlatform = GAME_TIME.get_ticks()
    if platformDelay > 800:
        platformDelay -= 50

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

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


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

我尝试过增加platformDelay变量的值,但没有效果。我也尝试过修补creatingPlatform()。不管我做什么,它们总是成群结队地出现!我希望这是一个游戏,平台来向玩家在固定的时间间隔,所以它实际上是可玩的,但随后的速度平台接近将随着时间的推移,增加游戏的难度。我该怎么做呢?谢谢您!:)


Tags: importeventgamefalsetruegetifdef
1条回答
网友
1楼 · 发布于 2024-10-03 23:18:22

我建议使用^{}来获取程序正在运行的毫秒数。注意,因为时间是以毫秒为单位的,所以1000的值是1秒。你知道吗

将变量newPlatformTimePoint初始化为0,并定义新平台出现的间隔

newPlatformTimePoint = 0
newPlatformInterval = 200 # 0.2 seconds

当游戏开始时,获取主循环中的当前时间点,并设置游戏开始时第一个平台的时间点:

timePoint = pygame.time.get_ticks()
if event.key == pygame.K_1:
    oneDown = True
    gameStart()
    newPlatformTimePoint = timePoint + 1000 # 1 second after start

当超过时间点时添加一个新的平台,并通过增加newPlatformInterval来设置下一个时间点。不能在比赛中改变(加速)间隔。你知道吗

if gameStarted is True: 
    drawingPlayer()
    movingPlayer()
    if timePoint > newPlatformTimePoint:
        newPlatformTimePoint = timePoint + newPlatformInterval
        creatingPlatform()
    movingPlatform()
    drawingPlatform()

主循环代码,以及应用建议:

newPlatformTimePoint = 0
newPlatformInterval = 200 # 0.2 seconds
while True:
    timePoint = pygame.time.get_ticks()

    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()
                newPlatformTimePoint = timePoint + 1000 # 1 second after start

        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()
        if timePoint > newPlatformTimePoint:
            newPlatformTimePoint = timePoint + newPlatformInterval
            creatingPlatform()
        movingPlatform()
        drawingPlatform()

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

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

    pygame.display.update()

相关问题 更多 >