1

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) 

1 Answers1

0

If the collision detection doesn't work, it helps to print the rects of the involved objects each frame. You'll see that the rect of the virus is positioned at the topleft coords of the screen (0, 0) and never moves when the virus moves.

In the __init__ method you have to set the coords of the self.rect, for example by passing x and y to get_rect as the topleft or center argument:

self.rect = self.player.get_rect(topleft=(x, y))

You could also do this:

self.rect.x = x
self.rect.y = y

And when the player moves, you always have to update the rect as well. You could also do this in an update method.

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(topleft=(x, y))
    def load(self):
        screen.blit(self.player, self.rect)
    def move_right(self):
        self.x += 7.5
        self.rect.x = self.x
    def move_left(self):
        self.x -= 7.5
        self.rect.x = self.x
    def move_up(self):
        self.y -= 7.5
        self.rect.y = self.y
    def move_down(self):
        self.y += 7.5
        self.rect.y = self.y
skrx
  • 19,980
  • 5
  • 34
  • 48
  • Side note: I recommend loading images as described in [this answer](https://stackoverflow.com/a/47883299/6220679). – skrx Dec 19 '17 at 19:59