I have been trying to design a snake game in python but after having implementing the part of the code that make the body of the snake move (not the head) I get this error:
TypeError: 'str' object is not callable
I looked online and all I found was just saying that I should not call my variable 'str' and I didn't so I am kind of confused. This is the class:
class snake(object):
def __init__(self,x,y,h_dir,b_dir):
self.X=x
self.Y=y
self.h_dir=h_dir
self.b_dir=b_dir
def direction(self):
global head_direction
self.direction=head_direction
if(self.X==turn_X and self.Y==turn_Y):
self.direction=head_direction
return self.direction
Here is where the error occur:
for i in range(1,snakeLength):
if(body[i].direction()=='UP'):
body[i].Y-=20
if(body[i].direction()=='RIGHT'):
body[i].X =20
if(body[i].direction()=='DOWN'):
body[i].Y =20
if(body[i].direction()=='LEFT'):
body[i].X-=20
pygame.draw.rect(window, grey, (body[i].X, body[i].Y, 20, 20))
and here is the whole code:
# Libraries
import pygame
from time import*
running=True
while running:
# Initialisation
pygame.init()
# Quit programm
for event in pygame.event.get():
if(event.type==pygame.QUIT):
running=False
# Class
class snake(object):
def __init__(self,x,y,h_dir,b_dir):
self.X=x
self.Y=y
self.h_dir=h_dir
self.b_dir=b_dir
def direction(self):
global head_direction
self.direction=head_direction
if(self.X==turn_X and self.Y==turn_Y):
self.direction=head_direction
return self.direction
# Functions
def displayScore():
score_font=pygame.font.Font('/Users/claudiabergeron/Documents/Python/Tutoriels/Video games/Pygame/Snake/G7StarForceTtf-OEg6.ttf',20)
score_surface=score_font.render('SCORE ' score, True, white)
window.blit(score_surface, (0,500))
pygame.display.update()
def gameOver():
window.fill((green))
global alive
gameOver_font=pygame.font.Font('/Users/claudiabergeron/Documents/Python/Tutoriels/Video games/Pygame/Snake/G7StarForceTtf-OEg6.ttf',50)
gameOver_surface=gameOver_font.render('GAME OVER', True, white)
gameOver_rect=gameOver_surface.get_rect(center=(window_X/2, window_Y/2))
score_font=pygame.font.Font('/Users/claudiabergeron/Documents/Python/Tutoriels/Video games/Pygame/Snake/G7StarForceTtf-OEg6.ttf',20)
score_surface=score_font.render('YOUR FINAL SCORE IS ' score, True, white)
window.blit(score_surface, (0,500))
window.blit(gameOver_surface, (gameOver_rect))
pygame.display.update()
sleep(3)
alive=False
# Window size
window_X=500
window_Y=520
# Colors
green=pygame.Color(153,255,153)
black=pygame.Color(0,0,0)
grey=pygame.Color(32,32,32)
red=pygame.Color(255,0,0)
white=pygame.Color(255,255,255)
# Window
pygame.display.set_caption("Snake")
window=pygame.display.set_mode((window_X,window_Y))
icon=pygame.image.load('/Users/claudiabergeron/Documents/Python/Tutoriels/Video games/Pygame/Snake/anaconda.png')
pygame.display.set_icon(icon)
# FPS
fps=pygame.time.Clock()
# Head
head_X=240
head_Y=240
head_direction='RIGHT'
head_no_direction='LEFT'
# Tail
body=['head']
snakeLength=5
turn_X=-1
turn_Y=-1
# Snake
snake_speed=5
# Score
score=0
for i in range(0,snakeLength):
body.append(i)
for i in range(snakeLength):
body_X=head_X-(20*i)
body_Y=head_Y
body_direction=head_direction
body[i]=snake(body_X, body_Y, head_direction, body_direction)
alive=True
while alive:
window.fill((green))
# Events
for event in pygame.event.get():
if(event.type==pygame.QUIT):
running=False
alive=False
# Keystrokes
if(event.type==pygame.KEYDOWN):
turn_X=head_X
turn_Y=head_Y
if(event.key==pygame.K_UP):
if(head_no_direction!='UP'):
head_direction='UP'
head_no_direction='DOWN'
elif(event.key==pygame.K_RIGHT):
if(head_no_direction!='RIGHT'):
head_direction='RIGHT'
head_no_direction='LEFT'
elif(event.key==pygame.K_DOWN):
if(head_no_direction!='DOWN'):
head_direction='DOWN'
head_no_direction='UP'
elif(event.key==pygame.K_LEFT):
if(head_no_direction!='LEFT'):
head_direction='LEFT'
head_no_direction='RIGHT'
# Head
pygame.draw.rect(window, black, (head_X, head_Y, 20, 20))
if(head_direction=='UP'):
head_Y-=20
if(head_direction=='RIGHT'):
head_X =20
if(head_direction=='DOWN'):
head_Y =20
if(head_direction=='LEFT'):
head_X-=20
# Body
for i in range(1,snakeLength):
if(body[i].direction()=='UP'):
body[i].Y-=20
if(body[i].direction()=='RIGHT'):
body[i].X =20
if(body[i].direction()=='DOWN'):
body[i].Y =20
if(body[i].direction()=='LEFT'):
body[i].X-=20
pygame.draw.rect(window, grey, (body[i].X, body[i].Y, 20, 20))
# Updates
pygame.draw.rect(window, red, (0, 0, 500, 500),3)
displayScore()
pygame.display.update()
fps.tick(snake_speed)
# Game over
if(head_X==500 or head_X==-20 or head_Y==500 or head_Y==-20):
gameOver()
I need help.
CodePudding user response:
This part:
for i in range(1,snakeLength):
if(body[i].direction()=='UP'):
body[i].Y-=20
if(body[i].direction()=='RIGHT'):
body[i].X =20
if(body[i].direction()=='DOWN'):
body[i].Y =20
if(body[i].direction()=='LEFT'):
body[i].X-=20
pygame.draw.rect(window, grey, (body[i].X, body[i].Y, 20, 20))
Should look like this:
for i in range(1,snakeLength):
if(body[i].direction=='UP'):
body[i].Y-=20
if(body[i].direction=='RIGHT'):
body[i].X =20
if(body[i].direction=='DOWN'):
body[i].Y =20
if(body[i].direction=='LEFT'):
body[i].X-=20
pygame.draw.rect(window, grey, (body[i].X, body[i].Y, 20, 20))
Your code has a few other issues that popped up, but this worked for me to resolve your issue.
If you are still having trouble, here is my completed code that ran without error. Please note, I replaced your icon and font filenames so that I could use resources available on my machine.
(spoilers!)# Libraries
import pygame
from time import*
running=True
while running:
# Initialisation
pygame.init()
# Quit programm
for event in pygame.event.get():
if(event.type==pygame.QUIT):
running=False
# Class
class snake(object):
def __init__(self,x,y,h_dir,b_dir):
self.X=x
self.Y=y
self.h_dir=h_dir
self.b_dir=b_dir
def direction(self):
global head_direction
self.direction=head_direction
if(self.X==turn_X and self.Y==turn_Y):
self.direction=head_direction
return self.direction
# Functions
def displayScore():
score_font=pygame.font.Font('Debrosee-ALPnL.ttf',20)
score_surface=score_font.render('SCORE ' str(score), True, white)
window.blit(score_surface, (0,500))
pygame.display.update()
def gameOver():
window.fill((green))
global alive
gameOver_font=pygame.font.Font('Debrosee-ALPnL.ttf',50)
gameOver_surface=gameOver_font.render('GAME OVER', True, white)
gameOver_rect=gameOver_surface.get_rect(center=(window_X/2, window_Y/2))
score_font=pygame.font.Font('Debrosee-ALPnL.ttf',20)
score_surface=score_font.render('YOUR FINAL SCORE IS ' str(score), True, white)
window.blit(score_surface, (0,500))
window.blit(gameOver_surface, (gameOver_rect))
pygame.display.update()
sleep(3)
alive=False
# Window size
window_X=500
window_Y=520
# Colors
green=pygame.Color(153,255,153)
black=pygame.Color(0,0,0)
grey=pygame.Color(32,32,32)
red=pygame.Color(255,0,0)
white=pygame.Color(255,255,255)
# Window
pygame.display.set_caption("Snake")
window=pygame.display.set_mode((window_X,window_Y))
icon=pygame.image.load('anaconda.png')
pygame.display.set_icon(icon)
# FPS
fps=pygame.time.Clock()
# Head
head_X=240
head_Y=240
head_direction='RIGHT'
head_no_direction='LEFT'
# Tail
body=['head']
snakeLength=5
turn_X=-1
turn_Y=-1
# Snake
snake_speed=5
# Score
score=0
for i in range(0,snakeLength):
body.append(i)
for i in range(snakeLength):
body_X=head_X-(20*i)
body_Y=head_Y
body_direction=head_direction
body[i]=snake(body_X, body_Y, head_direction, body_direction)
alive=True
while alive:
window.fill((green))
# Events
for event in pygame.event.get():
if(event.type==pygame.QUIT):
running=False
alive=False
# Keystrokes
if(event.type==pygame.KEYDOWN):
turn_X=head_X
turn_Y=head_Y
if(event.key==pygame.K_UP):
if(head_no_direction!='UP'):
head_direction='UP'
head_no_direction='DOWN'
elif(event.key==pygame.K_RIGHT):
if(head_no_direction!='RIGHT'):
head_direction='RIGHT'
head_no_direction='LEFT'
elif(event.key==pygame.K_DOWN):
if(head_no_direction!='DOWN'):
head_direction='DOWN'
head_no_direction='UP'
elif(event.key==pygame.K_LEFT):
if(head_no_direction!='LEFT'):
head_direction='LEFT'
head_no_direction='RIGHT'
# Head
pygame.draw.rect(window, black, (head_X, head_Y, 20, 20))
if(head_direction=='UP'):
head_Y-=20
if(head_direction=='RIGHT'):
head_X =20
if(head_direction=='DOWN'):
head_Y =20
if(head_direction=='LEFT'):
head_X-=20
# Body
for i in range(1,snakeLength):
if(body[i].direction=='UP'):
body[i].Y-=20
if(body[i].direction=='RIGHT'):
body[i].X =20
if(body[i].direction=='DOWN'):
body[i].Y =20
if(body[i].direction=='LEFT'):
body[i].X-=20
pygame.draw.rect(window, grey, (body[i].X, body[i].Y, 20, 20))
# Updates
pygame.draw.rect(window, red, (0, 0, 500, 500),3)
displayScore()
pygame.display.update()
fps.tick(snake_speed)
# Game over
if(head_X==500 or head_X==-20 or head_Y==500 or head_Y==-20):
gameOver()