pygame中的面具碰撞检测问题

问题描述

我是pygame rn的小行星风格游戏,但是在尝试使面罩碰撞正常工作时遇到了一个问题。为了检查它们是否发生碰撞,每次检测到碰撞时,我都会使用一个函数来打印“碰撞”。问题是,当我运行程序时,它只是开始不停地打印。因此,我认为以某种方式使背景成为面具或其他东西?邓诺(Dunno)真的,我以前没有碰过碰撞。我只希望游戏在飞船与小行星碰撞时结束。

反正这是我的代码


import pygame
from math import sin,cos,pi

from random import randint

scr_width = 800
scr_height = 600
window = pygame.display.set_mode((scr_width,scr_height))
pygame.display.set_caption("Asteroids")
clock = pygame.time.Clock()
space_img = pygame.image.load("sprites/space.jpg")
red = (255,0)

class Ship:
    def __init__(self,x,y):

        self.x = x
        self.y = y
        self.width = 0
        self.vel = 0
        self.vel_max = 12
        self.angle = 0
        self.hitBox = (self.x,self.y,10,10)
        self.ship_img = pygame.image.load("sprites/ship_off.png")
        self.ship_img_copy = pygame.transform.rotate(self.ship_img,self.angle)
# collision stuff
        self.rect = self.ship_img_copy.get_rect()

    def draw(self):
        self.ship_img = pygame.image.load("sprites/ship_off.png")
        self.ship_img_copy = pygame.transform.rotate(self.ship_img,self.angle)

        window.blit(self.ship_img_copy,(self.x - (self.ship_img_copy.get_width()) / 2,self.y - (self.ship_img_copy.get_height()) / 2))

        keys = pygame.key.get_pressed()

        if keys[pygame.K_w]:
            self.ship_img = pygame.image.load("sprites/ship_on.png")
            self.ship_img_copy = pygame.transform.rotate(self.ship_img,self.angle)
            window.blit(self.ship_img_copy,self.y - (self.ship_img_copy.get_height()) / 2))
# collision stuff
        self.mask = pygame.mask.from_surface(self.ship_img_copy)

    def move(self):
        keys = pygame.key.get_pressed()
        # todo acceleration and thrust mechanics
        if keys[pygame.K_w]:
            self.vel = min(self.vel + 1,self.vel_max)
        elif self.vel > 0:
            self.vel = self.vel - 0.4
        if keys[pygame.K_a]:
            self.angle += 7

        if keys[pygame.K_d]:
            self.angle -= 7

        self.x += self.vel * cos(self.angle * (pi / 180) + (90 * pi / 180))
        self.y -= self.vel * sin(self.angle * (pi / 180) + 90 * (pi / 180))
        if self.y < 0:
            self.y = (self.y - self.vel) % 600

        elif self.y > 600:
            self.y = (self.y + self.vel) % 600

        elif self.x < 0:
            self.x = (self.x - self.vel) % 800

        elif self.x > 800:
            self.x = (self.x + self.vel) % 800


class Asteroid:

    def __init__(self):

        self.ang_change = randint(1,5)
        self.ang = randint(0,90) * (pi / 180)
        y_values = [1,599]
        self.sx = randint(0,800)
        self.sy = y_values[randint(0,1)]
        
        if self.sy == y_values[0]:
            self.neg = -1
        else:
            self.neg = 1
        self.speed = randint(5,10)
        self.ang += self.ang_change
        self.asteroid_angle = randint(0,80)
        self.asteroid_img = pygame.image.load("sprites/asteroid.png")
        self.asteroid_copy = pygame.transform.rotate(self.asteroid_img,self.ang)


    def generate(self):
        self.ang += self.ang_change
        self.asteroid_img = pygame.image.load("sprites/asteroid.png")
        self.asteroid_copy = pygame.transform.rotate(self.asteroid_img,self.ang)

        window.blit(self.asteroid_copy,(self.sx - (self.asteroid_copy.get_width()) / 2,self.sy - (self.asteroid_copy.get_height()) / 2))
# collision stuff
        self.mask = pygame.mask.from_surface(self.asteroid_copy)
        self.rect = self.asteroid_copy.get_rect()


class Projectiles:

    def __init__(self,y,angle):
        self.x = x
        self.y = y
        self.angle = angle
        self.vel = 20

    def draw(self):
        pygame.draw.rect(window,(255,0),(self.x,5,5))


def redraw():
    window.blit(space_img,(0,0))
    ship.draw()
    for asteroid in asteroids:
        asteroid.generate()
    for bullet in bullets:
        bullet.draw()

    pygame.display.update()

# collision stuff
def collisions():
    collision = pygame.sprite.spritecollide(ship,asteroids,False)
    for i in collision:
        print("hit")



run = True
ship = Ship(400,300)
next_fire = pygame.time.get_ticks() + 400
bullets = []
asteroids = []

while run:
    clock.tick(60)
    keys = pygame.key.get_pressed()
    pygame.time.delay(35)
    # collision stuff
    collisions()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    if keys[pygame.K_SPACE]:
        if len(bullets) < 11 and pygame.time.get_ticks() >= next_fire:
            bullets.append(
                Projectiles(round(ship.x + ship.width - 6.5 // 2),round(ship.y + ship.width - 6.5 // 2),ship.angle))
            next_fire = pygame.time.get_ticks() + 400

    for bullet in bullets:
        if 800 > bullet.x > 0 and 600 > bullet.y > 0:
            bullet.x += bullet.vel * cos(bullet.angle * (pi / 180) + 90 * (pi / 180))
            bullet.y -= bullet.vel * sin(bullet.angle * (pi / 180) + 90 * (pi / 180))
        else:
            bullets.pop(bullets.index(bullet))
    
    if len(asteroids) < 5:
        asteroids.append(Asteroid())

    for asteroid in asteroids:
        if 800 > asteroid.sx > 0 and 600 > asteroid.sy > 0:
            asteroid.sx += asteroid.speed * cos(asteroid.asteroid_angle * (pi / 180) + 90 * (pi / 180))
            asteroid.sy -= asteroid.speed * sin(asteroid.asteroid_angle * (pi / 180) + 90 * (pi / 180)) * asteroid.neg
            if asteroid.sx < 0:
                asteroid.sx = (asteroid.sx - asteroid.speed) % 800

            elif asteroid.sx > 800:
                asteroid.sx = (asteroid.sx + asteroid.speed) % 800

        else:
            asteroids.pop(asteroids.index(asteroid))

    window.fill((0,0))
    ship.move()
    redraw()

pygame.quit()

解决方法

碰撞检测使用self.rect作为命中框。 self.rect应该与您在精灵上的位置匹配。您可以通过一些更改来修复代码:

在Ship类(绘制)中:

# collision stuff
        self.mask = pygame.mask.from_surface(self.ship_img_copy)
        self.rect =  pygame.Rect(self.x - (self.ship_img_copy.get_width()) / 2,self.y - (self.ship_img_copy.get_height()) / 2,self.ship_img_copy.get_width(),self.ship_img_copy.get_height())

在小行星类中(生成):

# collision stuff
        self.mask = pygame.mask.from_surface(self.asteroid_copy)
        #self.rect = self.asteroid_copy.get_rect()
        self.rect =  pygame.Rect(self.sx - (self.asteroid_copy.get_width()) / 2,self.sy - (self.asteroid_copy.get_height()) / 2,self.asteroid_copy.get_width(),self.asteroid_copy.get_height())