Reputation: 527
How to turn a car around to face the opposite direction on a key press in PyGame
I am making a game where a car will avoid an object on the road, and I want the car to be able to turn and face the opposite direction when I press a particular key (not just moving left, right, up, and down) but to be able to turn and around and face any particular position that I want it to face.
import turtle
import pygame
import os
WHITE = (255, 255, 255)
BACKGROUND_WIDTH, BACKGROUND_HEIGHT = 900, 500
WIN = pygame.display.set_mode((BACKGROUND_WIDTH, BACKGROUND_HEIGHT))
pygame.display.set_caption("Avoid Obstacles")
ROAD_IMAGE = pygame.image.load(
os.path.join("Assets", "road.png"))
CAR_IMAGE = pygame.image.load(
os.path.join("Assets", "carone.png"))
VELOCITY = 3.2
class Cars:
def __init__(self, x, y, car_width, car_height, car_image, turning):
self.x = x
self.y = y
self.car_width = car_width
self.car_height = car_height
self.car_image = car_image
self.turning = turning
# handles the drawing and resizing of images
def draw_window(self, car):
pygame.transform.flip(CAR_IMAGE, True, False)
CAR_ONE = pygame.transform.scale(
self.car_image, (self.car_width, self.car_height))
WIN.fill(WHITE)
WIN.blit(pygame.transform.scale(
ROAD_IMAGE, (BACKGROUND_WIDTH, BACKGROUND_HEIGHT)), (0, 0))
WIN.blit(CAR_ONE, (car.x, car.y))
pygame.display.update()
# handles car movements when keys are pressed
def handles_car_movement(self, keys_pressed, car):
if(keys_pressed[pygame.K_LEFT]): # Turn LEFT KEY
car.x -= self.turning
if(keys_pressed[pygame.K_RIGHT]): # Turn RIGHT KEY
car.x += self.turning
# handes the storing and manipulating of rectangular areas, proccessing and
function calls
def main(self):
clock = pygame.time.Clock()
carWithPosition = pygame.Rect(
self.x, self.y, self.car_width, self.car_height)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
keys_pressed = pygame.key.get_pressed()
self.handles_car_movement(keys_pressed, carWithPosition)
if event.type == pygame.QUIT:
run = False
self.draw_window(carWithPosition)
pygame.quit()
cars = Cars(500, 300, 55, 40, CAR_IMAGE, 5)
if(__name__ == "__main__"):
cars.main()
Upvotes: 1
Views: 116
Reputation: 412
Rather than changing the x position when turning, try giving the car a rotation and moving it's position based off of that using Trig. such as:
def handles_car_movement(self, keys_pressed, car):
if(keys_pressed[pygame.K_LEFT]): # Turn LEFT KEY
car.rotation -= self.turning
if(keys_pressed[pygame.K_RIGHT]): # Turn RIGHT KEY
car.rotation += self.turning
car.x += cos(car.rotation)
And also render the car with the proper rotation in your draw_window
(I believe something like:)
CAR_ONE = pygame.transform.rotate(self.car_image, self.rotation)
Be sure to be consistent between Radians and Degrees
Upvotes: 0
Reputation: 211166
pygame.transform.flip
does not flip the image itselfe, but returns a new and flipped image. e.g.:
pygame.transform.flip(CAR_IMAGE, True, False)
CAR_IMAGE = pygame.transform.flip(self.car_image, True, False)
Create 2 images for each care (e.g.: slef.CAR_LEFT
and self.CAR_RGIHT
) and assign the image for the first direction to the attribute self.CAR_ONE
. Change the image when the button is pressed:
class Cars:
def __init__(self, x, y, car_width, car_height, car_image, turning):
self.x = x
self.y = y
self.car_width = car_width
self.car_height = car_height
self.car_image = car_image
self.CAR_RIGHT = pygame.transform.scale(self.car_image, (self.car_width, self.car_height))
self.CAR_LEFT = pygame.transform.flip(self.CAR_RIGHT, True, False)
self.CAR_ONE = self.CAR_RIGHT
def draw_window(self, car):
WIN.blit(pygame.transform.scale(ROAD_IMAGE, (BACKGROUND_WIDTH, BACKGROUND_HEIGHT)), (0, 0))
WIN.blit(self.CAR_ONE, (car.x, car.y))
pygame.display.update()
def handles_car_movement(self, keys_pressed, car):
if(keys_pressed[pygame.K_LEFT]): # Turn LEFT KEY
car.x -= self.turning
car.CAR_ONE = car.CAR_LEFT
if(keys_pressed[pygame.K_RIGHT]): # Turn RIGHT KEY
car.x += self.turning
car.CAR_ONE = car.CAR_RIGHT
Upvotes: 2