Trying to make my own game in pygame, and I have encountered an error, that I just cant seem to get around. I incorporated a class into the program, called Player. This class is supposed to hold information regarding the player, (image, input, etc). However when I try to call the class and draw it onto the screen. I get the error, ''Player has no attribute 'draw'''. Without using a class I can just blit it onto the screen, now something is different.
import pygame
from sys import exit
pygame.init()
#player info
x = 450
y = 840
vel = 5
#window info
screen_w = 800
screen_h = 600
# player movement etc
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
player_surf = pygame.image.load('game graphics/player_stand.png').convert_alpha()
#scaled player image
player_surf = pygame.transform.scale(player_surf,(500,500))
self.player_rect = player_surf.get_rect(midbottom = (x,y))
def player_input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_d] and self.rect.bottomright <= screen_h or self.rect.bottomleft >=screen_w:
x = vel
elif keys[pygame.K_a] and self.rect.bottomright <= screen_h or self.rect.bottomleft >=screen_w:
x -= vel
def update(self):
self.player_input()
# def draw(self):
# pass
#window stuff
screen = pygame.display.set_mode((screen_w,screen_h))
pygame.display.set_caption('Cave In')
#clock
clock = pygame.time.Clock()
#background
cave_surf = pygame.image.load('game graphics/background.png').convert()
cave_surf = pygame.transform.scale(cave_surf, (screen_w,screen_h)).convert()
#game
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.fill((0,0,0))
screen.blit(cave_surf,(0,0))
#here is the problem :/
Player.draw(screen)
Player.update()
pygame.display.update()
My question is, using the Player class, how do I get it to appear on the screen?
Thanks.
EDIT ---------------------->
After watching youtube videos, looking a other programs, and experimenting. I still can not get the character on screen. I have updated my code, and I now have a different error message at the moment. Here is the updated program.
import pygame, random
from sys import exit
pygame.init()
#player info
x = 450
y = 840
vel = 5
health = 5
#window info
screen_w = 800
screen_h = 600
# player movement etc
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.player_surf = pygame.image.load('game graphics/player_stand.png').convert_alpha()
#scaled player image
self.player_surf = pygame.transform.scale(self.player_surf,(500,500))
self.rect = self.player_surf.get_rect(midbottom = (x,y))
self.x_speed = vel
self.health = health
self.direction = 1 #1 = right 0 = left
def player_input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
x = vel
elif keys[pygame.K_a]:
x -= vel
def update(self):
self.player_input()
# drawing the player on screen?
def draw_hero(self,screen,hero):
current_hero_sprite =
pygame.transform.flip(current_hero_sprite, True, False)
screen.blit(current_hero_sprite,hero.rect)
pygame.display.update(screen)
#window stuff
screen = pygame.display.set_mode((screen_w,screen_h))
pygame.display.set_caption('Cave In')
pygame.display.set_icon(pygame.image.load('game
graphics/player_stand.png'))
#clock
clock = pygame.time.Clock()
#background
cave_surf = pygame.image.load('game
graphics/background.png').convert()
cave_surf = pygame.transform.scale(cave_surf,
(screen_w,screen_h)).convert()
#game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.fill((0,0,0))
screen.blit(cave_surf,(0,0))
screen.blit(Player)
Player.update()
pygame.display.update()
My current error message is, ''line 75 TypeError: argument 1 must be pygame.Surface, not type'' There is similar problems on the on stack overflow/Quora etc. However none seem to really solve the problem?
Probably just a noob mistake.
CodePudding user response:
A class is a template to create (instantiate) an object. First instantiate a new Player object, then you can call methods on it.
# At the beginning, instantiate the player object as defined in the class.
player = Player()
#...
player.draw(screen)
player.update()
CodePudding user response:
SOLVED -->
screen.blit(player.player_surf,player.rect)
Thats it...