gpt4 book ai didi

python - 向我的船指向的方向发射子弹

转载 作者:太空宇宙 更新时间:2023-11-03 15:55:45 24 4
gpt4 key购买 nike

我想让子弹朝着我的船所面向的方向射击。我设法使用这个非常有用的代码在游戏中获得子弹: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

to bullets[spaceship.startX][spaceship.startY] -= 8 因为我相信这行代码代表 bullets[x][y] 。但是,我遇到了 TypeError: listindexsmustbeintegers, not float

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

最佳答案

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)

关于python - 向我的船指向的方向发射子弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40819115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com