通电不与球员发生碰撞(pygame的)(Powerups not colliding with pl

2019-10-30 06:39发布

我一直有麻烦我的通电后与玩家角色碰撞消失。 我已经能够得到通电,以在与子弹杀死敌人下降,但我似乎无法收集它们。 我列出我的项目至今。 我试着一对夫妇功能搞乱(pygame.sprite.spritecollide()和pygame.rect.colliderect()),但既不似乎工作。 我似乎无法弄清楚,如果问题出在我的播放器或电源类或底部的“while”循环。

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)

Answer 1:

你是不是设置self.rect.xself.rect.y您的播放器只有self.xself.y ,所以碰撞检测是使用(0, 0)作为它的位置。

在pygame的文档

spritecollide(子画面,组,dokill,相撞=无) - > Sprite_list

返回包含组中与另一雪碧相交组所有精灵列表。 交叉点是通过比较每个精灵的Sprite.rect属性确定。



文章来源: Powerups not colliding with player (Pygame)