So, I am trying to detect collision between my virus and rectangle, I have classes set up for each of them and I have the code for the collision, but the code ends up not working and I'm not sure what went wrong. I have turned the image into a rectangle and used colliderect to detect collision between the virus and the base, but nothing happens. Any help would be appreciated on why this is!
import pygame, random, time
pygame.init()
#Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
#Screen Stuff
size = (1080, 640)
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Random Platformer')
icon = pygame.image.load('Icon.jpg')
pygame.display.set_icon(icon)
pygame.display.update()
#Misc Stuff
clock = pygame.time.Clock()
running = True
#Objects
class Player:
def __init__(self, x, y, image):
self.x = x
self.y = y
self.player = pygame.image.load(image)
self.rect = self.player.get_rect()
def load(self):
screen.blit(self.player, (self.x, self.y))
def move_right(self):
self.x += 7.5
def move_left(self):
self.x -= 7.5
def move_up(self):
self.y -= 7.5
def move_down(self):
self.y += 7.5
class Block:
def __init__(self, x, y, width, length, edge_thickness):
self.x = x
self.y = y
self.width = width
self.length = length
self.edge_thickness = edge_thickness
def load(self):
self.rect = pygame.draw.rect(screen, BLACK, [self.x, self.y,
self.width, self.length], self.edge_thickness)
#Players
virus = Player(100, 539, 'Virus.jpg')
#Blocks
level_base = Block(0, 561, 1080, 80, 0)
#Game Loop
while running:
#Level Generation
screen.fill(WHITE)
level_base.load()
virus.load()
if level_base.rect.colliderect(virus.rect):
print ('Hi')
#Controls
key_press = pygame.key.get_pressed()
if key_press[pygame.K_d]:
virus.move_right()
if key_press[pygame.K_a]:
virus.move_left()
if key_press[pygame.K_w]:
virus.move_up()
if key_press[pygame.K_s]:
virus.move_down()
#Game Code
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
break
pygame.display.flip()
clock.tick(60)