AttributeError: 'Game' object has no attribute 'running' [duplicate] - python

When calling a class with the parameters goblin = enemey(100, 410, 64, 64, 450) TypeError: object() takes no parameters . All the code and calls look good, an I cant find the issue.
class enemey(object):
walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'),
pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'),
pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png'),
pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]
walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'),
pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'),
pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png'),
pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]
def __int__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.walkCount = 0
self.vel = 3
self.path = [self.x, self.end] # change variables <- to random for random moving? Make sure to try!
def draw(self,gameDisplay):
self.move()
if self.walkCount + 1 >= 33:
self.walkCount = 0
if self.vel > 0:
gameDisplay.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
else: # change to elif to add 3d movement
gameDisplay.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
else:
if self.x - self.vel > self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0

The issue is that your constructor is misspelled - it should be __init__, not __int__. As a result it uses default constructor which accepts no arguments.

Related

AttributeError: 'Cat' object has no attribute 'image' [duplicate]

When calling a class with the parameters goblin = enemey(100, 410, 64, 64, 450) TypeError: object() takes no parameters . All the code and calls look good, an I cant find the issue.
class enemey(object):
walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'),
pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'),
pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png'),
pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]
walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'),
pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'),
pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png'),
pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]
def __int__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.walkCount = 0
self.vel = 3
self.path = [self.x, self.end] # change variables <- to random for random moving? Make sure to try!
def draw(self,gameDisplay):
self.move()
if self.walkCount + 1 >= 33:
self.walkCount = 0
if self.vel > 0:
gameDisplay.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
else: # change to elif to add 3d movement
gameDisplay.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
else:
if self.x - self.vel > self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
The issue is that your constructor is misspelled - it should be __init__, not __int__. As a result it uses default constructor which accepts no arguments.

Why do I get an error when I blit an image

