0
\$\begingroup\$

I'm kinda new to python and pygame, anyone know why this won't show bullets coming out from my character?

from curses import window
import sys
from tkinter import Y
from turtle import speed
import pygame
import os
import math


WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('First Person Shooter')

WHITE = (255,0,0)

FPS = 60
VEL = 5
BULLET_VEL = 7

CHARACTER_PLAYER_ONE = pygame.image.load(os.path.join('Python', 'player_one.png'))
CHARACTER_PLAYER_ONE = pygame.transform.rotate(pygame.transform.scale(CHARACTER_PLAYER_ONE, (55, 40)), 90)

class PLAYERPOSITION:
    def __init__(player, x, y):
        player.x = x
        player.y = y



def draw_window(one):
    WIN.fill(WHITE)
    WIN.blit(CHARACTER_PLAYER_ONE, (one.x, one.y))
    pygame.display.update() 

class PlayerBullet:
    def __init__(self, x, y, mouse_x, mouse_y):
        self.x = x
        self.y = y
        self.mouse_x = mouse_x
        self.mouse_y = mouse_y
        self.speed = 15
        self.angle = math.atan2(self.mouse_y, self.mouse_x)
        self.x_vel = math.cos(self.angle) * self.speed
        self.y_vel = math.sin(self.angle) * self.speed
    def main(self, display):
        self.x -= int(self.x_vel)
        self.y -= int(self.y_vel)

        CHARACTER_PLAYER_ONE.main(display)

        for bullet in player_bullets:
            bullet.main(display)

        pygame.draw.circle(display, (0,0,0), (self.x, self.y), 5)

        playerx = x_movement
        playery = y_movement

display_scroll = [0,0]

def x_movement(keys_pressed, one):
        if keys_pressed[pygame.K_a] and one.x - VEL > -15: # left
            one.x -= VEL
        if keys_pressed[pygame.K_d] and one.x + VEL + one.width < 925: # right
            one.x += VEL

def y_movement(keys_pressed, one):
        if keys_pressed[pygame.K_w] and one.y - VEL > -15: # up
            one.y -= VEL
        if keys_pressed[pygame.K_s] and one.y + VEL + one.height < 500: # down
            one.y += VEL

player_bullets = []

def main():
    one = pygame.Rect(100, 300, 55, 40)

    clock = pygame.time.Clock()
    run = True
    while run:

        clock.tick(FPS)

        mouse_x, mouse_y = pygame.mouse.get_pos()

        draw_window(one)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    bullet = pygame.Rect(5,5,5,5)

            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    player_bullets.append(PlayerBullet(x_movement, y_movement, mouse_x, mouse_y))

        keys_pressed = pygame.key.get_pressed()

        
        x_movement(keys_pressed, one)
        y_movement(keys_pressed, one)



    pygame.quit()


if __name__ == '__main__':                
    main()

I'm trying to get it to shoot small black circles, but it just doesn't want to seem to do it

\$\endgroup\$
1
  • \$\begingroup\$ Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. \$\endgroup\$
    – Community Bot
    Feb 16, 2022 at 17:03

1 Answer 1

1
\$\begingroup\$

You add PlayerBullet to list player_bullets but you never draw these bullets.

def draw_window(one):
    WIN.fill(WHITE)
    WIN.blit(CHARACTER_PLAYER_ONE, (one.x, one.y))
    
    for item in player_bullets:  # <---
        item.draw(WIN)           # <---
        
    pygame.display.update() 

It also need function draw() in PlayerBullet

class PlayerBullet:

    # ... code ...

    def draw(self, display):
        pygame.draw.circle(display, (0,0,0), (self.x, self.y), 5)

I found another problem: you send x_movement, y_movement to PlayerBullet

player_bullets.append(PlayerBullet(x_movement, y_movement, mouse_x, mouse_y))

but x_movement, y_movement are functions, not values

def x_movement(keys_pressed, one):
        if keys_pressed[pygame.K_a] and one.x - VEL > -15: # left
            one.x -= VEL
        if keys_pressed[pygame.K_d] and one.x + VEL + one.width < 925: # right
            one.x += VEL

def y_movement(keys_pressed, one):
        if keys_pressed[pygame.K_w] and one.y - VEL > -15: # up
            one.y -= VEL
        if keys_pressed[pygame.K_s] and one.y + VEL + one.height < 500: # down
            one.y += VEL

You should send one.x, one.y

player_bullets.append(PlayerBullet(one.x, one.y, mouse_x, mouse_y))
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .