Czemu nie działają mi kolizje w grze?

0
import pygame
from random import randint

pygame.init()
pygame.init()
w = pygame.display.set_mode((600, 800))
Tło = pygame.image.load("tło.png")
Trawa_hitbox = pygame.rect.Rect(0, 600, 600, 100)

class Bullet:
    def __init__(self, x=0, y=0):
        self.x = 500
        self.y = 0
        self.speed = 2
        self.img = pygame.image.load("pocisk.png")
        self.hitbox = pygame.rect.Rect(self.x, self.y, 30, 30)

    def draw(self):
        w.blit(self.img, (self.x, self.y))
        print(self.y)

    def tick(self):
        self.hitbox = pygame.rect.Rect(self.x, self.y, 30, 30)

    def move(self):
        self.y -= self.speed

    def off_screen(self):
        return self.y < -self.img.get_height()

class Player:
    def __init__(self):
        self.x = 0
        self.y = 600
        self.yy = self.y
        self.width = 0
        self.height = 0
        self.img = pygame.image.load("Gracz.png")
        self.bullets = []
        self.hitbox = pygame.rect.Rect(self.x, self.y, 100, 100)

    def draw(self):
        w.blit(self.img, (self.x, self.y))

    def tick(self, keys):
        if keys[pygame.K_d]:
            self.x += 2
        if keys[pygame.K_a]:
            self.x -= 2
        if keys[pygame.K_SPACE]:
            self.shoot()
        self.hitbox = pygame.rect.Rect(self.x, self.y, 100, 100)

    def shoot(self):
        if len(self.bullets) < 10000000:
            self.bullets.append(Bullet(self.x, self.y))

class Asteroid:
    def __init__(self):
        self.x = randint(0, 600)
        self.y = 70
        self.witdh = 70
        self.height = 0
        self.image = pygame.image.load("meteor.png")
        self.hitbox = pygame.rect.Rect(self.x, self.y, self.witdh, self.height)

    def draw(self):
        w.blit(self.image, (self.x, self.y))

    def opadanie(self):
        self.y += 1

    def tick(self):
        self.hitbox = pygame.rect.Rect(self.x, self.y, 80, 80)

    def losowanie(self):
        self.x = randint(0, 600)

    def kolizje(self):
        if self.y == 500:
            self.y = 0
            self.losowanie()

def main():
    run = True
    clocck = 0
    score = 0
    bullet = Bullet()
    asteroids = []
    player = Player()
    asteroid = Asteroid()
    while run:
        clocck += pygame.time.Clock().tick(60) / 1000
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        keys = pygame.key.get_pressed()

        for bullet in player.bullets:
            for asteroid in asteroids:
                if asteroid.hitbox.colliderect(bullet.hitbox):
                    print(1)
                    player.bullets.remove(bullet)
                    asteroids.remove(asteroid)
                    break

        if clocck >= 8:
            asteroids.append(Asteroid())
            clocck = 0
            score = 0
            print(score)

        player.tick(keys)
        for asteroid in asteroids:
            asteroid.draw()

        asteroid.opadanie()

        w.blit(Tło, (0, 0))
        asteroid.draw()
        player.draw()
        for bullet in player.bullets:
            bullet.draw()
            bullet.move()
        player.bullets = [bullet for bullet in player.bullets if not bullet.off_screen()]
        asteroid.kolizje()
        pygame.draw.rect(w, (255, 0, 0), bullet.hitbox)

        pygame.display.update()

    pygame.quit()

if __name__ == "__main__":
    main()
1

Na pierwszy rzut oka if self.y == 500 może powodować problemy. Zazwyczaj używa się > lub <

Polecam używać ChatGPT (lub podobny).

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.