python - 如何获得分数以更新并显示在屏幕上?
问题描述
当玩家触摸硬币时,分数会更新并显示,分数会重叠,例如 1、2、3 等,因此看不清分数。我试图得到它,所以以前的分数消失了,只显示新的分数。
def main(): #my main loop
running = True
clock = pygame.time.Clock() # A clock to limit the frame rate.
score = (1)
score = str(score)
myfont = pygame.font.SysFont('OpenSans', 30)
textsurface = myfont.render('Level ONE: Greenland', False, (0, 0, 0))
background.blit(textsurface,(500,10))
textsurface = myfont.render('Score:', False, (0, 0, 0))
background.blit(textsurface,(10,10))
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
player_hit_list = pygame.sprite.spritecollide(player, coin_list,True)
for coin in player_hit_list:
textsurface = myfont.render(score, False, (0, 0, 0))
background.blit(textsurface,(90,10))
score = str(int(score)+ 1))
sprites.update()
screen.blit(background, (0, 0))
sprites.draw(screen) # Draws all of the sprites onto the screen
clock.tick(60) # Limit the frame rate to 60 FPS.
pygame.display.update()
我除了在左上角显示分数:1 并且当硬币被触摸时它会更新,例如分数:
解决方案
使用PyGame 表面 subsurface()
函数,复制位于核心正下方的背景部分,可能会添加一些额外的内容以处理核心中的更多数字。
然后更新分数,通过写入背景的那部分擦除现有分数,然后绘制文本位图。
就像是:
# rectangle around score
under_score_rect = [ 90, 10, 100, 40 ]
# copy of background that's under the score
under_score_background = background.subsurface( under_score_rect ).copy()
...
def drawScore( score, screen, background_img ):
global score_font
textsurface = score_font.render( score, False, (0, 0, 0) )
screen.blit( background_img, ( 90, 10 ) )
screen.blit( textsurface, ( 90,10 ) )
...
drawScore( score, window, under_score_background )
推荐阅读
- node.js - 节点 js CORS 失败
- python - 如何使用fuzzywuzzy连接两个数据集
- node.js - Puppeteer 集群有时无法在 k8s 中启动
- arrays - 我在 Swift 'NSInvalidArgumentException' 中收到此错误
- kotlin - 按键列表分组 (Kotlin)
- sql - 存储要在存储过程中使用的标识值
- ios - 相对内存引用问题
- sapui5 - SAPUI5 Variant 键保存奇怪的行为
- makefile - 使用 automake 生成随环境变化的 Makefile 的规范方法
- sql - 获取 Oracle Sql 中链(树)中任何元素的最顶层元素