0

im trying to draw a shape with pygame.draw.rect() when i press a key but the shape wont stay when another key or event happen and also the shape moves where ever the ship moves but i only want it to move when i shot it and stay in that place and the shape wont stay on the surface after i release that key

import pygame

pygame.init()

display_width = 800
display_hight = 600
colors = {"black": (0, 0, 0), "white": (255, 255, 255), "green": (0, 255, 0), "red": (255, 0, 0), "blue": (0, 0, 255)}
ship_width = 75
ship_hight = 72
gameDisplay = pygame.display.set_mode((display_width, display_hight))
pygame.display.set_caption('test')
clock = pygame.time.Clock()

shipimg = pygame.image.load('D:\\index.png')

def ship(x, y):
    gameDisplay.blit(shipimg, (x, y))



def Game_Loop():
    x = (display_width * 0.41)
    y = (display_hight * 0.8)
    x_change = 0
    fire_start_y = y - ship_hight
    fire_start_x = x + (ship_width / 2)

    GameExit = False


    while not GameExit:
        if fire_start_y < 0:
            fire_start_y = y - 30
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                GameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                if event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0
        x += x_change

        gameDisplay.fill(colors["white"])

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_x:
                pygame.draw.rect(gameDisplay, colors["green"], [fire_start_x, fire_start_y, 2, 10])

        ship(x, y)
        if x <= 0 or x >= display_width - ship_width:
            x -= x_change
        fire_start_y -= 5
        fire_start_x = x + (ship_width / 2)


        pygame.display.flip()
        clock.tick(60)


Game_Loop()
pygame.quit()

i tried this code to manage the disappearing problem but it made it worse as it ever lopping after i press the key once

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_x:
                pygame.draw.rect(gameDisplay, colors["green"], [fire_start_x, fire_start_y, 2, 10])
                fire_start_x = x + (ship_width / 2)
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_x:
                pygame.draw.rect(gameDisplay, colors["green"], [fire_start_x, fire_start_y, 2, 10])
                fire_start_x = x + (ship_width / 2)
mohamed mahrous
  • 117
  • 1
  • 2
  • 8
  • If you just want to shoot bullets, then create a list and append `pygame.Rect`s (which represent the bullets) when you shoot. To move the rects upwards, use a `for` loop and decrease their `y` values. – skrx Aug 15 '17 at 02:06
  • I can show you an example, but it's not really clear if you really just want to shoot some bullets. Please describe your goals in detail. – skrx Aug 15 '17 at 06:30

2 Answers2

0

If you want to fire many bullets when a button is held down use this code.

if pygame.key.get_pressed[pygame.K_x] != 0:
    # Add code to add Rect objects to a list.
    # Add in code to add the Rect objects in intervals.

In the code above you must add code so that you don't add in a rect object during every interation of the game loop. If you don't you will fire 60 bullets in a second or 1 bullet per frame which may or may not be too many.

If you want to fire one bullet for every keydown event use this code.

if pygame.event.type == pygame.KEYDOWN:
    if pygame.event.key == pygame.K_x:
        # Add code to add Rect objects to a list.

During the rendering of your game loop you need to render the rect objects.

for bullet in bullets:
    # Blit the bullet to the screen 
    # Increment/Decrement the x/y values so they move.

Of course you need handling to remove bullets from this list. You will also need to code in collision detection.

If you explain more and in more detail I will add to the answer. Hope this helped!

EDIT: Okay you need to use the first block of code to have "pressed-down" firing of bullets. Add these rect objects to a list.

if pygame.key.get_pressed[pygame.K_x] != 0:
    bullets.append(pygame.Rect(x, y, 10, 3))
    # Add in a time delay.

Make sure you create the bullets list. The x and y value will be wherever the you want the bullets to fire from in relation to the ship.

Now render them.

for bullet in bullets:
    window.blit(bullet, [bullet.x, bullet.y])
    bullet.x += 3 # This will make the bullet go EAST.

Remove bullets which have gone off of the screen.

for bullet in bullets:
    if bullet.x < 0 or bullet.x + bullet.width > window_width:
        bullets.remove(bullet)
    # Add in some collision detection with other ships here.

Hope this helped!

Micheal O'Dwyer
  • 1,237
  • 1
  • 16
  • 26
  • ok here is my goals i want to shot bullets as long as i press a specific key assume that key is x and i want that bullet to start from another object lets say that object is a ship so as long as the bullet is shot the bullet direction wont change only its shooting direction change i hope that gives you more details – mohamed mahrous Aug 15 '17 at 13:13
  • Just saw your answer. Nevermid. – Micheal O'Dwyer Aug 15 '17 at 22:38
0

thanks guys but i found the answer i am looking for in this How to create bullets in pygame?

creating a list of bullets and then use append method to add direction fo every bullet i shot when i press x key and removeing them from the list if they are out of my game surface

here is what my code looks like now

bullets = []
bullet_width = 2
bullet_hight = 10
def Game_Loop():
    x = (display_width * 0.41)
    y = (display_hight * 0.8)
    x_change = 0
    bullet_y = y - (ship_hight/2)

    GameExit = False

    while not GameExit:
        bullet_x = x + (ship_width / 2)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                GameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                if event.key == pygame.K_RIGHT:
                    x_change = 5
                if event.key == pygame.K_x:
                    bullets.append([bullet_x,bullet_y])
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0
        x += x_change

        gameDisplay.fill(colors["white"])

        for bullet in bullets:
            pygame.draw.rect(gameDisplay, colors["green"], (bullet[0], bullet[1], bullet_width, bullet_hight))

        ship(x, y)
        if x <= 0 or x >= display_width - ship_width:
            x -= x_change
        for bullet in bullets:
            bullet[1] -= 5
            if bullet[1] < 0:
                bullets.remove(bullet)
        pygame.display.flip()
        clock.tick(60)
mohamed mahrous
  • 117
  • 1
  • 2
  • 8
  • Never change a list while you're iterating over it. That leads to unexpected results and not all desired items will be removed. Either create a new list or iterate over a copy `for bullet in bullets[:]:`. – skrx Aug 15 '17 at 20:21