我如何获得“分数”显示在游戏窗口中?
到目前为止,在我的贪吃蛇游戏中,分数是在 python shell 中打印的。我怎样才能将其打印在游戏窗口中?那么比赛和比分都在同一个窗口吗? 谢谢埃里克
while running:
screen.fill((0, 0, 0))
worm.move()
worm.draw()
food.draw()
if worm.crashed:
exit();
elif worm.x <= 0 or worm.x >= w -1:
running = False
elif worm.y <= 0 or worm.y >= h-1:
running = False
elif worm.position() == food.position():
score += 1
worm.eat()
print " score: %d" % score
food = Food(screen)
elif food.check(worm.x, worm.y):
score += 1
worm.eat()
print "score: %d" % score
food = Food(screen)
elif running == False:
exit();
for event in pygame.event.get():
if event.type == pygame.quit:
running = False
elif event.type == pygame.KEYDOWN:
worm.event(event)
pygame.display.flip()
clock.tick(100)
编辑-
while running:
screen.fill((0, 0, 0))
worm.move()
worm.draw()
food.draw()
pygame.font.init()
pygame.font.get_fonts()
if worm.crashed:
exit();
elif worm.x <= 0 or worm.x >= w -1:
running = False
elif worm.y <= 0 or worm.y >= h-1:
running = False
elif food.check(worm.x, worm.y):
score += 1
worm.eat()
food = Food(screen)
message = 'score: %d' % score
font = pygame.font.Font(None, 40)
text = font.render(message, 1, white)
screen.blit(text, (50, 50))
elif running == False:
exit();
for event in pygame.event.get():
if event.type == pygame.quit:
running = False
elif event.type == pygame.KEYDOWN:
worm.event(event)
为什么没有出现分数,我没有收到任何错误?
So far in my snake game the score is printed in python shell. How can i have it printed in the games window instead? So that the game and the score are in the same window?
Thanks Eric
while running:
screen.fill((0, 0, 0))
worm.move()
worm.draw()
food.draw()
if worm.crashed:
exit();
elif worm.x <= 0 or worm.x >= w -1:
running = False
elif worm.y <= 0 or worm.y >= h-1:
running = False
elif worm.position() == food.position():
score += 1
worm.eat()
print " score: %d" % score
food = Food(screen)
elif food.check(worm.x, worm.y):
score += 1
worm.eat()
print "score: %d" % score
food = Food(screen)
elif running == False:
exit();
for event in pygame.event.get():
if event.type == pygame.quit:
running = False
elif event.type == pygame.KEYDOWN:
worm.event(event)
pygame.display.flip()
clock.tick(100)
Edit-
while running:
screen.fill((0, 0, 0))
worm.move()
worm.draw()
food.draw()
pygame.font.init()
pygame.font.get_fonts()
if worm.crashed:
exit();
elif worm.x <= 0 or worm.x >= w -1:
running = False
elif worm.y <= 0 or worm.y >= h-1:
running = False
elif food.check(worm.x, worm.y):
score += 1
worm.eat()
food = Food(screen)
message = 'score: %d' % score
font = pygame.font.Font(None, 40)
text = font.render(message, 1, white)
screen.blit(text, (50, 50))
elif running == False:
exit();
for event in pygame.event.get():
if event.type == pygame.quit:
running = False
elif event.type == pygame.KEYDOWN:
worm.event(event)
Why isnt the Score Apearing, im not getting any errors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 font 模块在屏幕上渲染字体。
来自用户指南:
Use the font module to render fonts on screen.
From the userguide:
针对您的编辑:只有当您采用特定的 elif 分支时(即仅当蠕虫获得食物时),您才会将文本传输到屏幕上。在下一帧中,您清除了屏幕,但食物不在那里,因此不会绘制分数。相反,您应该将每一帧的乐谱传输到屏幕上。
In response to your edit: You're only blitting the text to the screen when you take that particular elif branch (i.e, only when the worm gets the food). On the next frame, you clear the screen, but the food isn't there, so the score doesn't get drawn. You should blit the score to the screen every frame, instead.