如何在Pygame中创建级别系统?
我正在制作一个简单的数学游戏,其中有5个问题的设定顺序。我目前仅将前两个问题作为功能以及基于玩家答案所调用的“胜利”和“损失”功能实现。但是,我无法弄清楚如何建立适当的级别系统。这是我实施的四个函数的当前代码:
def victory():
global num_correct
global time
while True:
screen.fill((150, 200, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
victory_text = font.render("Correct!", True, dark_blue)
time -= 1
timer.tick(100)
if time <= 0:
second_problem()
score_display(30, 25)
screen.blit(victory_text, (325, 240))
pygame.display.update()
# when incorrect answer is chosen, call this function
def loss():
global num_correct
global time
while True:
screen.fill((150, 200, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
loss_text = font.render("Incorrect!", True, dark_blue)
time -= 1
timer.tick(100)
if time <= 0:
second_problem()
score_display(30, 25)
screen.blit(loss_text, (310, 240))
pygame.display.update()
# first math problem (9 - 6) + 3 = 6
def first_problem():
global num_correct
while True:
screen.fill((150, 200, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
gametext = font.render("(9 - 6) + 3 = ", True, dark_blue)
if six.draw(screen):
print("true")
num_correct += 1
victory()
if eight.draw(screen):
print("false")
loss()
score_display(30, 25)
screen.blit(gametext, (290, 75))
pygame.display.update()
# second math problem 3 + (4 * 1) = 7
def second_problem():
global num_correct
while True:
screen.fill((150, 200, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
gametext = font.render("3 + (4 * 1) = ", True, dark_blue)
if seven.draw(screen):
print("true")
num_correct += 1
victory()
if nine.draw(screen):
print("false")
loss()
score_display(30, 25)
screen.blit(gametext, (290, 75))
pygame.display.update()
当前“胜利”和“损失”仅涉及第二个问题。我计划总共实施5个。如何创建一个级别的系统来调用设定顺序的问题?
I am making a simple math game that has a set order of 5 questions. I currently only have the first two problems implemented as functions as well as "victory" and "loss" functions that are called based on the player answer. However, I cannot figure out how to make a proper level system. Here is the current code for the four functions I have implemented:
def victory():
global num_correct
global time
while True:
screen.fill((150, 200, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
victory_text = font.render("Correct!", True, dark_blue)
time -= 1
timer.tick(100)
if time <= 0:
second_problem()
score_display(30, 25)
screen.blit(victory_text, (325, 240))
pygame.display.update()
# when incorrect answer is chosen, call this function
def loss():
global num_correct
global time
while True:
screen.fill((150, 200, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
loss_text = font.render("Incorrect!", True, dark_blue)
time -= 1
timer.tick(100)
if time <= 0:
second_problem()
score_display(30, 25)
screen.blit(loss_text, (310, 240))
pygame.display.update()
# first math problem (9 - 6) + 3 = 6
def first_problem():
global num_correct
while True:
screen.fill((150, 200, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
gametext = font.render("(9 - 6) + 3 = ", True, dark_blue)
if six.draw(screen):
print("true")
num_correct += 1
victory()
if eight.draw(screen):
print("false")
loss()
score_display(30, 25)
screen.blit(gametext, (290, 75))
pygame.display.update()
# second math problem 3 + (4 * 1) = 7
def second_problem():
global num_correct
while True:
screen.fill((150, 200, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
gametext = font.render("3 + (4 * 1) = ", True, dark_blue)
if seven.draw(screen):
print("true")
num_correct += 1
victory()
if nine.draw(screen):
print("false")
loss()
score_display(30, 25)
screen.blit(gametext, (290, 75))
pygame.display.update()
Currently "victory" and "loss" only advance to the second problem. I plan to implement 5 in total. How can I create a level system of sorts to call upon the problems in a set order?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以减少代码。而不是
first_problem()
和second_problem()
您可以使用一个具有不同值的函数,并且函数应返回 true 或
false代码>代替运行
victor()
或lose()
,然后您可以将值保留在列表上,并使用 loop - loop,
您将减少<<。代码> victor()和
lose()
带有参数消息('corment!')
,message('Corrent!')
,然后您可以进行完整的工作代码,并进行更多更改。
You could reduce your code. Instead of
first_problem()
andsecond_problem()
you could use one function with different valuesAnd function should return
True
orFalse
instead of runningvictory()
orloss()
And then you could keep values on list and use
for
-loopYou would reduce
victory()
andloss()
to one function with parametermessage('Corrent!')
,message('Corrent!')
and then you could doFull working code with more changes.