collision detection in pygame-python

37 views Asked by At
import pygame
import sys
from time import sleep
class Doodlejump:
    def __init__(self):
        self.go = True
        self.x = 275
        self.y = 800
        self.doodle = Doodle(self.x, self.y, "doodle_resized.png")
        self.gravity = 7.5
        self.jump_counter = 0
        self.speed = 2
        self.jump_timer = 0

    def run(self):
        pygame.init()
        pygame.display.set_caption("Doodlejump")
        clock = pygame.time.Clock()
        screen = pygame.display.set_mode((600, 800))

        while self.go:
            if self.x < 0:
                self.x = 0
            elif self.x >= 600 - self.doodle.width:
                self.x = 600 - self.doodle.width

            if self.y < 0:
                self.y = 0
            elif self.y >= 800 - self.doodle.height:
                self.y = 800 - self.doodle.height

            if self.y >= 800 - self.doodle.height:
                self.jump_counter = 0

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

            pressed = pygame.key.get_pressed()
            if pressed[pygame.K_LEFT]:
                self.x -= self.speed
            if pressed[pygame.K_RIGHT]:
                self.x += self.speed

            screen.fill((255, 255, 255))

            is_jumping = False
            if pressed[pygame.K_SPACE] and not is_jumping and self.jump_counter < 1 and self.jump_timer <= 0:
                is_jumping = True
                self.y -= self.speed
                self.jump_counter += 1
                self.jump_timer = 100


            if self.jump_timer > 0:
                self.jump_timer -= 1

            jump_speed_factor = 250
            if is_jumping:
                self.y -= jump_speed_factor
                jump_speed_factor *= 0.3
            else:
                self.y += self.gravity
                self.jump_timer -= 15

            platform = pygame.draw.rect(screen, (0, 0, 0), (100, 700, 150, 30))
            if platform.colliderect(self.doodle):
                print("Kollision!!!")


            screen.blit(self.doodle.image, (self.x, self.y))

            pygame.display.update()
            clock.tick(60)

if __name__ == "__main__":
    game = Doodlejump()
    game.run()

I programmed a Doodlejump game with Pygame, but unfortunately, my collision detection doesn't work. When I try to land my Doodle on the platform, it just jumps through it. What could be the reason for this? I've already moved the if statement from top to bottom of the code, but that doesn't make a difference. Am I using the .colliderect() method incorrectly? The if block is not entered at all when there is a collision.

1

There are 1 answers

2
Sashika Sandeepa On

Here working code. This prints "Kollide" when collision happens. I sudgest you to refactor your code. Your OOP approch isn't maintainable.

import pygame
import sys
from time import sleep

class Doodle():
    def __init__(self, x, y, image_path):
        self.image = pygame.image.load(image_path)
        self.rect = pygame.Rect((x, y), self.image.get_size())
        self.width = self.rect.width
        self.height = self.rect.height
    
    def update(self, x, y):
        self.rect.x = x
        self.rect.y = y


class Doodlejump:
    def __init__(self):
        self.go = True
        self.x = 175
        self.y = 100
        self.doodle = Doodle(self.x, self.y, "doodle_resized.png")
        self.gravity = 7.5
        self.jump_counter = 0
        self.speed = 2
        self.jump_timer = 0

    def run(self):
        pygame.init()
        pygame.display.set_caption("Doodlejump")
        clock = pygame.time.Clock()
        screen = pygame.display.set_mode((600, 700))

        while self.go:
            if self.x < 0:
                self.x = 0
            elif self.x >= 600 - self.doodle.width:
                self.x = 600 - self.doodle.width

            if self.y < 0:
                self.y = 0
            elif self.y >= 800 - self.doodle.height:
                self.y = 800 - self.doodle.height

            if self.y >= 800 - self.doodle.height:
                self.jump_counter = 0

            self.doodle.update(self.x, self.y)

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

            pressed = pygame.key.get_pressed()
            if pressed[pygame.K_LEFT]:
                self.x -= self.speed
            if pressed[pygame.K_RIGHT]:
                self.x += self.speed

            screen.fill((255, 255, 255))

            is_jumping = False
            if pressed[pygame.K_SPACE] and not is_jumping and self.jump_counter < 1 and self.jump_timer <= 0:
                is_jumping = True
                self.y -= self.speed
                self.jump_counter += 1
                self.jump_timer = 100

            if self.jump_timer > 0:
                self.jump_timer -= 1

            jump_speed_factor = 250
            if is_jumping:
                self.y -= jump_speed_factor
                jump_speed_factor *= 0.3
            else:
                self.y += self.gravity
                self.jump_timer -= 15

            platform = pygame.draw.rect(screen, (0, 0, 0), (100, 500, 150, 30))
            if platform.colliderect(self.doodle.rect):
                print("Kollision!!!")
                self.doodle.update

            screen.blit(self.doodle.image, (self.x, self.y))

            pygame.display.update()
            clock.tick(60)


if __name__ == "__main__":
    game = Doodlejump()
    game.run()