I would like to get my pygame character walking. I tried everything but it's not moving. The player().x is not moving. What can I try next?
import pygame
pygame.init()
WIDTH, HEIGHT = 1920, 1080
ground = (69, 30, 6)
main_menu_color = (0, 0, 0)
max_fps = 60
class player:
def __init__(self):
self.x = 100
self.y = 300
self.width = 224
self.height = 224
self.site = "Left"
self.look = pygame.image.load('character/sprite_0.png')
def tick(self):
pass
def walking(self):
self.x = self.x 10
print(self.x)
def drawscreen(playerpos):
screen.fill((69, 30, 6))
screen.blit(player().look, (player().x, player().y))
pygame.display.update()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Zooombie!")
def main():
playerpos = pygame.Rect(player().x, player().y , player().width, player().height)
clock = pygame.time.Clock()
run = "run"
while run == "run":
clock.tick(max_fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
player().tick()
player().walking()
drawscreen(playerpos)```
CodePudding user response:
You're not creating a persistent instance of player
:
p = player()
You should update this rather than an unnamed player
. For example:
def drawscreen(p):
screen.fill((69, 30, 6))
screen.blit(p.look, (p.x, p.y))
pygame.display.update()
p.tick()
p.walking()
drawscreen(p)
CodePudding user response:
Read about Classes and Instance Objects.player()
creates a new instance of the class player
. So every time you call player()
, a new player object is created. You must create an instance at the beginning of the program and use that instance throughout the program.
Also see Style Guide for Python Code. Class names should normally use the CapWords convention and variable names should be lower case. So the name of the class should be Player
and the name of the instance object can be player
.
import pygame
pygame.init()
WIDTH, HEIGHT = 1920, 1080
ground = (69, 30, 6)
main_menu_color = (0, 0, 0)
max_fps = 60
class Player:
def __init__(self):
self.x = 100
self.y = 300
self.width = 224
self.height = 224
self.site = "Left"
self.look = pygame.image.load('character/sprite_0.png')
def tick(self):
pass
def walking(self):
self.x = self.x 10
def drawscreen(player):
screen.fill((69, 30, 6))
screen.blit(player.look, (player.x, player.y))
pygame.display.update()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Zooombie!")
def main():
player = Player()
clock = pygame.time.Clock()
run = "run"
while run == "run":
clock.tick(max_fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = "end"
player.tick()
player.walking()
drawscreen(player)
pygame.quit()
if __name__ == '__main__':
main()
CodePudding user response:
There are some problems in your code related to how a class and its instantiated objects work. Look at the following code and try to run it:
import pygame
pygame.init()
WIDTH, HEIGHT = 600, 480
ground = (69, 30, 6)
main_menu_color = (0, 0, 0)
max_fps = 60
class player:
def __init__(self):
self.x = 100
self.y = 300
self.width = 224
self.height = 224
self.site = "Left"
self.look = pygame.image.load('character/sprite_0.png')
def tick(self):
pass
def walking(self):
self.x = 10
print(self.x)
def drawscreen(playerpos):
screen.fill((69, 30, 6))
screen.blit(pygame.image.load('character/sprite_0.png'), (playerpos.x, playerpos.y))
pygame.display.update()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Zooombie!")
def main():
player1 = player()
clock = pygame.time.Clock()
run = "run"
while run == "run":
playerpos = pygame.Rect(player1.x, player1.y , player1.width, player1.height)
clock.tick(max_fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
player1.tick()
player1.walking()
drawscreen(playerpos)
if __name__ == "__main__":
main()
Here are some things to note:
Class methods can not be used directly like that (in this case). You need to instantiate an object of the class like player1 = player(). This means you created an object player1 from the class player that you created.
An object is not callable i.e. to access its properties you need to use the dot operator in Python. Like player1.x and so on.
Lastly, your drawscreen function contains the screen.blit function which takes in parameters source: Surface and dest: list of coordinates. I changed it to the correct parameters in this case. Your source parameter was a callable so was not throwing an error, but what it was returning, that was not the expected type required.
Hope this helps!