2

I am creating a racecar game and I have decided to rewrite it because I realised I was initially writing it in a very inefficient way. I initially used image an just changed their location around the screen but I am now trying to learn how to create an object and I am running into a problem which I had initially but I don't know how to fix within a class.

What happens is when I turn the image it distorts a lot and ends up slowing down because I think pygame can't handle it maybe. In my old code, I created a copy of the original image and rotated the copy of the image which fixed the distortion problem. I now don't know how to do the same within a class.

main.py

import pygame, random
#Let's import the Car Class
from Car import Car
pygame.init()


SCREENWIDTH=800
SCREENHEIGHT=600
 
size = (SCREENWIDTH, SCREENHEIGHT)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Car Racing")
 
#This will be a list that will contain all the sprites we intend to use in our game.
all_sprites_list = pygame.sprite.Group()

playerCar = Car(60, 80, 70)
playerCar.rect.x = 160
playerCar.rect.y = 100

# Add the car to the list of objects
all_sprites_list.add(playerCar)

#Allowing the user to close the window...
carryOn = True
clock=pygame.time.Clock()
 
while carryOn:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                carryOn=False
            elif event.type==pygame.KEYDOWN:
                if event.key==pygame.K_x:
                     playerCar.moveRight(10)
 
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            #playerCar.moveLeft(5)
            playerCar.rot_center(2)
        if keys[pygame.K_RIGHT]:
            #playerCar.moveRight(5)
            playerCar.rot_center(-2)
        if keys[pygame.K_UP]:
            playerCar.moveUp(5)
        if keys[pygame.K_DOWN]:
            playerCar.moveDown(5)


        all_sprites_list.update()
 
        #Drawing on Screen
        screen.fill("white")
 
        #Now let's draw all the sprites in one go. (For now we only have 1 sprite!)
        all_sprites_list.draw(screen)
 
        #Refresh Screen
        pygame.display.flip()
 
        #Number of frames per secong e.g. 60
        clock.tick(25)
 
pygame.quit()

Car.py

import pygame
WHITE = (255, 255, 255)
 
class Car(pygame.sprite.Sprite):
    #This class represents a car. It derives from the "Sprite" class in Pygame.
 
    def __init__(self, width, height, speed):
        # Call the parent class (Sprite) constructor
        super().__init__()
        
        # Instead we could load a proper picture of a car...
        self.image = pygame.image.load("car.png").convert_alpha()
        self.image = pygame.transform.rotate(self.image, 90)

        #Initialise attributes of the car.
        self.width=width
        self.height=height

        rect_surf = pygame.Surface((width, height), pygame.SRCALPHA)
        rect_surf.fill((0, 0, 0, 0))
        self.rect_surf = rect_surf
 
        # Draw the car (a rectangle!)
        pygame.draw.rect(self.image, (0, 0, 0, 0), [0, 0, self.width, self.height])
 
        # Fetch the rectangle object that has the dimensions of the image.
        self.rect = self.image.get_rect()
 
    def moveRight(self, pixels):
        self.rect.x += pixels
 
    def moveLeft(self, pixels):
        self.rect.x -= pixels
 
    def moveUp(self, pixels):
        self.rect.y -= pixels
 
    def moveDown(self, pixels):
        self.rect.y += pixels
 
    def changeSpeed(self, speed):
        self.speed = speed

    def rot_center(self, angle):
        rotated_image = pygame.transform.rotate(self.image, angle)
        new_rect = rotated_image.get_rect(center = self.image.get_rect(center = (self.rect.x + (self.rect.width / 2), self.rect.y + (self.rect.height / 2))).center)

        self.image = rotated_image
        self.rect = new_rect

Any help on how to slvoe this would be much appreciated. I had questions about how to initially turn the image on another question, How do I make an object in pygame rotate.

