2

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.

1
  • def __init__(self): 37 def __init__(self): what is the 37 doing there? Also you have pyjama.init() in a loop, I think that's only suppose to be called once. (side notes to the actual issue of never invoking movement) Commented Jul 17, 2020 at 18:21

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@user116541 can you please upvote my answer and accept it, please?
But it says that I have to wait 7min. before accepting it
The two init thing is a copy and paste mistake. Sorry.
1

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().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.