1

Hello I am trying to create a rpg game in pygame However, everytime i try to run the code to test it it tells me "descriptor 'blit' of 'pygame.Surface' object needs an argument"

I believe I have already givin it an argument.

import pygame
import os
from pygame.locals import*

screen_width = 1900
screen_height = 1080

black = (0, 0, 0)
white = (255, 255 ,255)
red = (255, 0 ,0)
lime = (0, 255, 0)
blue = (255, 255, 0)
aqua = (0, 255, 255)
magenta = (255, 0, 255)
silver = (192, 192, 192)
gray = (128, 128, 128)
maroon = (128, 0, 0)
olive = (128, 128, 0)
green = (0, 128, 0)
purple = (128, 0, 128)
teal = (0, 128, 128)
navy = (0, 0, 128)

pygame.display.init()
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption('This is a rpg')
clock = pygame.time.Clock()

current_path = os.path.dirname('untitled')
resource_path = os.path.join(current_path, 'Characters')
character_path = os.path.join(resource_path, 'Knight')
image_path = os.path.join(character_path, 'Walk_Attack')

playerimg = pygame.image.load(os.path.join(character_path, 'knight.png'))

x = (screen_width * 0.45) 
# I only put it up here because it told me "x" wasent defined in "def player(x, y):"

y = (screen_height * 0.8)

def player(x, y):
    x = (screen_width * 0.45)
    y = (screen_height * 0.8)
    pygame.Surface.blit(source = playerimg, x = x, y = y)

dead = False

while not dead:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            dead = True
    player(x, y)
    pygame.Surface.fill(white)
    pygame.display.flip()
    clock.tick(60)
pygame.quit()
quit()

screen.fill(white)

i expect it to open to a white screen. maybe be able to see the pic. but all it does is tells me blit needs an argument. which i thought i gave it an argument when i told it the place to find the picture and it x and y coords

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

2 Answers2

0

It seems like you are trying to learn how to write code in Python and use the Pygame library at the same time. I don't recommend this; learn some more code-writing fundamentals first. But I will try to help with your confusion:

but all it does is

Right; there is an error during the start-up, so it can't show you anything yet.

which i thought i gave it an argument when i told it the place to find the picture and it x and y coords

The key is in the words descriptor and object. pygame.Surface is a class, and blit is a method of that class, but you are trying to use it as if it were a plain function.

You need to create an instance of the class, and use its methods. You already have such an instance: it is called screen, and it was created by pygame.display.set_mode. So now we can, for example, screen.blit(playerimg, (x, y)).

I only put it up here because it told me "x" wasent defined in "def player(x, y):"

This happened because you defined player to accept x and y values, but in the code outside the function, you tried to call it using the x and y from outside the function - which didn't exist. Please study the example carefully:

def player(x, y):
    # We don't need to define x and y inside, because they will be given to us
    # if we do assign some values, it replaces what we were passed in.
    # This is bad, because the point is to let the outside code decide.
    print(f"I was passed in: x = {x}, y = {y}")

x = 1
y = 2
# this requires the above; it has nothing to do with x and y inside the function.
player(x, y)

# but we can use different names:
a = 1
b = 2
player(a, b)

# or no names at all:
player(1, 2)

(Again; learn more code-writing fundamentals first; then it will be obvious to you when something like this happens and why, and you will be able to fix it properly, the first time, and understand why the proper fix is proper.)

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Thanks for the input. I always run before i walk. lol. this is like my first week learning i appreciate the constructive feedback i wish i could upvote you but im not high enough rank yet but thanks!!!!!!!!!!! – Brandon Coffman Oct 08 '19 at 18:28
0

pygame.Surface.blit is a Method and the 2nd parameter (dest) is a position tuple with 2 components, so it has to be:

screen.blit(source = playerimg, dest = (x, y))

The same applies to pygame.Surface.fill

screen.fill(white)

Note, the operations .blit() and .fill() operator on an pygame.Surface object. If you want to fill the display or to blit something on the display, then you've to use the Surface which is associated the to the display. In your case this is screen;

screen = pygame.display.set_mode([screen_width, screen_height])

Alternatively you can do:

pygame.Surface.blit(screen, playerimg, (x, y))

respectively

pygame.Surface.fill(screen, white)

Furthermore you've to draw player, after the display was cleared and before the display is updated:

screen.fill(white)
player(x, y)
pygame.display.flip()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • okay so when using ''' pygame.Surface.fill() ''' i need a tuple to describe where it is going to be located but if i just use ''' screen.fill() ''' it should work fine? – Brandon Coffman Oct 08 '19 at 18:20
  • @BrandonCoffman You've to specify which surface has to be filled. In your case `screen` is the Surface object which is associated to the window. – Rabbid76 Oct 08 '19 at 18:23
  • OHHHHHHHHHHHHHHH thank you i see what i did wrong. Thanks for the input – Brandon Coffman Oct 08 '19 at 18:26
  • @BrandonCoffman Thank you. By the way, the `payer` has to be draw after the display is cleared. See the last section of the answer. – Rabbid76 Oct 08 '19 at 18:31