I'm Trying to put an enemy into a game That I am making. but when I run the project the python reports a 'TypeError'
File "C:\Users\Zee_S\OneDrive\Desktop\python projects\lil Shooter\Player\Lil Shooter.py", line 153, in draw
screen.blit(ZwalkRight[self.walkcount//3] (self.x, self.y))
TypeError: 'pygame.Surface' object is not callable
[Finished in 1.4s]
but I looked at my code an it looked fine
class Enemy(object):
ZwalkRight = [pygame.image.load('ZR1.png'), pygame.image.load('ZR2.png'),]
ZwalkLeft = [pygame.image.load('ZL1.png'), pygame.image.load('ZL2.png'),]
def __init__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.path = [self.x, self.end]
self.walkcount = 0
self.velo = 3
def draw(self,win):
self.move()
if self.walkcount + 1 <= 33:
self.walkcount = 0
if self.velo > 0:
screen.blit(self.ZwalkRight[self.walkcount//3] (self.x, self.y))
walkcount += 1
else:
win.blit(self.ZwalkLeft[self.walkcount //3] (self.x, self.y))
walkcount += 1
def move(self):
if self.velo > 0:
if self.x + self.velo < self.path[1]:
self.x += self.velo
else:
self.velo = self.velo * -1
self.walkcount = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.velo
else:
self.velo = self.velo * -1
self.walkcount = 0
And the problem cant be that I have forgotten to draw the image because I have here
def redrawGameWindow():
screen.blit(bg, (0,-100))
man.draw(screen)
zombie.draw(screen)
for bullet in bullets:
bullet.draw(screen)
pygame.display.update()
pygame.display.update()
man = Player(300, 508, 64, 64)
zombie = Enemy(100, 508, 64, 64, 450)
bullets = []
run = True
while run:
[...]
can someone fix or even explain why I am getting this error
You’re missing a comma after the closing square bracket in your call to blit:
screen.blit(self.ZwalkRight[self.walkcount//3] (self.x, self.y))
screen.blit(self.ZwalkRight[self.walkcount//3], (self.x, self.y))

Why does Pygame crashing from list index error?

I am working on a game in Pygame and the window crashes a few seconds into execution. I traced it back to the list I used for the "enemy" sprite walking animation. I still cannot find what it is that it is having trouble within my list. I did the same exact thing with my player walking animations.
here is the error it threw:
Traceback (most recent call last):
File "/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic >platformer/basic_platformer.py", line 188, in <module>
redrawGameWindow()
File "/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic >platformer/basic_platformer.py", line 111, in redrawGameWindow
goblin.draw(ben.window)
File "/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic >platformer/basic_platformer.py", line 85, in draw
window.blit(self.walkRight[self.walkCount //3], (self.x, self.y))
IndexError: list index out of range
I've commented out the player and enemy classes and lists for walking; "walkRight, "walkLeft".
import sys
import pygame
pygame.init()
pygame.display.set_caption("Ben Meitzen")
#player walking lists
walkRight = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/sprites/tile%s.png' % frame) for frame in range(17, 24)]
walkLeft = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/sprites/tile%s.png' % frame) for frame in range(9, 16)]
bg = pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/skybackground.png')
char = pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/sprites/tile1.png')
clock = pygame.time.Clock()
#player class
class player(object):
def __init__(self,x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.velocity = 11
self.isJump = False
self.jumpCount = 7
self.right = False
self.left = False
self.walkCount = 0
self.window = pygame.display.set_mode((860,538))
self.standing = True
def draw(self, window):
if self.walkCount + 1 >= 8:
self.walkCount = 0
if not (self.standing):
if self.left:
self.window.blit(walkLeft[self.walkCount//1], (self.x, self.y))
self.walkCount += 1
elif self.right:
self.window.blit(walkRight[self.walkCount//1], (self.x, self.y))
self.walkCount += 1
else:
if self.right:
window.blit(walkRight[0], (self.x, self.y))
else:
window.blit(walkLeft[0], (self.x, self.y))
class projectile(object):
def __init__(self, x, y, radius, color, facing):
self.x = x
self.y = y
self.color = color
self.facing = facing
self.velocity = 40 * facing
self.radius = radius
def draw(self, window):
pygame.draw.circle(window, self.color, (self.x, self.y), self.radius)
#enemy class
class enemy(object):
#enemy walking lists
walkRight = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/enemy sprites/enemy%s.png' % frame) for frame in range(17, 24)]
walkLeft = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/enemy sprites/enemy%s.png' % frame) for frame in range(9, 16)]
def __init__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.walkCount = 0
self.velocity = 3
self.path = [self.x, self.end]
def draw(self, window):
self.move()
if self.walkCount + 1 >= 8:
self.walkcount = 0
if self.velocity > 0:
window.blit(self.walkRight[self.walkCount //3], (self.x, self.y))
self.walkCount += 1
else:
window.blit(self.walkLeft[self.walkCount //3], (self.x, self.y))
self.walkCount += 1
def move(self):
if self.velocity > 0:
if self.x + self.velocity < self.path[1]:
self.x += self.velocity
else:
self.velocity = self.velocity * -1
self.walkcount = 0
else:
if self.x - self.velocity > self.path[0]:
self.x += self.velocity
else:
self.velocity = self.velocity * -1
self.walkcount = 0
#game window-------------------------------------------------------------------------------------------------------------------------------------------------
def redrawGameWindow():
ben.window.blit(bg,(0,0))
ben.draw(ben.window)
goblin.draw(ben.window)
for bullet in bullets:
bullet.draw(ben.window)
pygame.display.update()
##main loop##------------------------------------------------------------------------------------------------------------------------------------------------
goblin = enemy(100, 460, 70, 70, 700)
ben = player(30, 460, 64, 64)
bullets = []
run = True
while run:
clock.tick(8)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bullet in bullets:
if bullet.x < 860 and bullet.x > 0:
bullet.x += bullet.velocity
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
if ben.left:
facing = -1
else:
facing = 1
if len(bullets) < 5:
bullets.append(projectile(round(ben.x + ben.width //2), round(ben.y + ben.height -30 //2), 6, (0,0,0), facing))
if keys[pygame.K_LEFT] and ben.x > ben.velocity:
ben.x -= ben.velocity
ben.left = True
ben.right = False
ben.standing = False
elif keys[pygame.K_RIGHT] and ben.x < 860 - ben.width - ben.velocity:
ben.x += ben.velocity
ben.right = True
ben.left = False
ben.standing = False
else:
ben.standing = True
ben.walkCount = 0
if not(ben.isJump):
if keys[pygame.K_UP]:
ben.isJump = True
ben.right = False
ben.left = False
else:
if ben.jumpCount >= -7:
neg = 0.5
if ben.jumpCount < 0:
neg = -0.5
ben.y -= (ben.jumpCount **2) * 1.5 * neg
ben.jumpCount -= 2
else:
ben.isJump = False
ben.jumpCount = 7
ben.velocity = 11
redrawGameWindow()
#-----------------------------------------------------------------------------------------------
pygame.quit()
In this part of the code:
if self.walkCount + 1 >= 8:
self.walkcount = 0
self.walkCount is written as self.walkcount (lowercase c). So, the actual walkCount is not being reset to zero, thus the list index out of range error.

pygame images not loading?

How come my Goku sprites aren't loading my left and right image appear correct but my others aren't playing in motion? the mian thing am trying to accomplish is for my character images to work correctly in motion
below is the code with a link to the full code description:
https://pastebin.com/umMJHNQj
import pygame
pygame.init()#We always need to initialize our pygame IN EVERY PROJECT/FILE
win = pygame.display.set_mode((500, 480))# Here win is representing "window" for our screen which we have set at 500 by 480
pygame.display.set_caption("First Game")#We are giving our Window/Screen a name
walkRight = [pygame.image.load('image/gokuR0.png'), pygame.image.load('image/gokutest2.png'), pygame.image.load('image/gokuR2.png')]
walkLeft = [pygame.image.load('image/gokuL0.png'), pygame.image.load('image/gokutest.png'), pygame.image.load('image/gokuL2.png')]
bg = pygame.image.load('image/bg.jpg')
char = pygame.image.load('image/goku sprite - standing.png')
clock = pygame.time.Clock()
class player():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.isJump = False
self.left = False
self.right = False
self.walkCount = 0
self.jumpCount = 10
self.standing = False
Here is where i have tried to alter the code but yet no results.
def draw(self, win):
if self.walkCount + 1 >= 8:
self.walkCount = 0
if not self.standing:
if self.left:
win.blit(walkLeft[self.walkCount // 100], (self.x, self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount // 100], (self.x, self.y))
self.walkCount += 1
else:
if self.right:
win.blit(walkRight[0], (self.x, self.y))
else:
win.blit(walkLeft[0], (self.x, self.y))
pygame.display.flip()
print(walkLeft)
print(walkRight)
class projectile():
def __init__(self, x, y, radius, color, facing):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.facing = facing
self.vel = 8 * facing
def draw(self, win):
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)
All these images are working correctly yet can't seem to get my main characters images to work in properly in motion
class enemy():
walkRight = [pygame.image.load("image/R1E.png"), pygame.image.load("image/R2E.png"), pygame.image.load("image/R3E.png"), pygame.image.load("image/R4E.png"), pygame.image.load("image/R5E.png"), pygame.image.load("image/R6E.png"), pygame.image.load("image/R7E.png"), pygame.image.load("image/R8E.png"), pygame.image.load("image/R9E.png"), pygame.image.load("image/R10E.png"), pygame.image.load("image/R11E.png")]
walkLeft = [pygame.image.load("image/L1E.png"), pygame.image.load("image/L2E.png"), pygame.image.load("image/L3E.png"), pygame.image.load("image/L4E.png"), pygame.image.load("image/L5E.png"), pygame.image.load("image/L6E.png"), pygame.image.load("image/L7E.png"), pygame.image.load("image/L8E.png"), pygame.image.load("image/L9E.png"), pygame.image.load("image/L10E.png"), pygame.image.load("image/L11E.png")]
def __init__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.path = [self.x, self.end]
self.walkcount = 0
self.vel = 3
def draw(self,win):
self.move()
if self.walkcount + 1 >= 33:
self.walkcount = 0
if self.vel > 0:
win.blit(self.walkRight[self.walkcount // 3], (self.x, self.y))
self.walkcount += 1
else:
win.blit(self.walkLeft[self.walkcount // 3], (self.x, self.y))
self.walkcount += 1
print(walkLeft)
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkcount = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkcount = 0
def redrawGameWindow():
win.blit(bg, (0, 0))
goblin.draw(win)
man.draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
# mainloop
man = player(200, 400, 85, 85)
goblin = enemy(100, 400, 64, 64, 450)
bullets = []
run = True
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bullet in bullets:
if bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
if man.left:
facing = -1
else:
facing = 1
if len(bullets) < 5:
bullets.append(
projectile(round(man.x + man.width // 2), round(man.y + man.height // 2), 6, (0, 0, 0), facing))
if keys[pygame.K_LEFT] and man.x > man.vel:
man.x -= man.vel
man.left = True
man.right = False
man.standing = False
elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
man.x += man.vel
man.right = True
man.left = False
man.standing = False
else:
man.standing = True
man.walkCount = 0
if not (man.isJump):
if keys[pygame.K_UP]:
man.isJump = True
man.right = False
man.left = False
man.walkCount = 0
else:
if man.jumpCount >= -10:
neg = 1
if man.jumpCount < 0:
neg = -1
man.y -= (man.jumpCount ** 2) * 0.5 * neg
man.jumpCount -= 1
else:
man.isJump = False
man.jumpCount = 10
redrawGameWindow()
pygame.quit()
The issue is that self.walkCount is (integral) divided by 100:
win.blit(walkLeft[self.walkCount // 100], (self.x, self.y))
Note self.walkCount will never become grater than 7, because of:
if self.walkCount + 1 >= 8:
self.walkCount = 0
So the result of self.walkCount // 100 is always 0.
Anyway this code makes no sense, since walkLeft and walkRight are lists containing 3 images. I recommend to do the following:
def draw(self, win):
walkfps = 10
if self.standing:
self.walkCount = 0
if self.right:
win.blit(walkRight[0], (self.x, self.y))
else:
win.blit(walkLeft[0], (self.x, self.y))
elif self.left:
if self.walkCount // walkfps >= len(walkLeft):
self.walkCount = 0
win.blit(walkLeft[self.walkCount // walkfps], (self.x, self.y))
self.walkCount += 1
elif self.right:
if self.walkCount // walkfps >= len(walkRight):
self.walkCount = 0
win.blit(walkRight[self.walkCount // walkfps], (self.x, self.y))
self.walkCount += 1
If the images change to rapidly, then increase walkfps.

Crash when trying to create parameters for a class

When calling a class with the parameters goblin = enemey(100, 410, 64, 64, 450) TypeError: object() takes no parameters . All the code and calls look good, an I cant find the issue.
class enemey(object):
walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'),
pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'),
pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png'),
pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]
walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'),
pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'),
pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png'),
pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]
def __int__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.walkCount = 0
self.vel = 3
self.path = [self.x, self.end] # change variables <- to random for random moving? Make sure to try!
def draw(self,gameDisplay):
self.move()
if self.walkCount + 1 >= 33:
self.walkCount = 0
if self.vel > 0:
gameDisplay.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
else: # change to elif to add 3d movement
gameDisplay.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
else:
if self.x - self.vel > self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
The issue is that your constructor is misspelled - it should be __init__, not __int__. As a result it uses default constructor which accepts no arguments.

Categories