Last active
October 23, 2019 13:50
-
-
Save nbogie/d8b4f207c4f30f2d622c33c1a58010b7 to your computer and use it in GitHub Desktop.
pygamezero move actor in direction faced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
car = Actor("car_blue") | |
car.x = 200 | |
car.y = 200 | |
car.speed = 1 | |
accel = 0.5 | |
turning_speed = 1 | |
def update(): | |
# handle player inputs | |
if keyboard.right: | |
car.angle -= turning_speed | |
if car.angle < 0: | |
car.angle += 360 | |
if keyboard.left: | |
car.angle += turning_speed | |
if car.angle > 359: | |
car.angle -= 360 | |
if keyboard.down: | |
car.speed -= accel | |
if keyboard.up: | |
car.speed += accel | |
move_actor_forward(car) | |
def move_actor_forward(actor): | |
vx = actor.speed * math.cos(math.radians(actor.angle)) | |
vy = -actor.speed * math.sin(math.radians(actor.angle)) | |
actor.x += vx | |
actor.y += vy | |
def draw(): | |
screen.fill((100, 100, 100)) | |
car.draw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment