Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 朝我船的方向开枪_Python_Python 3.x - Fatal编程技术网

Python 朝我船的方向开枪

Python 朝我船的方向开枪,python,python-3.x,Python,Python 3.x,我想让我的子弹射向我的飞船所面对的方向。我在游戏中使用了以下非常有用的代码: 让我提供一些代码供参考。下面是一组代码,用于旋转我的船,并使其朝上述方向移动。另外,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]:

我想让我的子弹射向我的飞船所面对的方向。我在游戏中使用了以下非常有用的代码:

让我提供一些代码供参考。下面是一组代码,用于旋转我的船,并使其朝上述方向移动。另外,
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
子弹[spaceship.startX][spaceship.startY]-=8
,因为我相信那行代码代表
子弹[x][y]
。然而,我遇到了
TypeError:列表索引必须是整数,而不是float


我猜我的假设是错误的。你有什么建议可以让我的子弹按照我想要的方式移动吗?

b
是列表中子弹的索引
子弹
,而
0
1
是x和y坐标

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

对于项目符号中的项目符号:#因为“项目符号”是一个列表列表
子弹[0]+=数学坐标(角度)*速度
子弹[1]+=数学正弦(角度)*速度
另一种更面向对象的方法:

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)

请考虑看PEP08(Python风格指南)。还有一个小提示:你可以使用
event.key[pygame.K_RIGHT,pygame.K_LEFT,…]
@CodingLambdas来代替重复的
==
,我不确定我在这里要找什么..我写了一个答案,其中包括一个PointMass类,它做了一个简单的物理模拟,但我觉得太多了。这就是为什么下面只有一个简单的答案。我上一篇评论中的内容只是让你的代码更具可读性的提示。这有点奏效,我为我的代码道歉,我还在学习(第一年@Uni)。它在某种程度上是有效的,但对于某个角度来说,它不能正常工作,当然,旋转我的飞船会导致子弹抖动,有时会改变方向,好吧,我想我会添加另一种方法,添加一个
bullet
类。@Lucky38i如果你仔细想想,这是合乎逻辑的:如果你提供飞船的角度,因此,您也在更改所有项目符号的角度,而不是存储所有项目符号的角度。我应该补充一点,你应该把它们的角度存储在代表子弹的列表中,或者其他地方。我会给你的课堂方法看一看,然后再回来给你,非常感谢你的帮助。好吧,我设法稍微了解一下这里发生了什么,但似乎不知道如何在屏幕上显示它