【问题标题】:Pygame Balloon Pop Game: Balloon Class and ScorePygame气球流行游戏:气球类和分数
【发布时间】:2021-05-16 02:48:36
【问题描述】:

我有 2 种气球,一种蓝色和一种红色(50% 的几率生成红色或绿色气球)。如果弹出绿色气球,玩家将获得 2 分,如果弹出红色气球,玩家将获得 -2 分。 -1 如果绿色气球飞走了。 我创建了分数计数,但仅针对一个气球,这意味着如果我同时弹出红色和绿色气球但我被困在这里,分数有效(+2)。 我不知道 random.choice 是否是获得 50% 机会的最佳方式 这是一段代码:

class SpawnTime():
    def __init__(self, previous, time):
        self.time_previous = previous
        self.time= time


class Balloon():
    count = 0 

    def __init__(self):
        self.txr = random.choice(BALLOONS_TYPE)     
        self.width = self.txr.get_width()
        self.height = self.txr.get_height()        
        self.x = random.randint(0, SCREEN_WIDTH-self.width)
        self.y = SCREEN_HEIGHT
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
        self.speed = random.randint(MIN_BALLOON_SPEED, MAX_BALLOON_SPEED)
        self.active = True
        Balloon.count +=1 # add 1 to balloon counter

    def update(self):
        self.rect.y -=  self.speed

        if self.rect.bottom <= 0:  # off top of screen
            self.active= False


    def draw(self):
        SCREEN.blit(self.txr, self.rect)


class Player():
    def __init__(self):
        self.x = 0 
        self.y = 0
        self.txr = pygame.image.load('assets/sprites/player.png')
        self.width = self.txr.get_width()
        self.height = self.txr.get_height()
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height )
        self.score = 0

    def update(self):
        self.x, self.y = pygame.mouse.get_pos()
        self.rect.x= self.x
        self.rect.y= self.y


    def draw(self):
        SCREEN.blit(self.txr, self.rect)

    def clicked(self):
        # check if player has clicked on a balloon
        for b in balloons:
            if b.rect.collidepoint(self.x, self.y):
                self.score +=2
                b.active=False
                break # exit the for loop

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    不要使用ranfom.choice,而是使用random.randrange通过它的索引随机选择一个元素(参见random):

    class Balloon():
        count = 0 
    
        def __init__(self):
            self.balloon_type = random.randrange(len(BALLOONS_TYPE))
            img = BALLOONS_TYPE[self.balloon_type]
            self.width = img.get_width()
            self.height = img.get_height()     
            # [...]
        
        def draw(self):
            SCREEN.blit(BALLOONS_TYPE[self.balloon_type], self.rect)
    

    现在您可以根据气球的类型更改分数:

    class Player():
        # [...]
    
        def clicked(self):
            # check if player has clicked on a balloon
            for b in balloons:
                if b.rect.collidepoint(self.x, self.y):
                    
                    if b.balloon_type == 0:
                        self.score += 2
                    else
                        self.score -= 2
    
                    b.active=False
                    break # exit the for loop
    

    (不知道是红色气球还是绿色气球先出现,所以你可能需要交换+=2-=2

    【讨论】:

    • 感谢您的回答。这是我在使用您的代码时遇到的错误
    • self.width = self.balloon_type.get_width() AttributeError: 'int' object has no attribute 'get_width'
    • @Zisca 我已经扩展了答案。当然你必须使用BALLOONS_TYPE[self.balloon_type] 而不是self.txr
    • 当然。谢谢你。我应该将转义气球的代码放在同一个 for 循环块下吗?
    • @Zisca 不,我不这么认为。该函数名为clicked。可能只是在按下鼠标按钮时调用它。
    猜你喜欢
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多