当分数达到数百个位置时提高等级

时间:2016-07-29 02:01:08

标签: python-3.x

我有一个我正在进行的游戏,并且每次到达一百个地方时都试图提高等级,即100,200,300等等。

这是我的功能,这可能是我忘记做的非常简单

感谢@redunderthebed,我发现该函数位于__init__(),而且它没有进入if语句。所以我将我的关卡更改为if循环。所以我把它移到我的main()方法中,当得分变为100时,它仍然没有进入if循环。我在这里做错了什么?

# Pizza Panic
# Player must catch falling pizzas before they hit the ground


from livewires import games, color
from random import randrange


games.init(screen_width=640, screen_height=480, fps=50)


class Pan(games.Sprite):
    """ A pan controlled by player to catch falling pizzas """
    image = games.load_image("pan.bmp")

    def __init__(self):
        """ Initialize the pan object and create Text object for score """
        super(Pan, self).__init__(image=Pan.image,
                                  x=games.mouse.x,
                                  bottom=games.screen.height)
        self.score = games.Text(value=0, size=25, color=color.black,
                                top=5, right=games.screen.width - 10)
        games.screen.add(self.score)

        self.level = 0

    def update(self):
        """ move to mouse x position """
        self.x = games.mouse.x

        if self.left < 0:
            self.left = 0

        if self.right > games.screen.width:
            self.right = games.screen.width

        self.check_catch()

    def check_catch(self):
        """ Check if catch pizzas """
        for pizza in self.overlapping_sprites:
            self.score.value += 10
            self.score.right = games.screen.width - 10
            pizza.handle_caught()


class Pizza(games.Sprite):
    """ a pizza which falls to the ground """
    image = games.load_image("pizza.bmp")
    speed = 1

    def __init__(self, x, y=90):
        """ Initialize a pizza object """
        super(Pizza, self).__init__(image=Pizza.image,
                                    x=x, y=y,
                                    dy=Pizza.speed)

    def update(self):
        """ Check if bottom edge has reached screen bottom """
        if self.bottom > games.screen.height:
            self.end_game()
            self.destroy()

    def handle_caught(self):
        """ Destroy self if caught """
        self.destroy()

    def end_game(self):
        """ End the game """
        end_message = games.Message(value="Game Over",
                                    size=90,
                                    color=color.red,
                                    x=games.screen.width / 2,
                                    y=games.screen.height / 2,
                                    lifetime=5 * games.screen.fps,
                                    after_death=games.screen.quit)
        games.screen.add(end_message)


class Chef(games.Sprite):
    """ A chef which moves left and right, dropping pizzas """
    image = games.load_image("chef.bmp")

    def __init__(self, y=55, speed=2, odds_change=200):
        """ Initialize the chef object """
        super(Chef, self).__init__(image=Chef.image,
                                   x=games.screen.width / 2,
                                   y=y,
                                   dx=speed)
        self.odds_change = odds_change
        self.time_til_drop = 0

    def update(self):
        """ Determine if direction needs to be reversed """
        if self.left < 0 or self.right > games.screen.width:
            self.dx = -self.dx
        elif randrange(self.odds_change) == 0:
            self.dx = -self.dx

        self.check_drop()

    def check_drop(self):
        """ Decrease countdown or drop pizza and reset countdown """

        if self.time_til_drop > 0:
            self.time_til_drop -= 1
        else:
            new_pizza = Pizza(x=self.x)
            games.screen.add(new_pizza)

            # set buffer to approx 30% of pizza height, regardless of pizza
            # speed
            self.time_til_drop = int(new_pizza.height * 1.3 / Pizza.speed) + 1


def main():
    """ Play the game """
    wall_image = games.load_image("wall.jpg", transparent=False)
    games.screen.background = wall_image

    the_chef = Chef()
    games.screen.add(the_chef)

    the_pan = Pan()
    games.screen.add(the_pan)
    score = the_pan.score.value
    if score % 100 == 0:
        the_pan.level += 1
        the_pan.level_msg = games.Message(value="LEVEL {}"
                                          .format(the_pan.level),
                                          size=90,
                                          color=color.red,
                                          x=games.screen.width / 2,
                                          y=games.screen.height / 2,
                                          lifetime=5 * games.screen.fps,
                                          after_death=None)
        games.screen.add(the_pan.level_msg)
        pizza = Pizza(x=0)
        pizza.speed += 1
    else:
        print("not getting into the if statement")

    games.mouse.is_visible = False
    games.screen.event_grab = True
    games.screen.mainloop()


# start it up
main()

1 个答案:

答案 0 :(得分:1)

我找不到Livewires的任何真实文档,但我会根据你的代码说(并假设它正在做任何事情)你需要在这里进行等级检查:

def update(self):
    """ move to mouse x position """
    self.x = games.mouse.x

    if self.left < 0:
        self.left = 0

    if self.right > games.screen.width:
        self.right = games.screen.width

    self.check_catch()
    self.check_level()

也许check_level()

def check_level(self):
    if not self.score.value % 100:
        self.level += 1
        self.level_msg = games.Message(value="LEVEL {}"
                                       .format(the_pan.level),
                                       size=90,
                                       color=color.red,
                                       x=games.screen.width / 2,
                                       y=games.screen.height / 2,
                                       lifetime=5 * games.screen.fps,
                                       after_death=None)
    games.screen.add(the_pan.level_msg)