1

This is an early prototype for a game I'm currently working on for a project. However it seems that I have reached a roadblock where the player character cannot be displayed as you can see in the code I have created a class for the player

import pygame

pygame.init()
win = pygame.display.set_mode((500,480))#initialises the game window
pygame.display.set_caption("Hello world")
bg = pygame.image.load('bg.jpg')

#player class
class player:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.width = 64
        self.height = 64
        self.standing = True
        self.left = False
        self.right = True
        self.vel = 5
        self.jumping = False
        self.jumpCount = 10
    def move(self,x,y):
        self.k = pygame.key.get_pressed() 
        if self.k[pygame.K_LEFT] and self.x  > 0 + self.vel + self.width:
            left = True
            right = False
            self.standing = False
            self.x -= vel
        elif self.k[pygame.K_RIGHT] and self.x  < 500-self.vel-self.width:
              self.right = True
              self.left = False
              self.standing = False
              self.x += vel
        else:
            self.standing = True       
        if self.jumping:#checks if users jumping intiating jump
            if self.k[pygame.K_SPACE]:
                if self.jumpCount >= -10:
                    neg = 1
                if self.JumpCount < 0:
                    neg = -1
                self.y -= (jumpCount ** 2) * 0.5 * neg
                self.jumpCount -= 1
            else:
                self.jumping = False
                self.jumpCount = 10

    def draw(self,win,move):
        wLeft = pygame.image.load('runningleft.png')
        wRight = pygame.image.load('running.png')
        char = pygame.image.load('idleright.png')
        if not(self.standing):
            if self.left:
                win.blit(wleft,(self.x,self.y))
            elif self.right:
                win.blit(wright,(self.x,self.y))
        else:
            win.blit(char,(self.x,self.y))

        pygame.display.update()



pygame.display.update()
wizard = player(50,450)
run = True
while run:#main game loop
   for event in pygame.event.get():#loops through a list of keyboard or mouse events
       if event.type == pygame.QUIT:
           run = False
   wizard.move(wizard.x,wizard.y)
   wizard.draw(win)
   win.blit(bg,(0,0))
   pygame.display.update()
pygame.quit()

The background is shown in the main game window, whereas the character isn't. As stated before I attempted to transition the project into OOP which is where my code stopped. How can I diagnose the problems in my code?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • When I duplicate your code, I get an error that `wizard.draw(win)` is missing the required argument `move`. From this method, `def draw(self,win,move):`. Maybe you could post the entire code, with all the `jpg` in a dropbox or something? – RightmireM Oct 26 '19 at 09:40
  • Please read [how to ask](https://stackoverflow.com/help/how-to-ask). And [this post about asking help ASAP](https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest) – Valentino Oct 26 '19 at 09:50
  • https://drive.google.com/file/d/1ADjYKACTRotcKL_1bliUcloAQIiHRJ8E/view?usp=sharing – KremlingKoder Oct 26 '19 at 10:09

1 Answers1

0

The order of your instructions is wrong. You need to draw the player (wizard) after drawing the background and before updating the display:

  1. win.blit(bg,(0,0))
  2. wizard.draw(win)
  3. pygame.display.update()
wizard = player(50,450)

#main game loop
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    wizard.move(wizard.x,wizard.y)        
    
    win.blit(bg,(0,0))
    wizard.draw(win)
    pygame.display.update()

It is sufficient to update the display once at the end of the loop, so remove pygame.display.update() from player.draw. Multiple updates to the display cause flickering. See Why is the PyGame animation is flickering

Rabbid76
  • 202,892
  • 27
  • 131
  • 174