Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. import time, turtle, math
  2.  
  3. ticks_elapsed = 0
  4. running = True
  5. width = 800
  6. height = 600
  7.  
  8. window = turtle.Screen()
  9. window.setup(width, height)
  10. window.tracer(0, 0)
  11. window.delay(0)
  12.  
  13. '''
  14. This is how I did the movement for most of my games during your course
  15. using only the things you taught us.
  16.  
  17. The idea is you add a character (or ascii value) into a list of 'pressed keys'
  18. on the 'onkeypress' event, and remove it from the list on the 'onkeyrelease' event.
  19.  
  20. Then inside your game loop (that is locked at a certain interval now :D) you can refer
  21. to this list to check which keys are pressed, thus moving your player.
  22.  
  23. These events are both fired at the exact moment you press and release keys making the
  24. movement feel very responsive.
  25. '''
  26. pressed_keys = []
  27.  
  28.  
  29. def w_press():
  30. if "w" not in pressed_keys:
  31. pressed_keys.append("w")
  32. return
  33.  
  34.  
  35. def w_release():
  36. if "w" in pressed_keys:
  37. pressed_keys.remove("w")
  38. return
  39.  
  40.  
  41. def a_press():
  42. if "a" not in pressed_keys:
  43. pressed_keys.append("a")
  44. return
  45.  
  46.  
  47. def a_release():
  48. if "a" in pressed_keys:
  49. pressed_keys.remove("a")
  50. return
  51.  
  52.  
  53. def s_press():
  54. if "s" not in pressed_keys:
  55. pressed_keys.append("s")
  56. return
  57.  
  58.  
  59. def s_release():
  60. if "s" in pressed_keys:
  61. pressed_keys.remove("s")
  62. return
  63.  
  64.  
  65. def d_press():
  66. if "d" not in pressed_keys:
  67. pressed_keys.append("d")
  68. return
  69.  
  70.  
  71. def d_release():
  72. if "d" in pressed_keys:
  73. pressed_keys.remove("d")
  74. return
  75.  
  76.  
  77. window.listen()
  78. window.onkeypress(w_press, "w")
  79. window.onkeyrelease(w_release, "w")
  80. window.onkeypress(a_press, "a")
  81. window.onkeyrelease(a_release, "a")
  82. window.onkeypress(s_press, "s")
  83. window.onkeyrelease(s_release, "s")
  84. window.onkeypress(d_press, "d")
  85. window.onkeyrelease(d_release, "d")
  86.  
  87. '''
  88. This way of key input is great, although it is annoy having to make a 'press' and 'release'
  89. function for every key you want to use. I have a a solution! It's a tiny bit more technical,
  90. but still isn't hard to understand.
  91.  
  92. The alternative method is accessing the window's tkinter 'canvas' object for better key callback event executors.
  93.  
  94. ↓ Your turtle window
  95. canvas = window._root._canvas
  96. canvas.bind("<Key>", key_pressed)
  97. canvas.bind("<KeyRelease>", key_released)
  98.  
  99. This is the tkinter equivalent to window.onkeypress(...), only it doesn't take a key as an argument
  100. and instead is called every time any key is pressed. Because of this, it gives you a 'tkinter.Event'
  101. object, that contains all the information about the key event that you'll ever need. The important one
  102. being 'keycode', which is the ascii value of the key pressed.
  103.  
  104. def key_pressed(event):
  105. global PRESSED
  106. if event.keycode not in pressed_keys:
  107. pressed_keys.append(event.keycode)
  108. return
  109.  
  110.  
  111. def key_released(event):
  112. global PRESSED
  113. if event.keycode in pressed_keys:
  114. pressed_keys.remove(event.keycode)
  115. return
  116.  
  117. If this method is implemented, it would be easy for the programmer to add support for you arrow key newbies.
  118. '''
  119.  
  120. player = turtle.Turtle()
  121. player.shape("square")
  122. player.turtlesize(2, 2)
  123. player.hideturtle()
  124.  
  125. accel = 0.5
  126. decel = 0.5
  127. x_vel = 0
  128. y_vel = 0
  129. max_vel = 4
  130.  
  131. # Game logic update
  132. def update():
  133. global accel, decel, x_vel, y_vel, ticks_elapsed
  134.  
  135. # Some nasty movement that you can ignore!
  136. if "w" in pressed_keys:
  137. if y_vel < max_vel:
  138. y_vel += accel
  139. elif y_vel > 0:
  140. y_vel -= decel
  141. if y_vel < 0:
  142. y_vel = 0
  143. if "s" in pressed_keys:
  144. if y_vel > -max_vel:
  145. y_vel -= accel
  146. elif y_vel < 0:
  147. y_vel += decel
  148. if y_vel > 0:
  149. y_vel = 0
  150. if "d" in pressed_keys:
  151. if x_vel < max_vel:
  152. x_vel += accel
  153. elif x_vel > 0:
  154. x_vel -= decel
  155. if x_vel < 0:
  156. x_vel = 0
  157. if "a" in pressed_keys:
  158. if x_vel > -max_vel:
  159. x_vel -= accel
  160. elif x_vel < 0:
  161. x_vel += decel
  162. if x_vel > 0:
  163. x_vel = 0
  164. player.setx(player.xcor() + x_vel)
  165. player.sety(player.ycor() + y_vel)
  166.  
  167. # Cycling colors! :D
  168. sin = math.sin(math.radians(ticks_elapsed)) / 2 + 0.5
  169. cos = math.cos(math.radians(ticks_elapsed)) / 2 + 0.5
  170. player.color((sin, cos, 1 - cos))
  171. ticks_elapsed += 1
  172.  
  173. return
  174.  
  175. # Screen redraws
  176. def draw():
  177. player.clear()
  178. player.stamp()
  179. window.update()
  180. return
  181.  
  182. # Main game loop
  183. draws_per_second = 0
  184. updates_per_second = 0
  185. time_of_last_update = time.time()
  186. time_of_last_second = time.time()
  187. time_between_updates = 1 / 60
  188. while running:
  189. current_time = time.time()
  190. if current_time - time_of_last_update > time_between_updates:
  191. time_of_last_update += time_between_updates
  192. update()
  193. updates_per_second += 1
  194. draw()
  195. draws_per_second += 1
  196. if current_time - time_of_last_second > 1:
  197. time_of_last_second = current_time
  198. print("Screen Redraws Per Second:", draws_per_second, "Game Logic Updates Per Second:", updates_per_second)
  199. updates_per_second = 0
  200. draws_per_second = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement