0

I'm trying to replace the sprites with an image (in this case a race car), but the sprites aren't changing. They're remaining rectangles and aren't changing. The program doesn't give an errors so I don't know why it's not working. Is it the class thats not written right or the information I'm feeding to it? Any help would be greatly appreciated. The name of the image is racecar.png.

import pygame
import sys
import math
import random


pygame.init()
pygame.display.set_caption("__UNITY")
clock = pygame.time.Clock()


WIDTH = 1200
HEIGHT = 800
GRAVITY = 1

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)


IMAGE = pygame.image.load('racecar.png')
font = pygame.font.SysFont("comicsansms",30)
# beach = pygame.image.load("beach.jpeg")

screen = pygame.display.set_mode((WIDTH, HEIGHT))

class Sprite():
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.dx = 0
        self.dy = 0
        self.width = width
        self.height = height
        self.color = WHITE
        self.friction = 0.9
        
    def goto(self, x, y):
        self.x = x
        self.y = y

    def render(self):
        pygame.draw.rect(screen, self.color, pygame.Rect(int(self.x-self.width/2.0), int(self.y-self.height/2.0), self.width, self.height)) 

    def is_aabb_collision(self, other):
        x_collision = (math.fabs(self.x - other.x) * 2) < (self.width + other.width)
        y_collision = (math.fabs(self.y - other.y) * 2) < (self.height + other.height)
        return (x_collision and y_collision)

class Player(Sprite):
    def __init__(self, x, y, width, height,colour,image):
        Sprite.__init__(self, x, y, width, height)
        self.color=colour
        self.direction = pygame.math.Vector2(0,0)
        # super().__init__()
        self.rect = pygame.Rect(0, 0, width, height)
        self.image = pygame.Surface([width, height])
        self.image = pygame.image.load(image)
    
    def move(self):
        self.x += self.dx
        self.y += self.dy
        self.dy += GRAVITY
        
    def jump(self):
        self.dy -= 15
        
    def left(self):
        self.dx -= 6
        if self.dx < -12:
            self.dx = -12
        
    def right(self):
        self.dx += 6
        if self.dx > 12:
            self.dx = 12


          
player = Player(600, 0, 30, 40,RED,'racecar.png')
player2 = Player(600, 40, 20, 40,GREEN,'racecar.png')
blocks = []
blocks.append(Sprite(600, 200, 400, 20))
blocks.append(Sprite(600, 400, 600, 20))
blocks.append(Sprite(600, 600, 1000, 20))
blocks.append(Sprite(1000, 500, 100, 200))
blocks.append(Sprite(200, 500, 100, 200))


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
            sys.exit()
    


#controls
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.left()
            elif event.key == pygame.K_RIGHT:
                player.right()
            if event.key == pygame.K_UP:
                for block in blocks:
                    if player.is_aabb_collision(block):
                        player.jump()
                        break


          
      
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                player2.left()
            elif event.key == pygame.K_d:
                player2.right()
            elif event.key == pygame.K_w:
                for block in blocks:
                    if player2.is_aabb_collision(block):
                        player2.jump()
                        break

    player.move()
    player2.move()

  
    for block in blocks:
        if player.is_aabb_collision(block):
        
            if player.x < block.x - block.width/2.0 and player.dx > 0:
                player.dx = 0
                player.x = block.x - block.width/2.0 - player.width/2.0
        
            elif player.x > block.x + block.width/2.0 and player.dx < 0:
                player.dx = 0
                player.x = block.x + block.width/2.0 + player.width/2.0
        
            elif player.y < block.y:
                player.dy = 0
                player.y = block.y - block.height/2.0 - player.height/2.0 + 1
                player.dx *= block.friction
          
            elif player.y > block.y:
                player.dy = 0
                player.y = block.y + block.height/2.0 + player.height/2.0 
              
        elif player.is_aabb_collision(player2):
          
            if player.x < player2.x - player2.width/2.0 and player.dx > 0:
              player.dx = 0
              player.x = player2.x - player2.width/2.0 - player.width/2.0

            elif player.x > player2.x + player2.width/2.0 and player.dx < 0:
                player.dx = 0
                player.x = player2.x + player2.width/2.0 + player.width/2.0

            elif player.y < player2.y:
                player.dy = 0
                player.y = player2.y - player2.height/2.0 - player.height/2.0 + 1
                player.dx *= player2.friction
          
            elif player.y > player2.y:
                player.dy = 0
                player.y = player2.y + player2.height/2.0 + player.height/2.0 
              
        elif player2.is_aabb_collision(player):
          
            if player2.x < player.x - player.width/2.0 and player2.dx > 0:
              player2.dx = 0
              player2.x = player.x - player.width/2.0 - player2.width/2.0

            elif player2.x > player.x + player.width/2.0 and player2.dx < 0:
                player2.dx = 0
                player2.x = player.x + player.width/2.0 + player2.width/2.0

            elif player2.y < player.y:
                player2.dy = 0
                player2.y = player.y - player.height/2.0 - player2.height/2.0 + 1
                player2.dx *= player.friction
          
            elif player2.y > player.y:
                player2.dy = 0
                player2.y = player.y + player.height/2.0 + player2.height/2.0 

        if player2.is_aabb_collision(block):
        
            if player2.x < block.x - block.width/2.0 and player2.dx > 0:
                player2.dx = 0
                player2.x = block.x - block.width/2.0 - player2.width/2.0
        
            elif player2.x > block.x + block.width/2.0 and player2.dx < 0:
                player2.dx = 0
                player2.x = block.x + block.width/2.0 + player2.width/2.0
        
            elif player2.y < block.y:
                player2.dy = 0
                player2.y = block.y - block.height/2.0 - player2.height/2.0 + 1
                player2.dx *= block.friction
          
            elif player2.y > block.y:
                player2.dy = 0
                player2.y = block.y + block.height/2.0 + player2.height/2.0 

  
    if player.y > 800:
        player.goto(600, 0)
        player.dx = 0
        player.dy = 0
    if player2.y > 800:
        player2.goto(600, 0)
        player2.dx = 0
        player2.dy = 0
        
 
    screen.fill(BLACK)
    

    player.render()
    player2.render()
    for block in blocks:
        block.render()
     
  
    pygame.display.flip()
    
   
    clock.tick(30)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Rishad
  • 53
  • 1
  • 1
  • 5
  • You still draw rectangles in `render`. You must `blit` the images instead. You can't expect to see anything other than rectangles if you're just drawing rectangles. See [Replace a rectangle with an Image in Pygame](https://stackoverflow.com/questions/69634473/replace-a-rectangle-with-an-image-in-pygame/69634652#69634652) and [What is a good way to draw images using pygame?](https://stackoverflow.com/questions/8873219/what-is-a-good-way-to-draw-images-using-pygame/64630591#64630591) . – Rabbid76 May 14 '22 at 21:29
  • If you want to use [**`pygame.sprite.Sprite`**](https://www.pygame.org/docs/ref/sprite.html) objects, see [Making and using pygame sprites](https://stackoverflow.com/questions/68566126/making-and-using-pygame-sprites-for-donkey-kong-style-game/68600795#68600795) – Rabbid76 May 14 '22 at 21:31

0 Answers0