Umut Kaya Bal
Umut Kaya Bal

Reputation: 15

Python pygame bullet mechanics

enter image description here

enter image description here Hello i need a quick fix for that i found where error is but cant fix it until now. BulletY is working good, but BulletX is starting at 0 here is code:

import pygame
import keyboard
from pygame import mixer
pygame.mixer.init(44100, -16, 2, 64)
pygame.init()

mixer.music.load('music/bgmusic.mp3')
mixer.music.play(-1)

#fonts
game_font = pygame.font.Font('font/BOLDFONT/THEBOLDFONT.ttf',50)
death_font = pygame.font.Font('font/GOWFONT/GODOFWAR.ttf',20)

#settings
window = pygame.display.set_mode((1280,720))#setting screen
pygame.display.set_caption('test')#setting window name
clock = pygame.time.Clock()#clock module (later used for frames)

#health_bar
current_health = 500
max_health = 1200
health_bar_length = 500
health_ratio = max_health / health_bar_length

def get_damage(amount):#damage funct
    global current_health
    if current_health > 0:
        current_health -= amount
    if current_health <= 0:
        current_health = 0
        
def get_health(amount):#heal funct
    global current_health
    if current_health < max_health:
        current_health += amount
    if current_health >= max_health:
        current_health = max_health
        
def healthbaradvanced():#healthbar
    pygame.draw.rect(window,'Red',(10,10,current_health/health_ratio,35))
    pygame.draw.rect(window,'Black',(10,10,health_bar_length/2.4,35),5)    
    health_bar_text_surface = death_font.render(str(current_health),True,'Black')#transform integer to string
    health_bar_text_rectangle = health_bar_text_surface.get_rect(midleft =(30,25))
    window.blit(health_bar_text_surface,health_bar_text_rectangle)
        
#surfaces
ground_surface = pygame.image.load('pictures/sand.png').convert_alpha()

stone_surface = pygame.image.load('pictures/stone.png').convert_alpha()
stone_rectangle = stone_surface.get_rect(center = (640,360))

player_surface = pygame.image.load('pictures/player.png').convert_alpha()
player_rectangle = player_surface.get_rect(center = (0,0)) #there is problem if i set player ret x = a [![enter image description here][1]][1]bullet starts from a point

text_surface = game_font.render('WELCOME',True,'Black')
text_rectangle = text_surface.get_rect(center = (640,90))

#bullet
bullet_surface = pygame.image.load('pictures/bullet.png')
bullet_x = player_rectangle.x #problem there
bullet_y = player_rectangle.y
bullet_x_change = 10
bullet_y_change = 0
bullet_state = "ready"

def fire_bullet(x,y):
    global bullet_state
    bullet_state = "fire"
    window.blit(bullet_surface,(x,y))


   
#main loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
         
    #block image translation
    window.blit(ground_surface,(0,0))
    window.blit(stone_surface,stone_rectangle)
    window.blit(player_surface,player_rectangle)
    window.blit(text_surface,(text_rectangle))
    
    #health bar
    healthbaradvanced()
    
    #get damage   
    if player_rectangle.colliderect(stone_rectangle):get_damage(10)
        
    #player animation
    speed_player=5.0
    if keyboard.is_pressed('w'):player_rectangle.y -= speed_player
    if keyboard.is_pressed('s'):player_rectangle.y += speed_player
    if keyboard.is_pressed('a'):player_rectangle.x -= speed_player
    if keyboard.is_pressed('d'):player_rectangle.x += speed_player
    
    #i tried writing "bullet_x = player_rectangle" to there but i cant shoot bullets after it
     
    #bullet movement
    if bullet_state == "fire":
        bullet_x
        fire_bullet(bullet_x,player_rectangle.y)
        bullet_x += bullet_x_change
        
    #shot bullet
    if keyboard.is_pressed('space'):fire_bullet(player_rectangle.x,player_rectangle.y)    
    
    #respawn
    if current_health == 0: 
        deathsound = mixer.Sound('music/deathsound.mp3')
        deathsound.play()
        player_rectangle.x = 500
        player_rectangle.y = 500
        current_health = 500
       
    pygame.display.update()    
    clock.tick(60)

pygame.quit()

I understand that occurs because of i write that bullet_x = player_rectangle.x before player movement codes. But whenever i write that code below movement codes at main loop it wont shoot bullets

Upvotes: -1

Views: 177

Answers (1)

Rabbid76
Rabbid76

Reputation: 211116

You must set the X-coordinate of the bullet when SPACE is pressed and the bullet is ffired:

while True:
    # [...]

    #shot bullet
    if keyboard.is_pressed('space'):
        bullet_x = player_rectangle.x
        fire_bullet(player_rectangle.x,player_rectangle.y)    

    # [...]

I suggest reading How can i shoot a bullet with space bar? and How do I stop more than 1 bullet firing at once?.

Upvotes: 1

Related Questions