1 / 20

Topics in Python Graphics with Sound & Animation

Topics in Python Graphics with Sound & Animation. Professor Frank J. Rinaldo Creation Core - office 401. Graphics Collision Detection. # Slippery Pizza Program from livewires import games import random games.init(screen_width = 640, screen_height = 480, fps = 50) class Pan(games.Sprite):

ide
Download Presentation

Topics in Python Graphics with Sound & Animation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Topics in PythonGraphics with Sound & Animation Professor Frank J. Rinaldo Creation Core - office 401

  2. GraphicsCollision Detection # Slippery Pizza Program from livewires import games import random games.init(screen_width = 640, screen_height = 480, fps = 50) class Pan(games.Sprite): “”” A pan controlled by the mouse “”” def update(self): self.x = games.mouse.x self.y = games.mouse.y self.check_collider def check_collider(self): for pizza in self.overlapping_sprites: pizza.handle_collide()

  3. Slippery Pizza class Pizza(games.Sprite): “” A slippery pizza “”” def handle_collide(self): self.x = random.randrange(games.screen.width) self.y = random.randrange(games.screen.heigth) def main(): wall_image = games.load_image(“wall/jpg”, transparent = False) games.screen.background = wall_image pizza_image = games.load_image(“pizza.bmp”) pizza_x = random.randrange(games.screen.width) pizza_y = random.randrange(games.screen.height) the_pizza = Pizza(image = pizza_image, x = pizza_x, y = pizza_y) games.screen.add(the_pizza)

  4. Slippery Pizza continued pan_image = games.load_image(“pan.bmp”) the_pan = Pan(image = pan_image, x = games.mouse.x, y = games.mouse.y) games.screen.add(the_pan) games.mouse.is_visiable = False games.screen.event_grab = True games.screen.mainloop() # Kick it off main()

  5. Pizza Panic # Pizza Panic from livewires import games import random games.init(screen_width = 640, screen_height = 480, fps = 50) class Pan(games.Sprite): “” A pan controlled by player “”” image = game.load_image(“pan.bmp”) def __init__(self, y = 450): super(Pan, self).__init__(image = Pan.image, x = games.mouse.x, y = y) self.score = game.Text(value = 0, size = 25, color = coloc.black, x = 575, y = 20) game.screen.add(self.score) def update(self): self.x = game.mouse.x if self.left < 0: self.left = 0 if self.right > games.screen.width: self.right = game.screen.width self.check_catch()

  6. Pizza Paniccontinued def check_catch(self): for pizza in self.overlapping_sprites: self.score.value += 10 pizza.handle_caught() class Pizza(game.Sprite): “”” A pizza which falls to the ground “”” image = game.load.images(“pizza.bmp”) speed = 1 def __init__(self, x, y = 90): super(Pizza, self).__init__(image = Pizza.image, x = x, y = y, dy = Pizza.speed) def update(self): if self.bottom > games.screen.height: self.end_game() self.destroy() def handle_caught(self): self.destroy()

  7. Pizza Paniccontinued def end_game(self): end_message = games.Message(value = “Game Over”, size = 90, color = color.red, x = games.screen.width/2, x = games.screen.height/2, life_time = 5 * games.screen.fps, after_death = games.screen.quit) game.screen.add(end_message) class Chef(games.Sprite): “”” A chef which moves left & right “”” image = game.load_image(“chef.bmp”) def __init__(self, y = 55, speed = 2, odds_change = 200): 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

  8. Pizza Paniccontinued def update(self): if self.left < 0 or self.right > games.screen.width: self.dx = -self.dx elif random.randrange(self.odds_change) == 0: self.dx = -self.dx def check_drop(self): if self.time_til_drop > 0: self.time_til_drop -= 1 else: new_pizza = Pizza(x = self.x) games.screen.add(new_pizza) self.time_til_drop = int(65 / Pizza.speed)

  9. Pizza Paniccontinued def main(): wall_image = games.loadimage(“wall.jpg”, transparent = False) games.scren.background = wall_image the_chef = Chef() games.screen.add(the_chef) the_pan = Pan() games.screen.add(the_pan) games.mouse.is_visable = False games.screen.event_grab = True game.screen.mainloop() # start it up main()

  10. Keyboard input # Read Keyboard from livewires import games games.niit(screen_width = 640, screen_height = 48-, fps = 50) class Ship(games.Sprite): “”” A moving ship “”” def update(self): if games.keyboard.is_pressed(games.K_w): self.y = -= 1 if games.keyboard.is_pressed(games.K_s): self.y = += 1 if games.keyboard.is_pressed(games.K_a): self.x = -= 1 if games.keyboard.is_pressed(games.K_d): self.x = += 1

  11. Keyboard inputcontinued def main() nebula_image = games.load_image(“nebula.jpg”, transparent = False) games.screen.background = nebula_image ship_image = games.load_image(“ship.bmp”) the_ship = Ship(ship_image, x = games.screen.width/2, y = games.screen.height/2) games.screen.add(the_ship) games.screen.mainloop() # start game main()

  12. Rotating a sprite # Rotate Sprite from livewires import games games.init(screen_width = 640, screen_height = 480, fps = 50) class Ship(games.Sprite): “”” A Rotating Ship “”” def update(self): if games.keyboard.is_pressed(games.K_RIGHT): self.angle += 1 if games.keyboard.is_pressed(games.K_LEFT): self.angle -= 1 if games.keyboard.is_pressed(games.K_1): self.angle = 0 if games.keyboard.is_pressed(games.K_2): self.angle = 90 if games.keyboard.is_pressed(games.K_3): self.angle = 180 if games.keyboard.is_pressed(games.K_4): self.angle = 270

  13. Rotating a spritecontinued def main(): nebula_image = games.load_image(“nebula.jpg”, transparent = False) games.screen.background = nebula_image ship_image = games.load_image(“ship.bmp”) the_ship = Ship(ship_image, x = games.screen.width/2, y = games.screen.height/2) games.screen.add(the_ship) games.screen.mainloop() # Start the game main()

  14. Animation • What is an ‘animation’? • How do we ‘see’ something move or change? • Answer: • Show multiple images sequentially • One after the other • So… the ‘appears’ to be change or movement!

  15. Animating a sprite # Animating a Sprite from livewires import games games.init(screen_width = 640, screen_height = 480, fps = 50) nebula_image = games.load_image(“nebula.jpg”, transparent = False) games.screen.background = nebula_image explosion_files = [“explosion1.bmp”, “explosion2.bmp”, “explosion3.bmp”, “explosion4.bmp”, “explosion5.bmp”, “explosion6.bmp”, “explosion7.bmp”, “explosion8.bmp”, “explosion9.bmp”] explosion = games.Animation(images = explostion_files, x = games.screen.width/2, y = games.screen.height/2, n_repeats = 0, repeat_interval = 5) games.screen.add(explosion) games.screen.mainloop()

  16. Sound & Music # Sound and Music from livewires import games game.init(screen_width = 640, screen_height = 480, fps = 50) # load a sound missile_sound = games.load_sound(“missile.wav”) # load some music games.music.load(“theme.mid”)

  17. Sound & Musiccontinued # create a simple menu system choice = None while choice != “0”: print \ “”” Sound and Music 0 – Quit 1 – Play missile sound 2 – Loop missile sound 3 - Stop missile sound 4 – Play theme music 5 – Play theme music 6 - Stop theme music “”” choice = raw_imput(“Choice: “) print # exit if choice == 0: print “Good-bye”

  18. Sound & Musiccontinued # play missile sound elif choice == “1”: missile_sound.play() print “Playing missile sound.” # loop missile sound elif choice == “2”: loop = int(raw_imput(“Loop how many extra times? (-1 = forever): “)) missile_sound.play(loop) print “Looping missile sound.” # stop missile sound elif choice == “3”: missile_sound.stop() print “Stopping missile sound.”

  19. Sound & Musiccontinued #play theme music elif choice == “4”: games.music.play() print “Playing theme music.” # loop theme music elif choice == “5”: loop = int(raw_input(“Loop how many extra times? (-1 = forever): “)) games.music.play(loop) print “Looping theme music.” # stop theme music elif choice == “6”: games.music.stop() print “Stopping theme music.” # some unknown choice else: print “\nSorry, but”, choice, “isn’t a valid choice.” raw_input(“\n\nPress the enter key to exit.”)

  20. Homework • Due week 14 • Write a Python Program: • See problem #1 on page 365 of textbook. • In other words, improve Pizza Panic by slowly increasing the difficulty as the game progresses! • Include: • Simple Specification Document • Simple Pseudocode • Python code • Demonstrate program in class

More Related