【问题标题】:efficient way to make different coloured sprites worth different points? (pygame)使不同颜色的精灵价值不同的有效方法? (游戏)
【发布时间】:2016-04-25 23:15:03
【问题描述】:

我一直在做一个任务,要求我们用 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)

这是单独模块中的记分员类:

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 语句,以便当砖块具有某种颜色时, scorekeeper.point(1) 中的参数会更改为另一个数字,但所有砖块在被击中时仍会返回一个点。

如果能得到任何帮助,我将不胜感激。谢谢!

【问题讨论】:

    标签: python python-2.7 pygame


    【解决方案1】:

    让 Brick 接受分数作为其 init 方法的一部分。然后,在循环中,只做pyBreakoutSprites.Brick(screen, x, y, colour, points),然后当你检查碰撞时,你可以只做score += brick.score

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 2012-11-11
      • 2021-05-26
      相关资源
      最近更新 更多