Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time, turtle, math
- ticks_elapsed = 0
- running = True
- width = 800
- height = 600
- window = turtle.Screen()
- window.setup(width, height)
- window.tracer(0, 0)
- window.delay(0)
- '''
- This is how I did the movement for most of my games during your course
- using only the things you taught us.
- The idea is you add a character (or ascii value) into a list of 'pressed keys'
- on the 'onkeypress' event, and remove it from the list on the 'onkeyrelease' event.
- Then inside your game loop (that is locked at a certain interval now :D) you can refer
- to this list to check which keys are pressed, thus moving your player.
- These events are both fired at the exact moment you press and release keys making the
- movement feel very responsive.
- '''
- pressed_keys = []
- def w_press():
- if "w" not in pressed_keys:
- pressed_keys.append("w")
- return
- def w_release():
- if "w" in pressed_keys:
- pressed_keys.remove("w")
- return
- def a_press():
- if "a" not in pressed_keys:
- pressed_keys.append("a")
- return
- def a_release():
- if "a" in pressed_keys:
- pressed_keys.remove("a")
- return
- def s_press():
- if "s" not in pressed_keys:
- pressed_keys.append("s")
- return
- def s_release():
- if "s" in pressed_keys:
- pressed_keys.remove("s")
- return
- def d_press():
- if "d" not in pressed_keys:
- pressed_keys.append("d")
- return
- def d_release():
- if "d" in pressed_keys:
- pressed_keys.remove("d")
- return
- window.listen()
- window.onkeypress(w_press, "w")
- window.onkeyrelease(w_release, "w")
- window.onkeypress(a_press, "a")
- window.onkeyrelease(a_release, "a")
- window.onkeypress(s_press, "s")
- window.onkeyrelease(s_release, "s")
- window.onkeypress(d_press, "d")
- window.onkeyrelease(d_release, "d")
- '''
- This way of key input is great, although it is annoy having to make a 'press' and 'release'
- function for every key you want to use. I have a a solution! It's a tiny bit more technical,
- but still isn't hard to understand.
- The alternative method is accessing the window's tkinter 'canvas' object for better key callback event executors.
- ↓ Your turtle window
- ↓
- canvas = window._root._canvas
- canvas.bind("<Key>", key_pressed)
- canvas.bind("<KeyRelease>", key_released)
- This is the tkinter equivalent to window.onkeypress(...), only it doesn't take a key as an argument
- and instead is called every time any key is pressed. Because of this, it gives you a 'tkinter.Event'
- object, that contains all the information about the key event that you'll ever need. The important one
- being 'keycode', which is the ascii value of the key pressed.
- def key_pressed(event):
- global PRESSED
- if event.keycode not in pressed_keys:
- pressed_keys.append(event.keycode)
- return
- def key_released(event):
- global PRESSED
- if event.keycode in pressed_keys:
- pressed_keys.remove(event.keycode)
- return
- If this method is implemented, it would be easy for the programmer to add support for you arrow key newbies.
- '''
- player = turtle.Turtle()
- player.shape("square")
- player.turtlesize(2, 2)
- player.hideturtle()
- accel = 0.5
- decel = 0.5
- x_vel = 0
- y_vel = 0
- max_vel = 4
- # Game logic update
- def update():
- global accel, decel, x_vel, y_vel, ticks_elapsed
- # Some nasty movement that you can ignore!
- if "w" in pressed_keys:
- if y_vel < max_vel:
- y_vel += accel
- elif y_vel > 0:
- y_vel -= decel
- if y_vel < 0:
- y_vel = 0
- if "s" in pressed_keys:
- if y_vel > -max_vel:
- y_vel -= accel
- elif y_vel < 0:
- y_vel += decel
- if y_vel > 0:
- y_vel = 0
- if "d" in pressed_keys:
- if x_vel < max_vel:
- x_vel += accel
- elif x_vel > 0:
- x_vel -= decel
- if x_vel < 0:
- x_vel = 0
- if "a" in pressed_keys:
- if x_vel > -max_vel:
- x_vel -= accel
- elif x_vel < 0:
- x_vel += decel
- if x_vel > 0:
- x_vel = 0
- player.setx(player.xcor() + x_vel)
- player.sety(player.ycor() + y_vel)
- # Cycling colors! :D
- sin = math.sin(math.radians(ticks_elapsed)) / 2 + 0.5
- cos = math.cos(math.radians(ticks_elapsed)) / 2 + 0.5
- player.color((sin, cos, 1 - cos))
- ticks_elapsed += 1
- return
- # Screen redraws
- def draw():
- player.clear()
- player.stamp()
- window.update()
- return
- # Main game loop
- draws_per_second = 0
- updates_per_second = 0
- time_of_last_update = time.time()
- time_of_last_second = time.time()
- time_between_updates = 1 / 60
- while running:
- current_time = time.time()
- if current_time - time_of_last_update > time_between_updates:
- time_of_last_update += time_between_updates
- update()
- updates_per_second += 1
- draw()
- draws_per_second += 1
- if current_time - time_of_last_second > 1:
- time_of_last_second = current_time
- print("Screen Redraws Per Second:", draws_per_second, "Game Logic Updates Per Second:", updates_per_second)
- updates_per_second = 0
- draws_per_second = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement