Форум сайта python.su
Не могу понять, почему объекты спрайтов появляются так часто?
from livewires import games, color games.init(screen_width=640, screen_height=480, fps=50) import random class Pan(games.Sprite): image = games.load_image("pan.bmp") def __init__(self): super(Pan, self).__init__(image=Pan.image, x=games.mouse.x, bottom=games.screen.height - 40) self.score = games.Text(value=0, size=25, color=color.black, top=5, right=games.screen.width - 10) games.screen.add(self.score) 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: pizza.end_game() self.destroy() class Falling(games.Sprite): image = games.load_image("pizza.bmp") speed = 1 time_til_drop = 0 def __init__(self, x, y = 90): """ Initialize a Pizza object. """ super(Falling, self).__init__(image = Falling.image, x = x, y = y, dy = Falling.speed) def update(self): """ Decrease countdown or drop pizza and reset countdown. """ if self.time_til_drop > 0: self.time_til_drop -= 1 else: new_pizza = Falling(x=random.randrange(1, games.screen.width, 15)) 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 / Falling.speed) + 1 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) def main(): """ Play the game. """ wall_image = games.load_image("wall.jpg", transparent = False) games.screen.background = wall_image pizza = Falling(x=0) games.screen.add(pizza) the_pan = Pan() games.screen.add(the_pan) games.mouse.is_visible = False games.screen.event_grab = True games.screen.mainloop() # start it up! main()
Прикреплённый файлы: 2016-12-09_16-36-38.png (418,5 KБ)
Офлайн