0
\$\begingroup\$

This is my code:

import pygame
import os
import time
pygame.font.init()
import random


clock = pygame.time.Clock()
timez = 0
time_count = 1000
WIDTH, HEIGHT = 1300, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("mortal kombat")
YELLOW = (255,255,0)
vel = 5
BG = pygame.transform.scale(pygame.image.load("mortal kombat/mk 11 dragon grotto.jpg"),(WIDTH,HEIGHT))
fireball_image = pygame.transform.scale(pygame.image.load("mortal kombat/Explosion-Fireball-PNG.png"),(100,100))
Fire_vel = 5
Fire_list = []
Max_fire = 6
HIT = pygame.USEREVENT + 1
def draw(Fire_list):
    WIN.blit(BG,(0,0))
    WIN.blit(Liu_Kang_Character,(LK_rect.x,LK_rect.y))

    
    for fire in Fire_list:
           pygame.draw.rect(WIN,(255,255,0),fire)
           
           fire__y = fire.y
           WIN.blit(fireball_image,(fire.x-15,fire__y-45))
       
    pygame.display.update()
    
def pressing_keys(keys_pressed,LK):
    if keys_pressed[pygame.K_a] and LK.x >0:
           LK.x -= 8
    if keys_pressed[pygame.K_d] and LK.x < WIDTH - 250:
           LK.x += 8
    if keys_pressed[pygame.K_w] and LK.y > 0 :
           LK.y -= 8
    if keys_pressed[pygame.K_s] and LK.y < HEIGHT - 375:
           LK.y += 8
           
        
def handle_fire(Fire_list):
     for fire in Fire_list:         
            fire.x -= Fire_vel
            
            
            ############
            timez += clock.tick(60)
    
            if timez >= time_count:
                 fire_y = random.randint(0,800)
                 fire = pygame.Rect(1200,fire_y,15,15)
                          
                          
                 Fire_list.append(fire)
                 timez = 0
                 
            if fire.x < 0:
                   Fire_list.remove(fire)
                   
            ##############
                   
            if LK_rect.colliderect(fire):
                pygame.event.post(pygame.event.Event(HIT))
                Fire_list.remove(fire)  
                
                  
    
#liu kang
Liu_Kang_size = (50,50)

Liu_Kang_image= pygame.image.load(os.path.join('mortal kombat','Liu Kang-PNG-File.png'))
Liu_Kang_Character = pygame.transform.scale(Liu_Kang_image,(250,375))
LK_rect = pygame.Rect(100,300,250,375)
    
run = True    

while run:
    
    
    draw(Fire_list)
   
    keys_pressed = pygame.key.get_pressed()
    pressing_keys(keys_pressed,LK_rect)
    

    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                break
         
                   
    print(Fire_list)
    
    handle_fire(Fire_list)
    
    
pygame.quit()

Somehow fire rects don't appear. But when the code is like this:

import pygame
import os
import time
pygame.font.init()
import random

clock = pygame.time.Clock()
timez = 0
time_count = 1000
WIDTH, HEIGHT = 1300, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("mortal kombat")
YELLOW = (255,255,0)
vel = 5
BG = pygame.transform.scale(pygame.image.load("mortal kombat/mk 11 dragon grotto.jpg"),(WIDTH,HEIGHT))
fireball_image = pygame.transform.scale(pygame.image.load("mortal kombat/Explosion-Fireball-PNG.png"),(100,100))
Fire_vel = 5
Fire_list = []
Max_fire = 6
HIT = pygame.USEREVENT + 1
def draw(Fire_list):
    WIN.blit(BG,(0,0))
    WIN.blit(Liu_Kang_Character,(LK_rect.x,LK_rect.y))
    
    
    
    
    for fire in Fire_list:
           pygame.draw.rect(WIN,(255,255,0),fire)
           
           fire__y = fire.y
           WIN.blit(fireball_image,(fire.x-15,fire__y-45))
           
    
           
           
    pygame.display.update()
        
        

def pressing_keys(keys_pressed,LK):
    if keys_pressed[pygame.K_a] and LK.x >0:
           LK.x -= 8
    if keys_pressed[pygame.K_d] and LK.x < WIDTH - 250:
           LK.x += 8
    if keys_pressed[pygame.K_w] and LK.y > 0 :
           LK.y -= 8
    if keys_pressed[pygame.K_s] and LK.y < HEIGHT - 375:
           LK.y += 8
           
        
def handle_fire(Fire_list):
     for fire in Fire_list:         
            fire.x -= Fire_vel
            
              
            if fire.x < 0:
                   Fire_list.remove(fire)
                   
            
                   
            if LK_rect.colliderect(fire):
                pygame.event.post(pygame.event.Event(HIT))
                Fire_list.remove(fire)  
                
                  
    
#liu kang
Liu_Kang_size = (50,50)

Liu_Kang_image= pygame.image.load(os.path.join('mortal kombat','Liu Kang-PNG-File.png'))
Liu_Kang_Character = pygame.transform.scale(Liu_Kang_image,(250,375))
LK_rect = pygame.Rect(100,300,250,375)
    
run = True    

while run:
    
    
    draw(Fire_list)
   
    keys_pressed = pygame.key.get_pressed()
    pressing_keys(keys_pressed,LK_rect)
    

    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                break
    ##########     
    timez += clock.tick(60)
    
    if timez >= time_count:
                 fire_y = random.randint(0,800)
                 fire = pygame.Rect(1200,fire_y,15,15)
                          
                          
                 Fire_list.append(fire)
                 timez = 0
         
    ##########
           
    print(Fire_list)
    
    handle_fire(Fire_list)
    
    
pygame.quit()

...then the fire images appear.

I'm wondering why when I shift it to the function it doesn't appear anymore, while if it's inside the while loop, it does, since the function is called in the while loop anyway.

For easy reference I've marked the area ive changed with ####. All I did was switch around the places.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Fire_list is empty

My first clue was that in the first (non-functional) code, you have timez += clock.tick(60) inside the foreach loop. This seemed weird to me because usually you only want to increment time once per frame, not once per fire per frame.

This turned out not to be the root cause, but it got me thinking, "How many times per frame is this running?"

In the working example, the following code runs exactly once per frame:

if timez >= time_count:
    fire_y = random.randint(0,800)
    fire = pygame.Rect(1200,fire_y,15,15)

    Fire_list.append(fire)
    timez = 0

In the non-working example, that code runs once per fire per frame. If there are zero fires, it will never be run. Because it is never run, fire never gets appended to the list, so the list remains empty.

\$\endgroup\$
1
  • \$\begingroup\$ Works now, thanks! In all honesty, i just added it to the once per fire as all my fire object related stuff was there, I guess i didnt really think through what it really meant. \$\endgroup\$
    – David Tan
    Jul 12, 2023 at 15:52

You must log in to answer this question.

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