问题描述
我已经检查了一些其他人,但他们的示例与我试图实现的目标略有不同。目标是 player
射出 block
,分数将增加 10。对我来说,分数保持不变,即使 player
命中目标也不会更新。这是我所拥有的:
我使用 pygame.font.SysFont
是因为 pygame.freetype
对我来说有点太混乱了。
# Font
font = pygame.font.SysFont("Arial",16)
# Colours
white = (255,255,255)
black = (0,0)
# Text
scoreValue = font.render("score: " +str(score),True,black)
livesLeft = font.render("Health: " +str(health),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,580))
sprite_list.draw(screen)
pygame.display.flip()
我缩短了上面的代码以仅显示我认为必要的内容,但是如果您需要查看完整代码,我会直接在此消息下方添加它。在此示例中,我有一个 scoreValue
(忽略运行状况),但是当 scoreValue
击中屏幕上的 player
之一时,blocks
不会在屏幕上更新。有没有另一种方法可以在 pygame 中跟踪分数?还是我只是做错了什么?
完整代码如下:
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,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)
black = (0,0)
white = (255,255)
blue = (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),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,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,580))
sprite_list.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
解决方法
更改 score
的值不会神奇地更新 scoreValue
Surface。 pygame.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)