user116541
user116541

Reputation: 106

Why doesn't my python program start pygame?

I am making a game in pygame and practicing OOP. I've read a pygame tutorial and because my game is a bit different, I only took the parts I need such as movement, pygame.init(), etc. When I run the the script, there isn't any error just the pygame welcome message. I've checked if I actually added the lines of code to start the program and I did. I also checked if I made the screen and I also did that.

  1 import pygame
  2 from pygame.locals import *
  3 
  4 class character:
  5     def __init__(self, Name, Picture, Attack1, WaitAttack1, Attack2, WaitAttack2, Heal1, WaitHeal1, SuperAttack1, WaitSuperAttack1):
  6         self.Name = Name
  7         self.Player = pygame.image.load(Picture)
  8         self.Coins = 0
  9         self.Backpack = "Empty"
 10         self.WorldMap = "Comming soon"
 11         self.Health = 250
 12         self.Attack1 = Attack1
 13         self.WaitAttack1 = WaitAttack1
 14         self.Attack2 = Attack2
 15         self.WaitAttack2 = WaitAttack2
 16         self.Heal1 = Heal1
 17         self.WaitHeal1 = WaitHeal1
 18         self.SuperAttack1 = SuperAttack1
 19         self.WaitSuperAttack1 = WaitSuperAttack1
 20         self.Keys = [False, False, False, False]
 21         self.playerpos = [100,100]
 22 
 23     def directions(self):
 24 
 25         if self.Keys[0]:
 26             self.playerpos[1] -= 5
 27         elif self.Keys[2]:
 28             self.playerpos[1] += 5
 29         if self.Keys[1]:
 30             self.playerpos[0] -= 5
 31         elif self.Keys[3]:
 32             self.playerpos[0] += 5
 33 
 34 
 35 
 36 class Slade(character):
 37     def __init__(self): 38         character.__init__(self, "Slade", "Slade.png", 40, 20, 50, 25, 10, 5, 30, 25)
 39     
 40     def movement(self):
 41         while 1 == 1:
 42             pygame.init()
 43             self.Width, self.Height = 640, 480
 44             self.screen = pygame.display.set_mode((self.Width, self.Height))
 45             self.screen.fill(0)
 46             self.screen.blit(self.Player, self.playerpos)
 47             pygame.display.flip()
 48             for event in pygame.event.get():
 49                 if event.type == pygame.QUIT:
 50                     pygame.quit()
 51                     exit(0)
 52 
 53                 if event.type == pygame.KEYDOWN:
 54                     if event.key == K_w:
 55                         self.Keys[0] = True
 56                     elif event.key == K_a:
 57                         self.Keys[1] = True
 58                     elif event.key == K_s:
 59                         self.Keys[2] = True
 60                     elif event.key == K_d:
 61                         self.Keys[3] = True
 62 
 63                 if event.type == pygame.KEYUP:
 64                     if event.key == pygame.K_w:
 65                         self.Keys[0] = False
 66                     elif event.key == pygame.K_a:
 67                         self.Keys[1] = False
 68                     elif event.key == pygame.K_s:
 69                         self.Keys[2] = False
 70                     elif event.key == pygame.K_d:
 71                         self.Keys[3] = False
 72 
 73 
 74 if __name__ == '__main__':
 75     slade = Slade()
 76     slade.movement()

Thanks!

P.S.: I'm a newbie at OOP. So please try to ignore the fact that my code is terrible.

Upvotes: 2

Views: 68

Answers (2)

Henry Gustafson
Henry Gustafson

Reputation: 81

I am not actually too knowledgeable about this either, but it looks like only the init function is running, not the movement function, which has pygame.init().

Upvotes: 1

Aleksander Ikleiw
Aleksander Ikleiw

Reputation: 2685

At first, you have to assign the class to a variable. Then call a function, because after assigning the class the only thing that will run will be the __init__() function. For example

if __name__ == '__main__':
    obj = Slade()
    obj.movement()

There are some other issues within your code such as def __init__(self): 37 def __init__(self): but assigning a class to a variable and then calling a function will run the desired function. Also note, that you can use the __init__() class only once.

Upvotes: 3

Related Questions