Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame:错误检测子弹和敌人(太空入侵者)之间的碰撞_Python_Pygame - Fatal编程技术网

Python Pygame:错误检测子弹和敌人(太空入侵者)之间的碰撞

Python Pygame:错误检测子弹和敌人(太空入侵者)之间的碰撞,python,pygame,Python,Pygame,我正在尝试用Python创建一个基本的空间入侵者游戏。我正在努力的部分是检测玩家发射的子弹何时击中敌人目标。我有一个Sprite类用于子弹和敌人,该类属性指定了精灵的边界矩形,我有一个intersect方法,该方法使用collide rect方法检查子弹是否击中敌人。然而,不管子弹是否击中敌人,intersect方法总是返回1(不确定这意味着什么) from pygame import * import random import sys class Sprite: '''

我正在尝试用Python创建一个基本的空间入侵者游戏。我正在努力的部分是检测玩家发射的子弹何时击中敌人目标。我有一个
Sprite类
用于子弹和敌人,该类属性指定了精灵的边界矩形,我有一个
intersect方法
,该方法使用
collide rect
方法检查子弹是否击中敌人。然而,不管子弹是否击中敌人,
intersect方法总是返回1(不确定这意味着什么)

from pygame import *
import random
import sys

class Sprite:
    '''
        A Sprite class that can initialize a sprite based on an image, and display it.

        Parameters:
            filename -> the path of the image to be used
            xpos -> the x position of the sprite
            ypos -> the y position of the sprite
    '''
    def __init__(self, filename, xpos, ypos):
        self.xpos = xpos
        self.ypos = ypos
        self.sprite_image, self.rect = load_image(filename, (0, 0, 0))
        self.sprite_image.set_colorkey((0, 0, 0)) 

    def display(self, screen):
        '''
            Displays the sprite onto the screen.

            Parameters:
                screen -> the PyGame display to draw onto.
        '''
        screen.blit(self.sprite_image, (self.xpos, self.ypos))

    def intersect(self, sprite_2):
        '''
            Returns whether a sprite intersects with another sprite.

            Parameters:
                sprite_2 -> the sprite to compare intersection with

            Returns:
                Whether the sprite intersects with sprite_2
        '''
        return self.rect.colliderect(sprite_2.rect)

def load_image(path, colorkey):
    '''
        Returns an image and its bounding rectangle based on a filename.

        Parameters:
            path -> the path of the picture
            colorkey -> the color defined to be transparent

        Returns:
            the loaded image
            the bounding rectangle of the image
    '''
    try:
        sprite_image = image.load(path)
    except error, message:
        print("Cannot load image: {0}".format(path))

    sprite_image = sprite_image.convert()

    if colorkey is not None:
        if colorkey is -1:
            colorkey = sprite_image.get_at((0, 0))
        sprite_image.set_colorkey(colorkey, RLEACCEL)

    return sprite_image, sprite_image.get_rect()

def main():
    '''
        The main function runs the Space Invader game, implementing all the logic, calculation,
        and user interaction.
    '''
    init()
    screen = display.set_mode((800, 600))
    display.set_caption("Space Invaders")

    fighter = Sprite("spaceship.png", 357, 520)

    enemies = []
    bullets_good = [] # List of all of the bullets fired by the hero / fighter

    for i in range(8): # Add the enemies into the enemies list
        new_enemy = Sprite("enemy.png", 55 * i + 10, 50)
        enemies.append(new_enemy)

    while True:
        screen.fill((0, 0, 0)) # Continiously refresh the background of the screen

        for enemy in enemies: # Draw the enemies onto the screen
            enemy.display(screen)

        for bullet in bullets_good: # Draw the bullets onto the screen
            bullet.ypos -= 10

            for enemy in enemies:
                if bullet.intersect(enemy) == 0:
                    print("MISSILE HIT ENEMY")

            bullet.display(screen)

        for keyevent in event.get(): # Go through key press events
            if keyevent.type == QUIT:
                quit()
                sys.exit()
            if keyevent.type == KEYDOWN:
                if keyevent.key == K_LEFT: 
                    fighter.xpos -= 5
                if keyevent.key == K_RIGHT:
                    fighter.xpos += 5
                if keyevent.key == K_UP:
                    # Create a new bullet and add it to the list of bullets fired by the hero
                    bullet = Sprite("bullet.png", fighter.xpos + 34, fighter.ypos - 20)
                    bullets_good.append(bullet)

        fighter.display(screen)
        display.update()
        pass

main() # Run the game!

你必须为每个敌人设置精灵的rect属性,然后才能调用collide rect函数

我的意思是:

enemy.rect.top = some y-value
enemy.rect.left = some x-value
enemy.rect.bottom = some-height
enemy.rect.right = some-width

你必须为每个敌人设置精灵的rect属性,然后才能调用collide rect函数

我的意思是:

enemy.rect.top = some y-value
enemy.rect.left = some x-value
enemy.rect.bottom = some-height
enemy.rect.right = some-width