Home > database >  bullet stops when mouse isnt moving in pygame
bullet stops when mouse isnt moving in pygame

Time:10-21

Whenever I run this code and I click to shoot a bullet, the bullet moves in the direction it is supposed to go only when I continuously move the mouse. How do I make the bullet move at all times? I don't understand why it does this. I appreciate your help! I'm a noobie at this. This is my code for the game so far:

import pygame
import os
import math

# regular constants?
WIDTH, HEIGHT = 900, 500
window = pygame.display.set_mode((WIDTH, HEIGHT))
FPS = 60
VEL = 5
BULLET_VEL = 8
Sm_L, Sm_H = 50, 50
B_L, B_H = 5, 5
toaster = pygame.image.load(os.path.join("assets", 'indis.png')).convert()
toaster = pygame.transform.scale(toaster, (1000, 500))
pygame.display.set_icon(toaster)
smile = pygame.image.load(os.path.join("assets", "smile.png")).convert_alpha()
smile = pygame.transform.scale(smile, (Sm_L, Sm_H))
bullet_img = pygame.image.load(os.path.join("assets", 'bullet.png')).convert_alpha()
bullet_img = pygame.transform.scale(bullet_img, (Sm_L, Sm_H))


# window draw

def draw_window(toast, smiler, bullets):
    window.fill((255, 255, 255))
    window.blit(toaster, (toast.x, toast.y))
    window.blit(smile, (smiler.x, smiler.y))
    for bullet in bullets:
        window.blit(bullet_img, (bullet.x, bullet.y))
    pygame.display.flip()
    pygame.display.update()


def movement_handler(keys_pressed, smilez):
    if keys_pressed[pygame.K_a]:
        smilez.x -= VEL
    if keys_pressed[pygame.K_d]:
        smilez.x  = VEL
    if keys_pressed[pygame.K_w]:
        smilez.y -= VEL
    if keys_pressed[pygame.K_s]:
        smilez.y  = VEL


def shoot():
    pass


def main():
    toasterz = pygame.Rect(0, 0, 1000, 500)
    smilez = pygame.Rect(0, 0, Sm_L, Sm_H)
    border = pygame.Rect(0, 0, WIDTH, HEIGHT)
    bullets = []
    mouse_pos = []
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.MOUSEBUTTONDOWN:
                bullet = pygame.Rect(smilez.x, smilez.y, B_L, B_H)
                bullets.append(bullet)
                mouse_pos.append(pygame.mouse.get_pos())
                print(bullets)
                print(mouse_pos)

                pass

            for bullet in bullets:
                mouse_x, mouse_y = mouse_pos[bullets.index(bullet)]
                angle = math.atan2(mouse_y - smilez.y, mouse_x - smilez.x)
                # print("angle in degrees ", int(angle * 180 / math.pi))
                bullet.x  = math.cos(angle) * BULLET_VEL
                bullet.y  = math.sin(angle) * BULLET_VEL
                # pygame.draw.circle(toaster, (255, 30, 70), (bullet.x, bullet.y), 5)
                if len(bullets) == 2:
                    bullets.pop(0)
                    mouse_pos.pop(0)

            """try:
                pygame.draw.circle(toaster, (255, 30, 70), (bullet.x, bullet.y), 5)
            except:
                pass
"""
        # Basic Control
        movement_handler(pygame.key.get_pressed(), smilez, )
        shoot()
        draw_window(toasterz, smilez, bullets)

    pygame.quit()


if __name__ == "__main__":
    main()

CodePudding user response:

It's a matter of Indentation. You have to do the movement in the application and not in the event loop. The code in the event loop is executed only when an event occurs (for example, a mouse movement):

def main():
    # [...]

    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.MOUSEBUTTONDOWN:
                bullet = pygame.Rect(smilez.x, smilez.y, B_L, B_H)
                bullets.append(bullet)
                mouse_pos.append(pygame.mouse.get_pos())
                print(bullets)
                print(mouse_pos)

        # INDENTATION
        #<--|

        for bullet in bullets:
            mouse_x, mouse_y = mouse_pos[bullets.index(bullet)]
            angle = math.atan2(mouse_y - smilez.y, mouse_x - smilez.x)
            # print("angle in degrees ", int(angle * 180 / math.pi))
            bullet.x  = math.cos(angle) * BULLET_VEL
            bullet.y  = math.sin(angle) * BULLET_VEL
            # pygame.draw.circle(toaster, (255, 30, 70), (bullet.x, bullet.y), 5)
            if len(bullets) == 2:
                bullets.pop(0)
                mouse_pos.pop(0)


        # Basic Control
        movement_handler(pygame.key.get_pressed(), smilez, )
        shoot()
        draw_window(toasterz, smilez, bullets)
  • Related