gpt4 book ai didi

python - 函数重复十次而不是一次 - Pygame Zero

转载 作者:行者123 更新时间:2023-12-04 10:33:46 27 4
gpt4 key购买 nike

我在 Pygame Zero 中编写了一个简单的赛车游戏。这来自 MagPi“使用 Raspberry Pi 进行复古游戏”一书。我是一名初级程序员,所以我可能会问一个明显的问题。我想制作关卡。每 100 score , level + 1. 赢得 10 个级别。

感兴趣的领域可能是我的 draw()功能:

    global gameStatus, level
screen.fill((128, 128, 128))
if gameStatus == 0:
car.draw()
b = 0
while b < len(trackLeft):
trackLeft[b].draw()
trackRight[b].draw()
b += 1
screen.draw.text("Score: " + str(score), (50, 30), color="black")
if score % 100 == 0:
check_levels()
screen.draw.text("Level: " + str(level), (50, 50), color="black")
if gameStatus == 1:
screen.blit('rflag', (318, 268))
if gameStatus == 2:
screen.blit('cflag', (318, 268))

和我的 level_up()功能:
    global level
if level <= 10:
level += 1
if level == 10:
gameStatus == 2

每制作一首曲子就计分,得分高一个。

我的问题 : 当我达到 100 score , 级别加 10,而不是加 1。我不知道似乎发生了什么。可能是因为每个分数都有一些 FPS/tick,并且随着关卡每刻更新一次,似乎当分数在 100 上停留大约 10 个刻时,级别每刻更新一次?

由于我的程序很短,我将继续发布所有代码:
import time
from random import randint
import pygame
import pgzrun


WIDTH = 700
HEIGHT = 800


car = Actor("racecar")
car.pos = 250, 700
SPEED = 4
trackLeft = []
trackRight = []
trackCount = 0
trackPosition = 250
trackWidth = 120
trackDirection = False
gameStatus = 0
score = 0
level = 1


def draw():
global gameStatus, level
screen.fill((128, 128, 128))
if gameStatus == 0:
car.draw()
b = 0
while b < len(trackLeft):
trackLeft[b].draw()
trackRight[b].draw()
b += 1
screen.draw.text("Score: " + str(score), (50, 30), color="black")
if score % 100 == 0:
check_levels()
screen.draw.text("Level: " + str(level), (50, 50), color="black")
if gameStatus == 1:
screen.blit('rflag', (318, 268))
if gameStatus == 2:
screen.blit('cflag', (318, 268))


def update():
global gameStatus, trackCount
if gameStatus == 0:
if keyboard.left: car.x -= 2
if keyboard.right: car.x += 2
update_track()


def make_track():
global trackCount, trackLeft, trackRight, trackPosition, trackWidth, score
trackLeft.append(Actor("barrier", pos=(trackPosition - trackWidth, 0)))
trackRight.append(Actor("barrier", pos=(trackPosition + trackWidth, 0)))
trackCount += 1
score += 1


def update_track():
global trackCount, trackPosition, trackDirection, trackWidth, gameStatus
b = 0
while b < len(trackLeft):
if car.colliderect(trackLeft[b]) or car.colliderect(trackRight[b]):
gameStatus = 1
trackLeft[b].y += SPEED
trackRight[b].y += SPEED
b += 1
if trackLeft[len(trackLeft) - 1].y > 32:
if trackDirection == False: trackPosition += 16
if trackDirection == True: trackPosition -= 16
if randint(0, 4) == 1: trackDirection = not trackDirection
if trackPosition > 700 - trackWidth: trackDirection = True
if trackPosition < trackWidth: trackDirection = False
make_track()


def check_levels():
global level
if level <= 10:
level += 1
if level == 10:
gameStatus = 2


make_track()


pgzrun.go()

最佳答案

您必须在分数更新时进行级别检查。注意,draw执行得更频繁,然后分数更新,所以 if score % 100 == 0:多次满足并且级别增加不止一次:

def draw():
global gameStatus, level
screen.fill((128, 128, 128))
if gameStatus == 0:
# [...]

screen.draw.text("Score: " + str(score), (50, 30), color="black")

# DELETE <--------------------------
#if score % 100 == 0:
# check_levels()

screen.draw.text("Level: " + str(level), (50, 50), color="black")

def make_track():
#[...]
trackCount += 1
score += 1

# ADD <-----------------------------
if score % 100 == 0:
check_levels()

此外,您的游戏会在一段时间后滞后,因为列表 trackLefttrackRight正在不断增长。从列表中删除底部窗口之外的障碍:

def make_track():
global trackCount, trackLeft, trackRight, trackPosition, trackWidth, score
trackLeft.append(Actor("barrier", pos=(trackPosition - trackWidth, 0)))
trackRight.append(Actor("barrier", pos=(trackPosition + trackWidth, 0)))

# remove barriers which are out of screen
if trackLeft[0].y >= HEIGHT:
del trackLeft[0]
if trackRight[0].y >= HEIGHT:
del trackRight[0]

trackCount += 1
score += 1
if score % 100 == 0:
check_levels()

关于python - 函数重复十次而不是一次 - Pygame Zero,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60287254/

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