# creating the player class
class Player(pygame.sprite.Sprite):
COLOR = (255, 0, 0)
def __init__(self, x, y, width, height):
self.rect = pygame.Rect(x, y, width, height)
self.x_vel = 0
self.y_vel = 0
self.mask = None
self.direction = "left"
self.animation_count = 0
# movement features
def move(self, dx, dy):
self.rect.x += dx
self.rect.y += dy
#moving left
def move_left(self, vel):
self.x_vel = -vel
if self.direction != "left":
self.direction = "left"
self.animation_count = 0
#moving right
def move_right(self, vel):
self.x_vel = vel
if self.direction != "right":
self.direction = "right"
self.animation_count = 0
#loop and another draw
def loop(self, fps):
self.move(self.x_vel, self.y_vel)
def draw(self, win):
pygame.draw.rect(win, self.COLOR, self.rect)
#background info
def get_background(name):
image = pygame.image.load(join('Assets', 'Backgrounds', name))
_, _, width, height = image.get_rect()
tiles = []
for i in range(WIDTH // width + 1):
for j in range(HEIGHT // height + 1):
pos = (i * width, j * height)
tiles.append(pos)
return tiles, image
#drawing function to draw out the background, player, window etc
def draw(window, background, bg_image, player):
for tile in background:
window.blit(bg_image, tile)
#issue is here this is where i declared it
player.draw(window)
pygame.display.update()
def main(window):
clock = pygame.time.Clock()
background, bg_image = get_background("blue.JPEG")
player = Player(100, 100, 50, 50)
This is only some part of the code not mentioning the background, but i wanted a box or shape to be present but its not happening it keeps saying, attribute error. Specifically, ‘Player’ object has no attribute ‘draw’. I missed out the code for exiting out of pygames because it doesnt let me add mostly code.
New contributor
DragPVP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1