gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:20:01 25 4
gpt4 key购买 nike

因此,在我之前的问题中,我收到了一个答案,该答案有助于按照我想要的方式定位平台。但是,有一个我无法解决的新问题。如何使平台之间具有足够的空间?(按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()。无论我做什么,它们总是成群结队地出现!我希望这是一款平台定期向玩家靠近的游戏,这样它实际上是可以玩的,但平台靠近的速度会随着时间的推移而增加,从而增加游戏的难度。我该怎么做呢?谢谢你! :)

最佳答案

我建议使用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()

关于python - Pygame - 如何均匀地分布随机生成的平台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54363067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com