首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python游戏:错误检测子弹和敌人之间的碰撞(空间入侵者)

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

Stack Overflow用户
提问于 2016-07-19 01:06:34
回答 1查看 300关注 0票数 0

我试图在Python中创建一个基本的空间入侵者游戏。我正在苦苦挣扎的部分是探测到玩家发射的子弹击中了敌人的目标。我有一个针对子弹和敌人的Sprite class,它的属性指定了雪碧的边界矩形,我有一个intersect method来检查子弹是否使用colliderect方法击中敌人。然而,intersect method总是返回1(不确定这意味着什么),不管子弹是否真的击中了敌人。

代码语言:javascript
复制
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!
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-19 01:33:47

你必须为每个敌人设置你的精灵属性,只有这样你才能调用函数对撞机。

我的意思是:

代码语言:javascript
复制
enemy.rect.top = some y-value
enemy.rect.left = some x-value
enemy.rect.bottom = some-height
enemy.rect.right = some-width
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38448085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档