私の船が向いている方向に弾を撃ちたい。私はこの非常に便利なコードを使ってゲームで弾丸を得ることができました: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
と提示されました。
私の推測は間違っていると思います。どのようなアイデアを提案することができます私の弾丸は、私はそれが欲しいと思いますか?
(PythonのスタイルPEP008に探して検討してくださいガイド)。ちょっとしたヒント: '=='が繰り返されるのではなく、[pygame.K_RIGHT、pygame.K_LEFT、...] 'にevent.keyを使うことができます。 – CodenameLambda
@ CodeLambdas私はここで何を探しているのか分かりません。 – Lucky38i
私は単純な物理シミュレーションを行うPointMassクラスを含む答えを書いていましたが、それはあまりにも大きすぎると思いました。そのため、以下に簡単な答えがあります。以前のコメントの内容は、コードを読みやすくするためのヒントです。 – CodenameLambda