Reputation: 1
The following code gives me this error and I am confused. Sorry If I am asking questions wrong, I'm new here but I think this corresponds to the guidelines. The image will move across the background as soon as I release the key it gives me this error and crashes.
Traceback (most recent call last):
File "C:\Users\Samga_000\Desktop\game\game.py", line 89, in <module>
rungame()
File "C:\Users\Samga_000\Desktop\game\game.py", line 79, in rungame
p.still()
TypeError: 'int' object is not callable
import pygame, sys
from pygame.locals import *
pygame.init()
WIDTH = 400
HEIGHT = 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
bif = "grass.png"
background = pygame.image.load(bif)
pygame.display.set_caption('RPG')
def drawEntitys():
frispri.update()
frispri.draw(screen)
def drawScreen():
screen.blit(background, (0,0))
class Player(pygame.sprite.Sprite):
image = pygame.image.load("misha.png")
image = image.convert_alpha()
def __init__(self):
pygame.sprite.Sprite.__init__(self, self.groups)
self.pos = (20, 20)
self.image = Player.image
self.rect = self.image.get_rect()
self.movepos = [0,0]
self.area = screen.get_rect()
self.still = True
def update(self):
newpos = self.rect.move(self.movepos)
if self.area.contains(newpos):
self.rect = newpos
pygame.event.pump()
def move(self, x, y):
self.still = 0
if self.still == 0:
self.movepos[0] = self.movepos[0] - x
self.movepos[1] = self.movepos[1] - y
else:
pass
def still(self):
self.still = 1
frispri = pygame.sprite.Group()
Player.groups = frispri
p = Player()
def rungame():
while True:
drawScreen()
drawEntitys()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == KEYDOWN:
if event.key == K_UP:
p.move(0,1)
elif event.key == K_DOWN:
p.move(0,-1)
elif event.key == K_LEFT:
p.move(1,0)
elif event.key == K_RIGHT:
p.move(-1,0)
elif event.type == KEYUP:
if event.key == K_UP:
p.still()
elif event.key == K_DOWN:
p.still()
elif event.key == K_LEFT:
p.still()
elif event.key == K_RIGHT:
p.still()
flipgame()
def flipgame():
pygame.display.flip()
rungame()
Upvotes: 0
Views: 1272
Reputation: 193
This is essentially the same problem as discussed in this question
Your problem is that you have an instance variable, still
, with the same name as your method. When you do p.still(), Python interprets the p.still as trying to access the variable not the method, and then trying to 'call' that variable.
The solution is to rename the method or the variable. To get your code to work you should only need to change one of the two. But good programming practice might say you should change both.
Since the variable is a flag that indicates if the player is still or not, you might change it's name to be is_still
. This is a slightly more descriptive name, that makes it clear what the variable means.
For the method you could rename it to set_still
or mark_still
to indicate that it is changing the status of the player. Or combined with the above change to the variable name you could make the method name set_is_still
, to make it clear exactly what variable you are setting.
Upvotes: 1
Reputation: 67822
Player.still
is a boolean, assigned here:
class Player(pygame.sprite.Sprite):
def __init__(self):
# ...
self.still = True
You can't call a boolean, since it isn't a function, as you attempt here:
p = Player()
# ...
p.still()
Upvotes: 2