0

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

Tom Baker
  • 11
  • 4
  • 1
    `pygame.transform.rotate` returns a new surface, it does not rotate the surface in-place. You need to change your calls to this function to reassign the blue car variable `bluecar = pygame.transform.rotate(bluecar, 90)` – Iain Shelvington Jul 27 '19 at 09:34
  • Hi Iain, it is coming up with this error now - 'Traceback (most recent call last): File "D:\2018-2019\6.Coding\Python\racing car.py", line 69, in rotate() File "D:\2018-2019\6.Coding\Python\racing car.py", line 28, in rotate bluecar = pygame.transform.rotate(bluecar, 270) UnboundLocalError: local variable 'bluecar' referenced before assignment' Thats when i change the code to 'def rotate(): global direction if direction == 'right': bluecar = pygame.transform.rotate(bluecar, 270) if direction == 'left':' and repeat. – Tom Baker Jul 27 '19 at 09:48
  • you have to use `global bluecar` but better assign new image to new variable and always use original bluecar in rotation. – furas Jul 27 '19 at 09:50
  • Thanks, that worked furas, but the car is very glitchy - it swaps rotations very quickly with no key presses - any way to stop this – Tom Baker Jul 27 '19 at 10:05

1 Answers1

1

pygame.transform.rotate returns a new rotated pygame.Surface.
Don't rotate the original image, return an new and rotated image in the function rotate:

def rotate():

    rot_image = bluecar
    if direction == 'right':
        rot_image = pygame.transform.rotate(bluecar, 270)
    if direction == 'left':
        rot_image = pygame.transform.rotate(bluecar, 90)
    if direction == 'up':
        rot_image = pygame.transform.rotate(bluecar, 0)
    if direction == 'down':
        rot_image = pygame.transform.rotate(bluecar, 180)

    return rot_image

See alos How do I rotate an image around its center using Pygame?

Rotate the image in the function car and get the pygame.Rect of the image by .get_rect() and set the center to (x, y):

def car(x,y):
    rot_image = rotate()
    rect = rot_image.get_rect(center = (x,y))
    gameDisplay.blit(rot_image, rect)

The answer to Image rotation while moving is related a d shows how to use a pygame.sprite.Sprite for a moving car.

Use the class Car from the link and create a car object and add the car to a pygame.sprite.Group before the wain loop. .update() and .draw() the group in the loop:

car = Car(bluecar, x, y, 5, 0)
group = pygame.sprite.Group(car) 

clock = pygame.time.Clock()
while done == False:
    clock.tick(60)

    # [...]

    gameDisplay.blit(background_image, [0,0])

    group.update()
    group.draw(gameDisplay)

    pygame.display.flip()

Rabbid76
  • 202,892
  • 27
  • 131
  • 174