Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
so i made a simple game in python useing pygame,
when i put
kn=knight(100,100,40,50,2)
it stops working how do i fix that my code is

Python
import pygame
# how to do img /img/hero/knight 3 idle.png ex for me
pygame.init()

clock=pygame.time.Clock()
fps=60
#pygame widow
bottom_panel=100
screen_width=800
screem_height=400+bottom_panel
screen = pygame.display.set_mode((screen_width, screem_height))
pygame.display.set_caption('Hero World Battle')


#load images

#back ground image
background_img=pygame.image.load('something.png')
panel_img=pygame.image.load('pixil-frame-0(1).png')
#function for background
def draw_bg():
    clock.tick(fps)
    screen.blit(background_img, (0,0))
def draw_panel():
    screen.blit(panel_img, (-110,255))

class knight():
    def __init__(self,x,y,max_hp,power,potions):

        self.max_hp=max_hp
        self.hp=max_hp
        self.power=power
        self.start_potions=potions
        self.alive=True
        self.image=pygame.image.load('/img/Hero Knight/Sprites/HeroKnight/Idle/HeroKnight_Idle_0.png')
        self.rect=self.image.get_rect()
        self.rect.center(x,y)



    def draw(self):
        screen.blit(self.image,self.rect)


kn=knight(100,100,40,50,2)
run=True
while run:
  clock.tick(fps)

   #draw background
  draw_bg()
  draw_panel()
  for event in pygame.event.get():
       if event.type == pygame.QUIT:
           run=False
           print(run)
  pygame.display.update()

pygame.quit()


What I have tried:

i have tried chageing kn but it still happen also i have tried removeing code that dose not help
Posted
Updated 5-Jun-22 9:00am
Comments
Richard MacCutchan 6-Jun-22 6:05am    
Please show any error messages that appear when you run the program.

1 solution

At a guess - and that's all it can be without access to your system - the path is wrong, or not accessible to the user running the code:
self.image=pygame.image.load('/img/Hero Knight/Sprites/HeroKnight/Idle/HeroKnight_Idle_0.png')

So start by check that against the folder the code is running from, and use pdb — The Python Debugger — Python 3.10.4 documentation[^] to follow your code through while it is running to see exactly what is going on.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900