0

I am making a game called Pizza Panic, which basically is a guy throwing pizzas down and you have to catch them. Now I want to increase the difficulty level once the player has reached certain scores. But when I try to increase the pizza's speed, the whole game seems to slow down. Below is my code:

class Pan(games.Sprite):
    image = games.load_image("pan.bmp", transparent=True)

    def __init__(self):
        super(Pan, self).__init__(image=Pan.image,
                                  bottom=games.screen.height,
                                  x=games.mouse.x)
        self.score = games.Text(value=0, size=25, color=color.black, right=games.screen.width-10, top=10)
        games.screen.add(self.score)
        self.level = 0

    def update(self):
        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_collide()
        if self.score.value > 200:
            text = games.Message(value='Increased difficulty',
                                 color=color.red,
                                 size=60,
                                 x=games.screen.width/2,
                                 y=games.screen.height/2,
                                 lifetime=100,
                                 after_death=Pizza.increased_speed)
            games.screen.add(text)

    def check_collide(self):
        for pizza in self.overlapping_sprites:
            self.score.value += 10
            self.score.right = games.screen.width-10
            pizza.handle_caught()


class Pizza(games.Sprite):
    image = games.load_image('pizza.bmp')
    speed = 1

    def __init__(self, x=90, y=90):
        super(Pizza, self).__init__(image=Pizza.image,
                                    y=y,
                                    x=x,
                                    dy=Pizza.speed)

    def handle_caught(self):
        self.destroy()

    def update(self):
        if self.bottom > games.screen.height:
            self.end_game()
            self.destroy()

    def end_game(self):
        end_message = games.Message(value='GAME OVER',
                                    size=90,
                                    color=color.red,
                                    x=games.screen.width / 2,
                                    y=games.screen.height / 2,
                                    lifetime=4 * games.screen.fps,
                                    after_death=games.screen.quit)
        games.screen.add(end_message)

    @staticmethod
    def increased_speed():
        Pizza.speed = 2
Bahrom
  • 4,752
  • 32
  • 41
Manh Nguyen Huu
  • 331
  • 1
  • 4
  • 12
  • The only variable that seems to change when doing this is `pizza.speed`. Where else do you use this variable? – Jacob H Nov 07 '16 at 18:12
  • Do you need to use a static method? Integers are pass-by-value, not pass-by-reference, so what you're trying to do won't work (under basic assumptions). The `dy` passed into the initializer will be stuck at the original `Pizza.speed`. If you want to change the `dy`, `self.dy = 2` might work, but I don't know the details of PyGame. – sudo Nov 07 '16 at 18:12
  • `increased_speed()` doesn't return anything, so it's unclear what assigning `None` to the `after_death` argument does in the `games.Message()` call is supposed to accomplish. – martineau Nov 07 '16 at 18:24
  • I don't use Pizza.speed anywhere else. And the static method does work, the new sprites do fly faster than the older ones, but the entire game is slowed down, and my increased difficulty message refuses to leave. I am thinking this has something to do with having to run conditional "if" every fps, but I don't know how to fix this. Please help. – Manh Nguyen Huu Nov 08 '16 at 03:40

0 Answers0