Guest User

My Code

a guest
Dec 14th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. import tkinter
  2. import math
  3. from PIL import ImageTk, Image
  4.  
  5. # initialize our window to display graphics
  6. window = tkinter.Tk()
  7. window.title("bad game")
  8. HEIGHT = window.winfo_screenheight() # finds height of window
  9. WIDTH = window.winfo_screenwidth() # finds width of window
  10. window.geometry("{1}x{0}".format(int(HEIGHT / 2), int(WIDTH / 2)))
  11.  
  12. c = tkinter.Canvas(window, height=HEIGHT * 2, width=WIDTH * 2,
  13. highlightthickness=0)
  14. c.place(x=WIDTH / 2 + (WIDTH / 4), y=HEIGHT / 2, anchor="center") # put the canvas widget inside our window
  15. window.wm_attributes("-transparentcolor", "white")
  16. for i in range(0, HEIGHT, 10):
  17. c.create_line(i, 0, i, WIDTH, fill="light grey")
  18.  
  19. for i in range(0, WIDTH, 10):
  20. c.create_line(0, i, HEIGHT, i, fill="light grey")
  21.  
  22.  
  23. class Player:
  24. def __init__(self, movement_speed, coords, color):
  25. """
  26. size parameter: adjusts size of player
  27. movement_speed parameter: adjusts speed of our player
  28. coords parameter: sets spawn coordinates of player
  29. """
  30. self.coords = coords
  31. self.movement_speed = movement_speed
  32. self.color = color
  33.  
  34. def spawn(self, coords, movement, canv_coord_x, canv_coord_y, move):
  35. p = Image.open("tank.png")
  36. p2 = ImageTk.PhotoImage(p)
  37. p2.image = p2
  38.  
  39. self.movement = movement
  40. self.canv_coord_x = WIDTH / 2 + (WIDTH / 4)
  41. self.canv_coord_y = HEIGHT / 2
  42. self.player_coord_x = coords[0]
  43. self.player_coord_y = coords[1]
  44. self.move = move
  45.  
  46. c.place(x=WIDTH / 2 + (WIDTH / 4), y=HEIGHT / 2, anchor="center")
  47. player2 = c.create_image(coords[0], coords[1], image=p2)
  48.  
  49. def mouse_movement(self, angle, event = "<Motion>"):
  50. x, y = event.x, event.y
  51. relx, rely = x - self.player_coord_x, self.player_coord_y
  52. self.angle = (180 / math.pi) * -math.atan2(rely, relx)
  53. p = Image.open("tank.png")
  54. p.rotate(angle)
  55. p2 = ImageTk.PhotoImage(p)
  56. p2.image = p2
  57. player2 = c.create_image(self.coords[0], self.coords[1], image=p2)
  58.  
  59. def movement2(self, event="<Key>"):
  60. key = event.keysym
  61. if key == "Right" or key == "d":
  62. c.delete("all")
  63. for i in range(0, HEIGHT, 10):
  64. c.create_line(i, 0, i, WIDTH, fill="light grey")
  65.  
  66. for i in range(0, WIDTH, 10):
  67. c.create_line(0, i, HEIGHT, i, fill="light grey")
  68. p = Image.open("tank.png")
  69. p.rotate(self.angle)
  70. p2 = ImageTk.PhotoImage(p)
  71. p2.image = p2
  72. self.canv_coord_x -= self.move
  73. self.coords[0] += self.move
  74. c.place(x=self.canv_coord_x, y=self.canv_coord_y, anchor="center")
  75. player2 = c.create_image(self.coords[0], self.coords[1], image=p2)
  76.  
  77. elif key == "Left" or key == "a":
  78. c.delete("all")
  79. for i in range(0, HEIGHT, 10):
  80. c.create_line(i, 0, i, WIDTH, fill="light grey")
  81.  
  82. for i in range(0, WIDTH, 10):
  83. c.create_line(0, i, HEIGHT, i, fill="light grey")
  84. p = Image.open("tank.png")
  85. p.rotate(self.angle)
  86. p2 = ImageTk.PhotoImage(p)
  87. p2.image = p2
  88. self.canv_coord_x += self.move
  89. self.coords[0] -= self.move
  90. c.place(x=self.canv_coord_x, y=self.canv_coord_y, anchor="center")
  91. player2 = c.create_image(self.coords[0], self.coords[1], image=p2)
  92. elif key == "Up" or key == "w":
  93. c.delete("all")
  94. for i in range(0, HEIGHT, 10):
  95. c.create_line(i, 0, i, WIDTH, fill="light grey")
  96.  
  97. for i in range(0, WIDTH, 10):
  98. c.create_line(0, i, HEIGHT, i, fill="light grey")
  99. p = Image.open("tank.png")
  100. p.rotate(self.angle)
  101. p2 = ImageTk.PhotoImage(p)
  102. p2.image = p2
  103. self.canv_coord_y += self.move
  104. self.coords[1] -= self.move
  105. c.place(x=self.canv_coord_x, y=self.canv_coord_y, anchor="center")
  106. player2 = c.create_image(self.coords[0], self.coords[1], image=p2)
  107. elif key == "Down" or key == "s":
  108. c.delete("all")
  109. for i in range(0, HEIGHT, 10):
  110. c.create_line(i, 0, i, WIDTH, fill="light grey")
  111.  
  112. for i in range(0, WIDTH, 10):
  113. c.create_line(0, i, HEIGHT, i, fill="light grey")
  114. p = Image.open("tank.png")
  115. p.rotate(self.angle)
  116. p2 = ImageTk.PhotoImage(p)
  117. p2.image = p2
  118. self.canv_coord_y -= self.move
  119. self.coords[1] += self.move
  120. c.place(x=self.canv_coord_x, y=self.canv_coord_y, anchor="center")
  121. player2 = c.create_image(self.coords[0], self.coords[1], image=p2)
  122.  
  123.  
  124.  
  125. player = Player(10, [WIDTH / 2, HEIGHT / 2 + (HEIGHT / 4)], "blue")
  126. player.spawn([WIDTH / 2, HEIGHT / 2 + (HEIGHT / 4)], 3, WIDTH / 2, HEIGHT / 2, 3)
  127.  
  128.  
  129. def main_game_loop():
  130. c.bind_all("<Key>", player.movement2)
  131. c.bind_all("<Motion>", lambda x2: player.mouse_movement(180))
  132. window.after(60, lambda: main_game_loop())
  133.  
  134.  
  135. main_game_loop()
  136.  
  137. window.mainloop() # so we can actually see stuff
  138.  
Advertisement
Add Comment
Please, Sign In to add comment