1

I'm trying to make a game using pygame module I've already imported my pictures and can move the helicopter but I'm not able to shoot. The commented lines are the things that I've done so far for the shooting part. When pressing the button for shooting, nothing happens. It seems that the code does not run for the second if statement and I don't know the reason.

Thank you for your help!

import pygame

white=(255,255,255)
black=(0,0,0)

pygame.init()
surface = pygame.display.set_mode((1000,1000))
pygame.display.set_caption("AI Game")
clock = pygame.time.Clock()

def helicopter(x , y, image):
    surface.blit(helicopterPicture ,(x,y))

def bulletshape(x, y, image):
    surface.blit(bulletPicture ,(x,y))

bullets=[]



helicopterPicture = pygame.image.load('helicoptter.png')
bulletPicture = pygame.image.load('bullet.png')

x_helicopter= 150
y_helicopter= 200
x_bullet= 260
y_bullet= 240
y_move = 0
x_move = 0
y_bullet_move = 0
x_bullet_move = 0

Game_Over=False

def GameOver():
    pygame.quit()
    quit()


while not Game_Over:
    for event in pygame.event.get():
        if (event.type == pygame.QUIT):
            Game_Over=True
        if (event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_UP):
                y_move = -5
                x_move = 0
                y_bullet_move = -5
                x_bullet_move = 0
                if (event.type == pygame.KEYDOWN):
                    if(event.key == pygame.K_SPACE):
                        y_bullet_move = -20
                        x_bullet_move = 0

        if (event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_DOWN):
                y_move = 5
                x_move = 0
               # y_bullet_move = 5
               # x_bullet_move = 0
               # if (event.key == pygame.K_KP_ENTER):
                #    y_bullet_move = 10
                 #   x_bullet_move = 0
        if(event.type == pygame.KEYDOWN):
            if(event.key == pygame.K_RIGHT):
                x_move = 5
                y_move = 0
                x_bullet_move = 5
                y_bullet_move = 0
                #if(event.key == pygame.K_KP_ENTER):
                 #   x_bullet_move = 10
                  #  y_bullet_move = 0
        if(event.type == pygame.KEYDOWN):
            if(event.key == pygame.K_LEFT):
                x_move = -5
                y_move = 0
               # x_bullet_move = -5
               # y_bullet_move = 0
               # if(event.key == pygame.K_KP_ENTER):
                #    x_bullet_move = -100
                 #   y_bullet_move = 0

    y_helicopter+=y_move
    x_helicopter+=x_move
    x_bullet+=x_bullet_move
    y_bullet+=y_bullet_move

    surface.fill(black)

    helicopter(x_helicopter,y_helicopter,helicopterPicture)
    bulletshape(x_bullet,y_bullet,bulletPicture)

    if (y_helicopter>950 or y_helicopter<25 or x_helicopter>950 or x_helicopter<25):
        GameOver()

    pygame.display.update()
    clock.tick(10)


pygame.quit()
quit()
Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48
bobitm
  • 29
  • 2
  • 1
    the event for `if(event.key == pygame.K_SPACE):` looks not well indented, is it the same in your code ? – PRMoureu May 20 '18 at 07:54
  • yes it is because i want to shoot in the direction that i'm moving – bobitm May 20 '18 at 08:02
  • also i want to create bullets after shooting and i don't know how – bobitm May 20 '18 at 08:03
  • no the bullet should go up... can anyone tell me how to shoot the bullet and create it again? – bobitm May 20 '18 at 08:06
  • @HarshitAgrawal that's incorrect. The y-axis in pygame is inverted, so an object with negative y-speed moves upwards. – skrx May 20 '18 at 08:51
  • @bobitm should there be only one bullet at a time or several? Also, have you checked out any other questions and answers about bullets? There are already quite a lot. – skrx May 20 '18 at 09:00
  • it should be multiple... yes indeed I've checked similar topics yet found no solution – bobitm May 20 '18 at 09:08
  • And you want to shoot in the direction of the player/helicopter? – skrx May 20 '18 at 09:13
  • I think this [answer](https://stackoverflow.com/a/45666029/6220679) will help you. I could just copy and paste it here, and that means I should vote to close this question, since it's a duplicate, unless you want to do someting different. – skrx May 20 '18 at 09:18

0 Answers0