I also don't know how to apply the rect correctly so that it is the size of the image and doesn't create a transparent spot on the image. Any help would be much appreciated thanks. :):)

  • do the same - keep original image in ie. `self.original_image` and after rotation put it in `self.image`. And it needs to keep angle in `self.angle` so you rotate original image using `self.angle + angle. – furas May 03 '21 at 02:47
  • instead of `self.rect.x + (self.rect.width / 2)` you can get `self.rect.centerx` and the same with `rect.centery`, But I'm wondering - if you sent `center` in `self.image.get_rect(center=...)` and you also get `center` from this image then you should get exactly the same value - so maybe you should do `rotated_image.get_rect(center=self.rect.center)` ? – furas May 03 '21 at 02:51

1 Answers1

1

You should keep original image in separated variable with angle

 self.original_image = pygame.image.load("car.png").convert_alpha()
 self.angle = 90

and use it to generate rotated image at start

 self.image = pygame.transform.rotate(self.original_image, self.angle)

and later you should increase self.angle and again create rotated image using original image

def rot_center(self, angle):
    self.angle += angle

    rotated_image = pygame.transform.rotate(self.original_image, self.angle)
    new_rect = rotated_image.get_rect(center=self.rect.center)

    self.image = rotated_image
    self.rect = new_rect

or even shorter

def rot_center(self, angle):
    self.angle += angle

    self.image = pygame.transform.rotate(self.original_image, self.angle)
    self.rect = self.image.get_rect(center=self.rect.center)

Full working code

I used red rectangle instead image so everyone can run it and test it.

I used pygame.math.Vector2(pixels, 0).rotate(self.angle) to move up/down using current angle - car direction - instead of moving to top/bottom of the screen.

import pygame
import random

# --- constants ---

WHITE = (255, 255, 255)
RED   = (255,   0,   0)

SCREENWIDTH = 800
SCREENHEIGHT = 600

size = (SCREENWIDTH, SCREENHEIGHT)

# --- classes ---

class Car(pygame.sprite.Sprite):
    #This class represents a car. It derives from the "Sprite" class in Pygame.
 
    def __init__(self, width, height, speed):
        # Call the parent class (Sprite) constructor
        super().__init__()

        #Initialise attributes of the car.
        self.width = width
        self.height = height
        
        # Instead we could load a proper picture of a car...
        #self.original_image = pygame.image.load("car.png").convert_alpha()
        self.original_image = pygame.surface.Surface((width, height)).convert_alpha()
        self.original_image.fill(RED)
        
        self.angle = 90
        self.image = pygame.transform.rotate(self.original_image, self.angle)
 
        # Fetch the rectangle object that has the dimensions of the image.
        self.rect = self.image.get_rect()
 
    def moveRight(self, pixels):
        self.rect.x += pixels
 
    def moveLeft(self, pixels):
        self.rect.x -= pixels
 
    def moveUp(self, pixels):
        #self.rect.y += pixels
        x, y = pygame.math.Vector2(pixels, 0).rotate(self.angle)
        self.rect.x += x
        self.rect.y -= y
        
    def moveDown(self, pixels):
        #self.rect.y += pixels
        x, y = pygame.math.Vector2(pixels, 0).rotate(self.angle)
        self.rect.x -= x
        self.rect.y += y
 
    def changeSpeed(self, speed):
        self.speed = speed

    def rot_center(self, angle):
        self.angle += angle
        
        self.image = pygame.transform.rotate(self.original_image, self.angle)
        self.rect = self.image.get_rect(center=self.rect.center)

# --- main ---

pygame.init()
 
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Car Racing")
 
#This will be a list that will contain all the sprites we intend to use in our game.
all_sprites_list = pygame.sprite.Group()

playerCar = Car(60, 80, 70)
playerCar.rect.x = 160
playerCar.rect.y = 100

# Add the car to the list of objects
all_sprites_list.add(playerCar)

#Allowing the user to close the window...
carryOn = True
clock = pygame.time.Clock()
 
while carryOn:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                carryOn=False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_x:
                     playerCar.moveRight(10)
 
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            #playerCar.moveLeft(5)
            playerCar.rot_center(2)
        if keys[pygame.K_RIGHT]:
            #playerCar.moveRight(5)
            playerCar.rot_center(-2)
        if keys[pygame.K_UP]:
            playerCar.moveUp(5)
        if keys[pygame.K_DOWN]:
            playerCar.moveDown(5)

        all_sprites_list.update()
 
        #Drawing on Screen
        screen.fill("white")
 
        #Now let's draw all the sprites in one go. (For now we only have 1 sprite!)
        all_sprites_list.draw(screen)
 
        #Refresh Screen
        pygame.display.flip()
 
        #Number of frames per secong e.g. 60
        clock.tick(25)
 
pygame.quit()
furas
  • 134,197
  • 12
  • 106
  • 148