python - 创建一个自我更新的分数
问题描述
我检查了其他一些,但他们的例子与我想要实现的有点不同。目标是每player
投一球block
,分数就会上升十倍。对我来说,分数保持在恒定的 0 并且即使player
击中它的目标也不会更新。这是我所拥有的:
我正在使用pygame.font.SysFont
,因为pygame.freetype
对我来说有点太混乱了。
# Font
font = pygame.font.SysFont("Arial", 16)
# Colours
white = (255, 255, 255)
black = (0, 0, 0)
# Text
scoreValue = font.render("Score: " +str(score), True, black)
livesLeft = font.render("Health: " +str(health), True, black)
score = 0
health = 0
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
bullet_list.update()
for bullet in bullet_list:
block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
for block in block_hit_list:
bullet_list.remove(bullet)
sprite_list.remove(bullet)
score += 10
screen.fill(white)
screen.blit(scoreValue, (4, 550))
screen.blit(livesLeft, (4, 580))
sprite_list.draw(screen)
pygame.display.flip()
我缩短了上面的代码以仅显示我认为必要的内容,但是如果您需要查看完整的代码,我将直接在此消息下方添加它。在这个例子中,我有一个scoreValue
(忽略健康),但是当点击屏幕上的一个scoreValue
时,它不会在屏幕上更新。还有另一种方法来跟踪pygame中的分数吗?还是我只是做错了什么?player
blocks
这是完整的代码:
import pygame
pygame.init()
class Player(pygame.sprite.Sprite):
def __init__(self, color):
super().__init__()
self.image = pygame.Surface([25, 25])
self.image.fill(color)
self.color = color
self.rect = self.image.get_rect()
def moveRight(self, pixels):
if self.rect.x + pixels > 1000 - 50:
self.rect.x = 1000 - 50
self.rect.x += pixels
def moveLeft(self, pixels):
if self.rect.x - pixels < 0:
self.rect.x = 0
else:
self.rect.x -= pixels
class Block(pygame.sprite.Sprite):
def __init__(self, color):
super().__init__()
self.image = pygame.Surface([25, 25])
self.image.fill(color)
self.color = color
self.rect = self.image.get_rect()
def update(self):
self.rect.y +=5
class Bullet(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([4,10])
self.image.fill(blue)
self.rect = self.image.get_rect()
def update(self):
self.rect.y -=5
# Font
font = pygame.font.SysFont("Arial", 16)
# Colours
red = (255, 0, 0)
black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 0, 255)
screen = pygame.display.set_mode([800,600])
# Sprite Lists
sprite_list = pygame.sprite.Group()
block_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()
# Player
player = Player(red)
player.rect.x = 375
player.rect.y = 500
sprite_list.add(player)
score = 0
health = 3
# Block
margin = 50
for column in range(4):
for row in range(15):
block = Block(black)
block.rect.x = 30 + margin*row
block.rect.y = 25 + margin*column
block_list.add(block)
sprite_list.add(block)
# Text
scoreValue = font.render("Score: " +str(score), True, black)
livesLeft = font.render("Health: " +str(health), True, black)
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
bullet = Bullet()
bullet.rect.x = player.rect.x + 12
bullet.rect.y = player.rect.y
sprite_list.add(bullet)
bullet_list.add(bullet)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
player.moveLeft(3)
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
player.moveRight(3)
bullet_list.update()
for bullet in bullet_list:
block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
for block in block_hit_list:
bullet_list.remove(bullet)
sprite_list.remove(bullet)
score += 10
if bullet.rect.y < -10:
bullet_list.remove(bullet)
sprite_list.remove(bullet)
screen.fill(white)
screen.blit(scoreValue, (4, 550))
screen.blit(livesLeft, (4, 580))
sprite_list.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
解决方案
更改 的值score
不会神奇地更新scoreValue
Surface。Apygame.Surface
只是一个图像。您需要使用更改后的文本重新渲染scoreValue
Surface :
while not done:
# [...]
for bullet in bullet_list:
block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
for block in block_hit_list:
bullet_list.remove(bullet)
sprite_list.remove(bullet)
score += 10
scoreValue = font.render("Score: " + str(score), True, black)
推荐阅读
- sql - 在oracle中将一列数据拆分为多列
- r - 如何将 2 个数据集附加到另一个具有稍微不同的列名的下方?
- c++ - 如何从 std::async 任务返回 std::tuple
- highcharts - 将图像添加到图表上每个组的每列顶部
- javascript - Angular 5 - HttpClient XML Post - 解析期间的 Http 失败
- asp.net-mvc - 带有 Onclick 事件的 Razor EditorFor
- google-apps-script - 没有在清单的 api_console_project_id 字段中指定 id 的 API 控制台项目
- javafx - JavaFX 用 treeMap 数据填充 tableView
- java - 打开下载的文件并复制它
- android - 即使从“未优化的应用程序”中删除 PowerManager.isIgnoringBatteryOptimizations 也始终返回 true