Pygame碰撞无法正常工作

时间:2017-01-03 19:23:53

标签: python python-2.7 pygame

对于我的计算项目,我需要制作某种应用或游戏。我决定让太空入侵者。我已经设法使基本工作,精灵会产生我可以移动玩家,敌人从屏幕顶部掉落,背景和音乐正常工作,但碰撞不能正常工作。当角落接触或两个角落之间的中点接触然后它工作正常,程序关闭。但是如果角点/中点不接触,那么精灵就会直接穿过彼此。任何帮助将非常感激。这是代码:

import pygame
import random
import sys
# --- constants --- (UPPER_CASE names)

WHITE  = (255, 255, 255)
BLACK  = (  0,   0,   0)
RED    = (255,   0,   0)
GREEN  = (  0, 255,   0)
BLUE   = (  0,   0, 255)
ORANGE = (255, 255,   0)
YELLOW = (  0, 255, 255)

DISPLAY_WIDTH = 720
DISPLAY_HEIGHT = 720

FPS = 60

# --- classes --- (CamelCase names)

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images\\user1.gif").convert()
        self.image = pygame.transform.scale(self.image, (50, 50))
        self.rect = self.image.get_rect()
        self.speed_x = 0

    def update(self):
        self.speed_x = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.speed_x = -7
        if keystate[pygame.K_RIGHT]:
            self.speed_x = 7
        self.rect.x += self.speed_x
        if self.rect.right > DISPLAY_WIDTH:
            self.rect.right = DISPLAY_WIDTH
        if self.rect.left < 0:
            self.rect.left = 0


class Mob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images\\enemy1.gif").convert()
        self.image = pygame.transform.scale(self.image, (50, 50))
        self.rect = self.image.get_rect()
        self.speed_x = 0
        self.rect.x = random.randrange(0, DISPLAY_WIDTH - self.rect.width)
        self.rect.y = random.randrange(-100, -40)
        self.speedy = random.randrange(3, 11)

    def update(self):
            self.rect.y +=self.speedy
            if self.rect.top > DISPLAY_HEIGHT + 10:
                self.rect.x = random.randrange(0, DISPLAY_WIDTH - self.rect.width)
                self.rect.y = random.randrange(-100, -40)
                self.speedy = random.randrange(3, 11)

# --- functions --- (lower_case names)

# empty

# --- main --- (lower_case names)

# - init -

pygame.init()
pygame.mixer.init()

display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
display_rect = display.get_rect()

# - objects -

all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()

player = Player()
player.rect.center = ((DISPLAY_WIDTH / 2), DISPLAY_HEIGHT/1.2)

all_sprites.add(player)
for z in range(8):
    mob = Mob()
    mobs.add(mob)
    all_sprites.add(mob)
background = pygame.image.load("images\\background.jpg")
background = pygame.transform.scale(background, (DISPLAY_WIDTH, DISPLAY_HEIGHT))

# - other -

pygame.mixer.music.load("audio\\soundtrack.mp3")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.4)

# - mainloop -

crashed = False
clock = pygame.time.Clock()

while not crashed:

    # - events -
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

        print(event)


# - checks for a hit -
    col = pygame.sprite.collide_rect(mob, player)
    if col:
            sys.exit()
# - updates (without draws) -

    all_sprites.update()

# - draws (without updates) -

    display.blit(background, (0, 0))

    all_sprites.draw(display)

    pygame.display.update()

# - FPS -

    clock.tick(FPS)

# - end -
pygame.quit()

任何帮助都会很棒,谢谢。

1 个答案:

答案 0 :(得分:1)

函数pygame.sprite.collide_rect不适合直接使用。它旨在传递到“主要”碰撞函数

  1. pygame.sprite.spritecollide()
  2. pygame.sprite.groupcollide()
  3. pygame.sprite.spritecollideany()
  4. 在您的情况下,您需要pygame.sprite.spritecollideany

    col = pygame.sprite.spritecollideany(player, mobs)
    if col:
        sys.exit()
    

    此功能将返回与mobs碰撞的player中的一个。