通电不与玩家碰撞(Pygame)

时间:2014-09-08 20:56:37

标签: python pygame collision-detection collision

在与玩家角色发生碰撞时,我一直无法让我的通电消失。我已经能够通过子弹杀死敌人而获得通电,但我似乎无法收集它们。到目前为止,我已经列出了我的项目。我试过搞几个函数(pygame.sprite.spritecollide()和pygame.rect.colliderect()),但似乎都没有用。我似乎无法弄清楚问题是在我的播放器或Power类中,还是在"同时"循环在底部。

import pygame, sys, random
from pygame.locals import *

# Colors
black = (0,0,0)
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)

pygame.init()
screen_width = 600
screen_height = 600
screen = pygame.display.set_mode([screen_width, screen_height])

# Bullet
bullet_image = pygame.image.load('pbbullet.png')

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('pbtouhou.png')
        self.rect = self.image.get_rect()
        self.x = 300
        self.y = 300
        self.bombamount = 3
        self.bombtimer = 0
        self.bombmax = 10
        self.power = 1.0
        self.lives = 3

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 5
        if key[pygame.K_LSHIFT]:
            dist = 2
        else:
            dist = 5

        if key[pygame.K_DOWN]:
            self.y += dist
        elif key[pygame.K_UP]:
            self.y -= dist
        if key[pygame.K_RIGHT]:
            self.x += dist
        elif key[pygame.K_LEFT]:
            self.x -= dist

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

class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = bullet_image

        self.rect = self.image.get_rect()

    def update(self):
        # Move the bullet
        self.rect.y -= 5

class Enemy(pygame.sprite.Sprite):
    def __init__(self, color):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([20, 15])
        self.image.fill(color)

        self.rect = self.image.get_rect()

# Powerups
class Power(pygame.sprite.Sprite):
    def __init__(self, x, y, player):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([5, 5])
        self.image.fill(red)
        self.player = player

        self.rect = self.image.get_rect()

        self.rect.x = x
        self.rect.y = y

    def update(self):
        self.rect.y += 2

#Set up
player = Player() # create an instance
clock = pygame.time.Clock()

#Lists
all_sprites_list = pygame.sprite.Group()
enemy_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()
power_list = pygame.sprite.Group()

#Test Enemy
bad_guy = Enemy(blue)

bad_guy.rect.x = 100
bad_guy.rect.y = 100

all_sprites_list.add(bad_guy)
enemy_list.add(bad_guy)
###

running = True
while running: #main game loop
    key = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            running = False

    if key[pygame.K_z]:
        bullet = Bullet()

        bullet.rect.x = player.x + 35
        bullet.rect.y = player.y

        all_sprites_list.add(bullet)
        bullet_list.add(bullet)

    all_sprites_list.update()

    # Bullets hit enemies
    for bullet in bullet_list:
        # See if it hit a enemy
        enemy_hit_list = pygame.sprite.spritecollide(bullet, enemy_list, True)

        for enemy in enemy_hit_list:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)

            power = Power(bullet.rect.left, bullet.rect.top, player)
            power_list.add(power)
            all_sprites_list.add(power)

        if bullet.rect.y < -10:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)

    # Player gathers bonuses
    power_hit_list = pygame.sprite.spritecollide(player, power_list, True)

    for power in power_hit_list:
        power_list.remove(power)
        all_sprites_list.remove(power)

    for power in power_list:
        if power.rect.y > (10 + screen_height):
            power_list.remove(power)
            all_sprites_list.remove(power)

    player.handle_keys()

    screen.fill(white)
    all_sprites_list.draw(screen)
    player.draw(screen)
    pygame.display.flip()

    clock.tick(60)

1 个答案:

答案 0 :(得分:0)

您没有在self.rect.xself.rect.y的播放器上设置self.xself.y,因此碰撞检测使用(0, 0)作为其位置

pygame docs

  

spritecollide(sprite,group,dokill,collided = None) - &gt; Sprite_list

     

返回一个列表,其中包含与另一个Sprite相交的Group中的所有Sprite。通过比较每个Sprite的Sprite.rect属性来确定交集。