Python代码编写报错!
用pygame编写一个贪吃蛇小游戏,出现以下错误
而且明明有编写游戏结束时出现一个“YOU DEAD!”的文字提示,运行时什么也没有
以下是代码:
import sys
import random
import pygame
#定义画布大小
screen_x = 600
screen_y = 600
#蛇
class Snake(object):
#属性:1.初始化长度
# 2.头 方向
def __init__(self):
self.body = [] #初始化蛇身:[(25,0),(50,0),(75,0)]
self.dirction = pygame.K_RIGHT #默认蛇头方向向右
for x in range(5):
self.addnode()
#方法:1.吃
# 2.死亡
# 3.移动
# 4.方向
def addnode(self):
left,top = (0,0)
if self.body:
left,top = (self.body[0].left,self.body[0].top)
node = pygame.Rect(left,top,25,25) #像素方格以25为一个单位
#判断:键盘控制蛇头方向
if self.dirction == pygame.K_LEFT:
node.left -= 25
elif self.dirction == pygame.K_RIGHT:
node.left += 25
elif self.dirction == pygame.K_UP:
node.top -= 25
elif self.dirction == pygame.K_DOWN:
node.top += 25
self.body.insert(0,node)
def delnode(self):
self.body.pop()
#死亡判断
def isdead(self):
#撞墙
if self.body[0].x not in range(screen_x):
return True
if self.body[0].y not in range(screen_y):
return True
#撞自己
if self.body[0] in self.body[1:]:
return True
return False
#移动
def move(self):
self.addnode()
self.delnode()
#改变方向:上下左右只选一个
def changedirction(self,curkey):
LR = [pygame.K_LEFT,pygame.K_RIGHT]
UD = [pygame.K_UP,pygame.K_DOWN]
if curkey in LR + UD:
if (curkey in LR) and (self.dirction in LR):
return
if (curkey in UD) and (self.dirction in UD):
return
self.dirction = curkey
#食物
class Food:
def __init__(self):
self.rect = pygame.Rect(-25,0,25,25)
def remove(self):
self.rect.x = -25
def set(self):
if self.rect.x == -25:
allpos = []
#不能靠墙太近
for pos in range(25,screen_x - 25,25):
allpos.append(pos)
self.rect.left = random.choice(allpos)
self.rect.top = random.choice(allpos)
print(self.rect)
def show_text(screen,pos,text,color,font_bold=False,font_size=60,font_italic=False):
cur_font = pygame.font.SysFont("宋体",font_size)
cur_font.set_bold(font_bold)
cur_font.set_italic(font_italic)
text_fmt = cur_font.render(text,1,color)
screen.blit(text_fmt,pos)
def main():
#初始化
pygame.init()
screen_size = (screen_x,screen_y)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('贪吃蛇') #窗口标题
clock = pygame.time.Clock() #时间滴答:页面刷新率
scores = 0 #成绩默认为0
isdead = False #默认未死亡
snake = Snake()
food = Food()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
snake.changedirction(event.key)
if event.key == pygame.K_SPACE and isdead:
return main()
screen.fill((255,255,255))
if not isdead:
snake.move()
for rect in snake.body:
pygame.draw.rect(screen,(20,220,39),rect,0)
isdead = snake.isdead()
if isdead:
show_text(screen,(100,200),'YOU DEAD!',(277,29,18),False,100)
show_text(screen,(150,260), 'press space to try again...', (0,0,22), False, 30)
#食物的处理 吃到+50 蛇长度+1(方块+25) 食物与蛇头区域是否重叠
if food.rect == snake.body[0]:
scores += 50
food.remove()
snake.addnode()
food.set()
pygame.draw.rect(screen,(136,0,24),food.rect,0)
#显示分数文字
show_text(screen,(50,500),"Scores" + str(scores),(233,233,233))
pygame.display.update() #更新
clock.tick(10)
main()
从你的描述和代码中,我无法看到具体的错误信息,所以我只能根据你的描述进行一些猜测。
游戏结束的文字没有显示:这可能是因为你的游戏结束的文字只在蛇死亡时显示,而你的蛇可能没有死亡。你可以尝试让蛇撞墙或者撞到自己,看看游戏结束的文字是否会显示。
程序卡在某个地方:这可能是因为你的程序进入了一个无限循环。你的主循环中有一个while True
,这意味着这个循环会一直执行,直到程序退出。如果在这个循环中没有适当的退出条件,你的程序就会一直卡在这里。你可以检查你的事件处理代码,看看是否有适当的退出条件。
403错误:这个错误通常是由于HTTP请求被服务器拒绝。但是在你的代码中,我没有看到任何HTTP请求。你是否在其他地方进行了HTTP请求?如果是的话,你需要检查你的请求是否正确,以及你是否有权限访问你正在请求的资源。
扩展的部分就不多说了,拿最根本的部分来讲