Reputation: 41
I am making a game, and previously had made it using images and bliting them but I am now remaking it trying to uses object and sprites. I am unsure how I would make my image and rect rotate and I am also unsure how to create a rect that is behind or none visible around my object as I plan to have two cars that can collide. I previously used sin and cos to calculate the direction and the image would turn when I clicked the left and right arrow but I don't know how to apply this to an object class. Any help would be appreciated thanks.
This is my code
import pygame, random
#Let's import the Car Class
from Car import Car
pygame.init()
speed = 1
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)
if keys[pygame.K_RIGHT]:
playerCar.moveRight(5)
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(60)
pygame.quit()
###second file###
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()
#Initialise attributes of the car.
self.width=width
self.height=height
#self.speed = speed
# 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
Upvotes: 3
Views: 1133
Reputation: 210909
I am also unsure how to create a rect that is behind or none visible around my object.
If you don't draw an object it is not visible. Don't confuse a pygame.Rect
objects and pygame.draw.rect
. However, a pygame.Rect
stores a position and a size. It is always axis aligned and cannot represent a rotated rectangle.
I'm suspect you need the rectangular outline of the objects for collision detection. See How do I detect collision in pygame?, Collision between masks in PyGame and Pygame mask collision.
Use pygame.transform.rotate
to rotate an image. e.g:
def blitRotateCenter(surf, image, center, angle):
rotated_image = pygame.transform.rotate(image, angle)
new_rect = rotated_image.get_rect(center = image.get_rect(center = center).center)
surf.blit(rotated_image, new_rect)
See also How do I rotate an image around its center using PyGame?
If you want to rotate a rectangle you have to fill a pygame.Surface
object with transparency information with a uniform color and rotate the surface. Set the SRCALPHA
to crate a Surface with per pixel alpha format:
rect_surf = pygame.Surface((width, height), pygame.SRCALPHA)
rect_surf.fill(color)
blitRotateCenter(screen, rect_surf, (x, y), angle)
See also Getting rotated rect of rotated image in Pygame
Upvotes: 2