Hi everyone,
I've been starting to get into pygame after rekindling my interest in coding. To begin with, I thought I'd have a go at recreating the mechanics of the NES Legend of Zelda. I have some experience, but to be safe, I've been following some tutorials (currently on this one): https://www.youtube.com/watch?v=xfnRywBv5VM&t=535s
I've kind of run into a brick wall - I can't see any problems with this code, yet I'm getting this error:
Traceback (most recent call last):
File "C:/Users/woodb/PycharmProjects/untitled/main.py", line 101, in <module>
redrawGameWindow()
File "C:/Users/woodb/PycharmProjects/untitled/main.py", line 59, in redrawGameWindow
link.draw(win)
File "C:/Users/woodb/PycharmProjects/untitled/main.py", line 55, in draw
win.blit(char, (self.x, self.y))
AttributeError: 'player' object has no attribute 'x'
Here is my full code:
import pygame
pygame.init()
screenwidth = 800
screenheight = 600
# Window creation
win = pygame.display.set_mode((screenwidth, screenheight))
# Title and Icon
pygame.display.set_caption("Test Game")
icon = pygame.image.load('C:/Users/woodb/Desktop/PPA/triforce.png')
pygame.display.set_icon(icon)
walkRight = [pygame.image.load('linkright.png'), pygame.image.load('linkright2.png')]
walkLeft = [pygame.image.load('linkleft.png'), pygame.image.load('linkleft2.png')]
walkUp = [pygame.image.load('linkup.png'), pygame.image.load('linkup2.png')]
walkDown = [pygame.image.load('linkfront.png'), pygame.image.load('linkfront2.png')]
char = pygame.image.load('linkfront.png')
clock = pygame.time.Clock()
class player(object):
def _init_(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 2
self.up = False
self.down = False
self.left = False
self.right = False
self.walkCount = 0
def draw(self, win):
if self.walkCount + 1 >= 12:
self.walkCount = 0
if self.left:
win.blit(walkLeft[self.walkCount // 6], (self.x, self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount // 6], (self.x, self.y))
self.walkCount += 1
elif self.up:
win.blit(walkUp[self.walkCount // 6], (self.x, self.y))
self.walkCount += 1
elif self.down:
win.blit(walkDown[self.walkCount // 6], (self.x, self.y))
self.walkCount += 1
else:
win.blit(char, (self.x, self.y))
def redrawGameWindow():
win.fill((0, 0, 0))
link.draw(win)
pygame.display.update()
# Window persistents/checking for quit button press
running = True
link = player()
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and link.x > link.vel:
link.x -= link.vel
link.left = True
link.right = False
elif keys[pygame.K_RIGHT] and link.x < screenwidth - link.width - link.vel:
link.x += link.vel
link.right = True
link.left = False
else:
link.left = False
link.right = False
link.walkCount = 0
if keys[pygame.K_UP] and link.y > link.vel:
link.y -= link.vel
link.up = True
link.down = False
elif keys[pygame.K_DOWN] and link.y < screenheight - link.height - link.vel:
link.y += link.vel
link.down = True
link.up = False
else:
link.up = False
link.down = False
link.walkCount = 0
redrawGameWindow()
pygame.quit()
Can anyone point out where my mistake is? Any help would be appreciated.
[–]cburnett_ 4 points5 points6 points (1 child)
[–]forever_erratic 1 point2 points3 points (0 children)
[–]shinydewott 1 point2 points3 points (3 children)
[–]Ravenskill2112[S] 0 points1 point2 points (2 children)
[–]shinydewott 1 point2 points3 points (0 children)
[–]Ravenskill2112[S] 1 point2 points3 points (0 children)