函数重复十次而不是一次 - Pygame 零
Function is Repeating Ten Times Instead of Once - Pygame Zero
我在Pygame零中写了一个简单的赛车游戏。这来自 MagPi "Retro Gaming with Raspberry Pi" Book。我是一名初级程序员,所以我可能会问一个显而易见的问题。我想制作关卡。每 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/ticks,并且随着每个刻度更新级别,似乎当分数保持在 100 上大约 10 个刻度时,每个刻度都会更新级别?
由于我的程序很短,我将继续 post 所有代码:
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()
此外,您的游戏会在一段时间后滞后,因为列表 trackLeft
和 trackRight
在不断增长。从列表中删除底部 window 之外的障碍:
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()
我在Pygame零中写了一个简单的赛车游戏。这来自 MagPi "Retro Gaming with Raspberry Pi" Book。我是一名初级程序员,所以我可能会问一个显而易见的问题。我想制作关卡。每 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/ticks,并且随着每个刻度更新级别,似乎当分数保持在 100 上大约 10 个刻度时,每个刻度都会更新级别?
由于我的程序很短,我将继续 post 所有代码:
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()
此外,您的游戏会在一段时间后滞后,因为列表 trackLeft
和 trackRight
在不断增长。从列表中删除底部 window 之外的障碍:
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()