Pygame-如何均匀分布随机生成的平台?

时间:2019-01-25 10:09:28

标签: python pygame

因此,在我之前的问题中,我收到了一个答案,该答案可以按照我想要的方式帮助定向平台。但是,有一个新问题我无法解决。如何使平台之间留有足够的空间? (按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()。不管我做什么,它们总是成团出现!我希望这是一款平台定期进入玩家的游戏,因此它确实可以玩,但是随着时间的流逝,平台接近的速度会增加,从而增加了游戏难度。我将如何去做呢?谢谢! :)

1 个答案:

答案 0 :(得分:0)

我建议使用pygame.time.get_ticks()来获取程序运行的毫秒数。注意,由于时间以毫秒为单位,因此值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()