pygame bullets is reset every time i press multiple time mousebutton

Question:

i have a problem with my game, i am beginner. My Bullets not work(if i press multiple time, my bullet resset)
for example if i want to shoot from the mouse (left_button), i press 1 time, bullet go top, if i press 3 times in 1 sec, my bullet is resst, coppy my code, and see!
Please!

import pygame

pygame.init()
red = (189, 87, 87)
white = (230, 230, 230)

xbullet = 150
ybullet = 510

tix = 380
tiy = 50

screen = pygame.display.set_mode((800, 600))
window_title = pygame.display.set_caption("bullets")
xa, ya = pygame.mouse.get_pos()
running = True
bullet = pygame.draw.rect(screen, white, pygame.Rect(xa, ya, 5, 20))
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            xa, ya = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()
            if click:
                print(xa, ya)

    screen.fill((94, 94, 94))
    ya -= 2.15
    object = pygame.draw.rect(screen, red, pygame.Rect(tix, tiy, 60 ,60))
    bullet = pygame.draw.rect(screen, white, pygame.Rect(xa, ya, 5, 20))
    pygame.display.flip()
    pygame.display.update()
Asked By: Octavian

||

Answers:

You have no bullets you have just 1 bullet. Create a list for the bullets:

bullet_list = []

Add a new bullet to the list when the mouse button is pressed:

if event.type == pygame.MOUSEBUTTONDOWN:
    xa, ya = event.pos
    bullet_list.append([xa, ya])

Draw and move all the bullets in the list:

for bullet_pos in bullet_list[:]:
    bullet_pos[1] -= 2.15
    pygame.draw.rect(screen, white, pygame.Rect(*bullet_pos, 5, 20))
    if bullet_pos[1] < 0.0:
        bullet_list.remove(bullet_pos)
Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.