0

I'd like to create multiple objects from 1 variable. I'm doing a shooting game :p Here is my code (not all of it :D) :

The first things

    dx = math.cos(math.radians(direction))
    dy = math.sin(math.radians(direction))

    if ball_independant == False:
        ball_x = x
        ball_y = y

This is for the ball (bullet) to go off in a single direction independant of my spaceship.

        # BALL DEPLACEMENT
    if space:
        ball_independant = True
        ball_exist = True

        if ball_exist == True:
            gameDisplay.blit(ball_draw, (ball_x, ball_y))

            if d_ball_limit == False:
                ball_direction = direction

                d_ball_x = math.cos(math.radians(direction))
                d_ball_y = math.sin(math.radians(direction))

                d_ball_limit = True
                ball_direction_set = True

            ball_y -= int(ball_velocity * d_ball_x)
            ball_x -= int(ball_velocity * d_ball_y)

Direction is the direction of my spaceship BEFORE I launch the ball. So after, the ball's direction dosent change, it goes strait on. For the ball, I use a pygame.image.load so I can after rotate it. Which I can't with a draw.rect I think

So I'd like to when Space is pushed, I create some balls that take the direction of the spaceship and NEVER CHANGE DIRECTION after.

I saw on google things like Sprites, but I don't understand it and I don't wanna redo my code again :D So if it's possible to do it without to much changing, it would be awesome !! Thanks !

nicobld
  • 131
  • 2
  • 12
  • I just don't know how to create multiple balls with independant coordinates – nicobld Nov 06 '15 at 19:58
  • You first need to define the ball, as a [class](https://docs.python.org/3/tutorial/classes.html). Then you can simply create them. – dsh Nov 06 '15 at 20:13
  • A little bit more detail please ? I'm new at pygame I dunno how to use classes – nicobld Nov 06 '15 at 20:14
  • It sounds like you are asking what object oriented programming is and how to do it? That would be a bit too broad for a SO question. – cmd Nov 06 '15 at 20:19
  • 1
    The easiest way I can see to do what you want (and I've done this many times in the past), would be to create a class called `Bullet` that inherited from `Sprite`. Every time you fire create a new instance of `Bullet` and stuff it into a `sprite.group`. This also makes update, drawing and collision easy as you can do each against all bullets with one call. – cmd Nov 06 '15 at 20:49

0 Answers0