gorskaja2019

Untitled

May 15th, 2019
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. from tkinter import *
  2. import time
  3.  
  4. class Game:
  5. def __init__(self):
  6. self.root = Tk()
  7. self.canvas = Canvas(self.root, width = 800, height = 500)
  8. self.canvas.pack()
  9. self.root.update()
  10. self.bg = PhotoImage(file = 'bg.gif')
  11. for x in range(8):
  12. for y in range(5):
  13. self.canvas.create_image(100 * x,100 * y,image=self.bg,anchor='nw')
  14. self.sprites = []
  15. def mainloop(self):
  16. while True:
  17. for sprite in self.sprites:
  18. sprite.move()
  19. self.root.update_idletasks()
  20. self.root.update()
  21. time.sleep(0.01)
  22.  
  23. class Sprite:
  24. def __init__(self, game):
  25. self.game = game
  26. def move(self):
  27. pass
  28.  
  29. class Platform(Sprite):
  30. def __init__(self, game, photo_image, x, y):
  31. Sprite.__init__(self, game)
  32. self.photo_image = photo_image
  33. self.image = game.canvas.create_image(x,y,image=self.photo_image,anchor='nw')
  34.  
  35. class Fox(Sprite):
  36. def __init__(self, game):
  37. Sprite.__init__(self, game)
  38. self.images = [PhotoImage(file = 'fox.gif')]
  39. self.image = game.canvas.create_image(200, 400, image = self.images[0], anchor = 'nw')
  40. self.x = 2
  41. self.y = 0
  42. game.canvas.bind_all('<KeyPress-Left>',self.turn_left)
  43. game.canvas.bind_all('<KeyPress-Right>',self.turn_right)
  44. def turn_left(self, event):
  45. self.x = -2
  46. def turn_right(self, event):
  47. self.x = 2
  48. def move(self):
  49. self.game.canvas.move(self.image, self.x, self.y)
  50.  
  51. g = Game()
  52. platforma1 = Platform(g, PhotoImage(file='p.gif'), 20, 400)
  53. g.sprites.append(platforma1)
  54. platforma2 = Platform(g, PhotoImage(file='p.gif'), 50, 300)
  55. g.sprites.append(platforma2)
  56. f = Fox(g)
  57. g.sprites.append(f)
  58. g.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment