I want to get my bullet shooting in the direction that my ship is facing. I managed to get a bullet in the game using this very useful code: How to create bullets in pygame?
Let me provide some code for reference. Below is the set of codes used to rotate my ship as-well as get it to move in said direction. Also, 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
Below is the class the spaceship code is in.
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))
Now as part of the "How to create bullets..." link I previously mentioned I was thinking of changing
for b in range(len(bullets)):
bullets[b][1] -= 8
to bullets[spaceship.startX][spaceship.startY] -= 8 As it was my belief that that line of code represented bullets[x][y]. However I was presented with TypeError: list indices must be integers, not float.
I'm guessing my assumption is wrong. Any ideas you can suggest to get my bullet moving according to how I'd like it to be?