Advertisement
gorskaja2019

Untitled

May 22nd, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. from tkinter import *
  2. from time import *
  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. 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.  
  39. #self.images = [PhotoImage(file = 'fox.gif')]
  40. self.images_left = [PhotoImage(file = 'f1_left.gif'),
  41. PhotoImage(file = 'f2_left.gif'),
  42. PhotoImage(file = 'f3_left.gif'),
  43. PhotoImage(file = 'f4_left.gif'),
  44. PhotoImage(file = 'f5_left.gif'),
  45. PhotoImage(file = 'f6_left.gif')]
  46. self.images_right = [PhotoImage(file = 'f1_right.gif'),
  47. PhotoImage(file = 'f2_right.gif'),
  48. PhotoImage(file = 'f3_right.gif'),
  49. PhotoImage(file = 'f4_right.gif'),
  50. PhotoImage(file = 'f5_right.gif'),
  51. PhotoImage(file = 'f6_right.gif')]
  52.  
  53. self.image = game.canvas.create_image(200, 400, image = self.images_left[0], anchor = 'nw')
  54. self.x = -2
  55. self.y = 0
  56. self.current_image = 0
  57. self.last_time = time()
  58.  
  59.  
  60. game.canvas.bind_all('<KeyPress-Left>',self.turn_left)
  61. game.canvas.bind_all('<KeyPress-Right>',self.turn_right)
  62. def turn_left(self, event):
  63. self.x = -2
  64. def turn_right(self, event):
  65. self.x = 2
  66.  
  67. def animate(self):
  68. #на какой кадр? self.current_image
  69. if self.x != 0 and self.y == 0:
  70. if time() - self.last_time > 0.1:
  71. self.last_time = time()
  72. self.current_image = (self.current_image + 1) % 6
  73. if self.x < 0:
  74. if self.y == 0:
  75. self.game.canvas.itemconfig(self.image, image = self.images_left[self.current_image])
  76. elif self.x > 0:
  77. if self.y == 0:
  78. self.game.canvas.itemconfig(self.image, image = self.images_right[self.current_image])
  79.  
  80. def move(self):
  81. self.animate()
  82. self.game.canvas.move(self.image, self.x, self.y)
  83.  
  84. g = Game()
  85. platforma1 = Platform(g, PhotoImage(file='pl1.gif'), 20, 400)
  86. g.sprites.append(platforma1)
  87. platforma2 = Platform(g, PhotoImage(file='pl1.gif'), 50, 300)
  88. g.sprites.append(platforma2)
  89. f = Fox(g)
  90. g.sprites.append(f)
  91. g.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement