我一直致力于一项任务,要求我们使用pygame创建Atari的Break-Out版本。一切都进行得非常顺利,但我仍然坚持如何制作它,以便不同颜色的砖块在击中时返回不同的点数。 (例如,红色= 1点,蓝色= 2点......)
这里是游戏循环代码的一部分:
while keepGoing:
#collision detection between ball and bricks
brick_hit = pygame.sprite.spritecollide(ball, brick_group, False)
if brick_hit:
ball.change_direction()
#scorekeeper
for hit in brick_hit:
scorekeeper.point(1)
hit.kill()
if scorekeeper.winner() == 1:
break
这里是砖块的代码:
#appending brick sprites into a list so its coordinates can be easily manipulated.
bricks = []
x = -35
y = 100
colour = (0, 255, 0)
points = 6
for i in range(108):
x += 35
if x == screen.get_width():
y += 35
x = 0
if y == 135:
colour = (255, 0, 0)
points = 5
elif y == 170:
colour = (0, 0, 255)
points = 4
elif y == 205:
colour = (120, 0, 0)
points = 3
elif y == 240:
colour = (0, 0, 90)
points = 2
elif y == 275:
colour = (0, 0, 200)
points = 1
brick = pyBreakoutSprites.Brick(screen, x, y, colour)
bricks.append(brick)
这里是一个单独模块中的Scorekeeper课程:
class Scorekeeper(pygame.sprite.Sprite):
def __init__(self, screen):
pygame.sprite.Sprite.__init__(self)
self.__font = pygame.font.SysFont("Helvetica", 20)
self.__player_score = 0
self.__screen = screen
def point(self, score):
self.__player_score += score
def update(self):
message = "Points: %d" %(self.__player_score)
self.image = self.__font.render(message, 1, (255, 255, 255))
self.rect = self.image.get_rect()
self.rect.center = (200, self.__screen.get_height()-10)
我尝试使用if语句,以便当brick是某种颜色时,scorekeeper.point(1)中的参数会更改为另一个数字,但所有砖块在命中时仍会返回一个点。
我希望得到任何帮助。谢谢!
答案 0 :(得分:0)
让Brick接受分数作为其init方法的一部分。然后,在循环中,只需执行pyBreakoutSprites.Brick(screen, x, y, colour, points)
,然后当您检查碰撞时,您可以score += brick.score
。