繁体   English   中英

pygame 中未检测到碰撞

[英]Collisions aren't being detected in pygame

我不知道为什么,但在我的游戏中没有检测到碰撞。 这是一个小行星风格的游戏,我希望子弹摧毁小行星,当飞船被击中时游戏结束,但它们在没有做任何事情的情况下相互穿过。

这是我的代码:

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, 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)

    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.x - (self.ship_img_copy.get_width()) / 2, self.y - (self.ship_img_copy.get_height()) / 2))
# 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())

    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))
        # So that if it leaves one side it comes from the other
        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 object spawns from the top, it moves down instead of moving up and de-spawning immediately
        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 = 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())


class Projectiles:

    def __init__(self, x, y, angle):
        self.x = x
        self.y = y
        self.angle = angle
        self.vel = 20
        self.bullet_body = pygame.image.load("sprites/bullet.png")

    def draw(self):
        self.bullet_body = pygame.image.load("sprites/bullet.png")
# collision stuff
        self.rect = pygame.Rect(self.x - (self.bullet_body.get_width()) / 2,
                                self.y - (self.bullet_body.get_height()) / 2, 5, 5)
        window.blit(self.bullet_body, (self.x - 2, self.y))
        self.mask = pygame.mask.from_surface(self.bullet_body)


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():
    asteroids = pygame.sprite.Group()
    bullets = pygame.sprite.Group()
    if pygame.sprite.spritecollide(ship, asteroids, True, pygame.sprite.collide_mask):
        print("hit")
        pygame.quit()
    pygame.sprite.groupcollide(asteroids, bullets, True, True, pygame.sprite.collide_mask)


# main loop
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))
    # To limit the number of asteroids on screen
    if len(asteroids) < 5:
        asteroids.append(Asteroid())

    for asteroid in asteroids:
        if 805 > asteroid.sx > 0 and 605 > 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) % 805

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

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

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

pygame.quit()

是我不能为碰撞单独制作 function,还是我错过了其他东西?

我无法测试代码,但通常问题是碰撞的 function 使用来自self.rect的值,但您不使用它们,而是使用self.xself.y来保留 position。

你应该在__init__

self.rect = self.image.get_rect()

self.rect.center = (x, y)

然后你可以添加到self.rect.xself.rect.y ,它会自动计算 position

blit(self.image, self.rect)

并与其他 object 发生碰撞

self.rect.colliderect(other.rect)

或在event.MOUSEBUTTONDOWN中使用鼠标

self.rect.collidepoint(event.pos)

测试是否单击了 object(即按钮)。


如果要使用 pygame Group

asteroids = pygame.sprite.Group()
bullets = pygame.sprite.Group()

那么你不应该在collisions()中创建它们,而是在开始而不是列表时创建它们

asteroids = []
bullets = []

并且您应该将对象添加到组而不是附加到列表中。

asteroids.add(Asteroid())

您甚至可以为所有对象运行 function 而无需使用for -loop

asteroids.draw()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM