Advertisement
gorskaja2019

Untitled

May 22nd, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 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. game.canvas.bind_all('<KeyPress-Left>',self.turn_left)
  60. game.canvas.bind_all('<KeyPress-Right>',self.turn_right)
  61. def turn_left(self, event):
  62. self.x = -2
  63. def turn_right(self, event):
  64. self.x = 2
  65.  
  66. def animate(self):
  67. #на какой кадр? self.current_image
  68. if self.x != 0 and self.y == 0:
  69. if time() - self.last_time > 0.1:
  70. self.last_time = time()
  71. self.current_image = (self.current_image + 1) % 6
  72. if self.x < 0:
  73. if self.y == 0:
  74. self.game.canvas.itemconfig(self.image, image = self.images_left[self.current_image])
  75. elif self.x > 0:
  76. if self.y == 0:
  77. self.game.canvas.itemconfig(self.image, image = self.images_right[self.current_image])
  78.  
  79. def move(self):
  80. self.animate()
  81. self.game.canvas.move(self.image, self.x, self.y)
  82.  
  83. class Coords:
  84. def __init__(self, x1=0,y1=0,x2=0,y2=0):
  85. self.x1 = x1
  86. self.x2 = x2
  87. self.y1 = y1
  88. self.y2 = y2
  89.  
  90. def intersection_x(obj1, obj2):
  91. A = max(obj1.x1, obj2.x1)
  92. B = min(obj1.x2, obj2.x2)
  93. return B > A
  94.  
  95. def intersection_y(obj1, obj2):
  96. A = max(obj1.y1, obj2.y1)
  97. B = min(obj1.y2, obj2.y2)
  98. return B > A
  99.  
  100. def int_left(obj1, obj2):
  101. return intersection_y(obj1, obj2) and obj2.x1 <= obj1.x1 and obj1.x1 <= obj2.x2
  102.  
  103. def int_right(obj1, obj2):
  104. return intersection_y(obj1, obj2) and obj2.x1 <= obj1.x2 and obj1.x2 <= obj2.x2
  105.  
  106. def int_top(obj1, obj2):
  107. return intersection_x(obj1, obj2) and obj2.y1 <= obj1.y1 and obj1.y1 <= obj2.y2
  108.  
  109. def int_bottom(d, obj1, obj2):
  110. return intersection_x(obj1, obj2) and obj2.y1 <= obj1.y2 + d and obj1.y2 + d <= obj2.y2
  111.  
  112.  
  113. g = Game()
  114. platforma1 = Platform(g, PhotoImage(file='pl1.gif'), 20, 400)
  115. g.sprites.append(platforma1)
  116. platforma2 = Platform(g, PhotoImage(file='pl1.gif'), 50, 300)
  117. g.sprites.append(platforma2)
  118. f = Fox(g)
  119. g.sprites.append(f)
  120. g.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement