Player not displaying when the code runs?

Viewed 72

The code dose give some errors but I don't understand them. I tried to search them up but nothing!

The errors are:

  File "d:\Python\sendsend\client.py", line 67, in <module>
    main()
  File "d:\Python\sendsend\client.py", line 64, in main
    redrawWindow(win,p)
  File "d:\Python\sendsend\client.py", line 50, in redrawWindow
    pygame.display.update()

The Code is:

import pygame

                                                    
width = 500
height = 500
win = pygame.display.set_mode ((width, height))
pygame.init()
pygame.display.set_caption("Client")

clientNumber = 0

class Player:
    def __init__(self, x, y, width, height, color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.rect = (x,y,width,height)
        self.val = 3
    
    def draw(self,win):
        pygame.init()
        pygame.draw.rect(win, self.color, self.rect)

    def move(self):
        pygame.init()
        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT]:
            self.x -= self.vel

        if keys[pygame.K_RIGHT]:
            self.x += self.vel

        if keys[pygame.K_DOWN]:
            self.y -= self.vel

        if keys[pygame.K_UP]:
            self.y += self.vel

        self.rect = self.x,self.y,self.width,self.height

     


def redrawWindow(win,player):   
    player.draw(win)                              
    win.fill((0,0,255))
    pygame.display.update()

def main () :                                       
    run = True
    p = Player(50,50,100,100,(0,255,0))

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:           
                run = False
                pygame.init()
                pygame.quit()
        
        pygame.init()
        p.move()
        redrawWindow(win,p)    

pygame.init()
main() 

I am trying to code an online game with the help of this tutorial:

https://www.youtube.com/watch?v=McoDjOCb2Zo&t=22s

By the way this is my first post so sorry if I made any mistakes!

1 Answers

There is a typo in Player.__init__. self.val needs to be self.vel:

self.val = 3

self.vel = 3

You need to draw the player after clearing the background and before updating the screen:

def redrawWindow(win,player):  
    win.fill((0,0,255)) 
    player.draw(win)                              
    pygame.display.update()

Use pygame.time.Clock to control the frames per second and thus the game speed.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

def main () :                                       
    run = True
    p = Player(50,50,100,100,(0,255,0))

    clock = pygame.time.Clock()
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:           
                run = False
                pygame.init()
                pygame.quit()
        
        pygame.init()
        p.move()
        redrawWindow(win,p)
        clock.tick(60)    

pygame.init()
main() 

The typical PyGame application loop has to:

Related