I am confused as to how I would replace the sprite with an image I have uploaded. Is there a way to simply replace it with another image (In this case racecar.png)? Can someone also help me be able to move the sprite continuously when I hold the key down instead of just moving once and stopping. Any help would be greatly appreciated. The sprite is not a rectangle but rather a class that I can change the values of
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').convert_alpha()
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,pos):
Sprite.__init__(self, x, y, width, height)
self.color=GREEN
self.direction = pygame.math.Vector2(0,0)
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, 20, 40)
player2 = Player(600, 40, 20, 40)
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
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)