在我的船指向的方向射击子弹

时间:2016-11-26 14:00:02

标签: python python-3.x

我想让我的子弹朝着我的船所面向的方向射击。我设法使用这个非常有用的代码在游戏中获得了一颗子弹:How to create bullets in pygame?

让我提供一些参考代码。下面是用于旋转我的船的代码集,以及让它沿着所述方向移动。另外,Offset = 0

'''Directional commands for the sprite'''
if pressed[pygame.K_LEFT]: offset_change += 4
if pressed[pygame.K_RIGHT]: offset_change -= 4
if pressed[pygame.K_UP]:
    y_change -= cos(spaceship.angle) * 8
    x_change -= sin(spaceship.angle) * 8    
if pressed[pygame.K_DOWN]:
    y_change += 5
if event.type == pygame.KEYUP:
    if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
        x_change = 0
        y_change = 0
        offset_change = 0

'''changes X/Y based on the value of X/Y_Change'''

spaceship.angle += offset_change        
spaceship.startY += y_change
spaceship.startX += x_change

以下是太空船代码所在的类。

class Spaceship():
'''Class attributed to loading the spaceship'''
    def __init__(self, char, startX, startY, Angle):
        '''Loads in the image and sets variables'''
        self.char = char
        self.startX = startX
        self.startY = startY
        self.angle = Angle
        self.space = load_item(self.char)
        self.drawChar()

    def drawChar(self):
        '''Draws the image on the screen'''
        #Code is from Pygame documentation 
        #Due to limitations of Pygame the following code had to be used with a few of my own manipulations
        rot_image = pygame.transform.rotate(self.space,self.angle)
        rot_rect = self.space.get_rect()
        rot_rect.center = rot_image.get_rect().center
        rot_image = rot_image.subsurface(rot_rect).copy()
        gameDisplay.blit(rot_image,(self.startX,self.startY))

现在作为“如何创建项目符号...”链接的一部分,我之前提到过,我正在考虑改变

for b in range(len(bullets)):
    bullets[b][1] -= 8

bullets[spaceship.startX][spaceship.startY] -= 8因为我认为这行代码代表bullets[x][y]。但是我收到了TypeError: list indices must be integers, not float

我猜我的假设是错的。你可以建议任何想法让我的子弹按照我喜欢的方式移动吗?

1 个答案:

答案 0 :(得分:1)

b是列表bullets中项目符号的索引,01是x和y坐标。

您可以按如下方式更新它们,角度和速度更改为其值:

for bullet in bullets:  # as 'bullets' is a list of lists
    bullet[0] += math.cos(angle) * speed
    bullet[1] += math.sin(angle) * speed

另一种更面向对象的方法:

class Vector(tuple):
    def __new__(cls, *args):
        if len(args) == 1 and hasattr(args[0], "__iter__"):
            return super().__new__(cls, args[0])
        else:
            return super().__new__(cls, args)

    def __add__(self, other):
        return Vector(a + b for a, b in zip(self, other))

    def __sub__(self, other):
        return Vector(a - b for a, b in zip(self, other))

    def __neg__(self):
        return Vector(-i for i in self)

    def __mul__(self, other):
        return Vector(i * other for i in self)

    def __rmul__(self, other):
        return self.__mul__(other)

    def __div__(self, other):
        return Vector(i / other for i in self)

    def __truediv__(self, other):
        return self.__div__(other)


class Bullet(object):
    def __init__(self, position, angle, speed):
        self.position = Vector(position)
        self.speed = Vector(math.cos(angle), math.sin(angle)) * speed

    def tick(self, tdelta=1):
        self.position += self.speed * tdelta

    def draw(self):
        pass  # TODO


# Construct a bullet
bullets.append(Bullet(position, angle, speed)

# To move all bullets
for i in bullets:
    i.tick()  # or i.tick(tdelta)