My car should move around the screen, which it does, but should also rotate due to direction - but this doesn't happen.
I have tried lot's of different things, but they don't work, and as i am a begginner, it would be great if someone could tell would needs to change - possibly the Global Direction
code.
import pygame
pygame.init()
done = False
size = (900, 570)
x = (50)
y = (320)
x_change = 0
y_change = 0
pygame.display.set_caption('Car Racing!')
gameDisplay = pygame.display.set_mode((size))
background_image = pygame.image.load('backgroundpicture.png').convert()
bluecar = pygame.image.load('car_black_small_1.png').convert_alpha()
bluecar = pygame.transform.scale(bluecar, (20, 50))
direction = 'right'
def rotate():
global direction
if direction == 'right':
pygame.transform.rotate(bluecar, 270)
if direction == 'left':
pygame.transform.rotate(bluecar, 90)
if direction == 'up':
pygame.transform.rotate(bluecar, 0)
if direction == 'down':
pygame.transform.rotate(bluecar, 180)
def car(x,y):
gameDisplay.blit(bluecar, (x,y))
while done == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -0.25
direction = 'left'
elif event.key == pygame.K_RIGHT:
x_change = 0.25
direction = 'right'
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change = -0.25
direction = 'up'
elif event.key == pygame.K_DOWN:
y_change = 0.25
direction = 'down'
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
x += x_change
y += y_change
gameDisplay.blit(background_image, [0,0])
car(x,y)
rotate()
pygame.display.flip()
pygame.quit()
I expect the car to rotate with direction of keys - ie. left key rotates car left