我一直在嘗試在 python 中設計一個蛇游戲,但是在實作了使蛇的身體(不是頭部)移動的代碼部分之后,我得到了這個錯誤:
TypeError: 'str' object is not callable
我在網上查看,我發現的只是說我不應該呼叫我的變數“str”,而我沒有,所以我有點困惑。這是課程:
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
這是發生錯誤的地方:
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))
這是整個代碼:
# 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()
我需要幫助。
uj5u.com熱心網友回復:
這部分:
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))
應該是這樣的:
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))
您的代碼還出現了一些其他問題,但這對我解決了您的問題有用。
如果您仍然遇到問題,這里是我完整運行的代碼,沒有錯誤。請注意,我替換了您的圖示和字體檔案名,以便我可以使用我機器上可用的資源。
(劇透!)# 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/350591.html
