def init_body(self, length=5):
left, top = (0, 0)
for i in range(5):
if self.body:
left, top = self.body[0].left, self.body[0].top
node = pygame.Rect(left + 20, top, 20, 20)
else:
node = pygame.Rect(0, 0, 20, 20) # 设置要绘画的正方形 self.body.insert(0, node)
def draw_snake(self):
for n in self.body:
pygame.draw.rect(self.screen, (62, 122, 178), n, 0) # 绘制 def add_node(self):
if self.body:
left, top = self.body[0].left, self.body[0].top
if self.fx == pygame.K_RIGHT:
left += 20
elif self.fx == pygame.K_LEFT:
left -= 20
elif self.fx == pygame.K_UP:
top -= 20
else:
top += 20
node = pygame.Rect(left, top, 20, 20)
self.body.insert(0, node)
def del_node(self):
self.body.pop()
def move(self):
self.del_node()
self.add_node()
def change(self, fx):
LR = [pygame.K_LEFT, pygame.K_RIGHT]
UD = [pygame.K_UP, pygame.K_DOWN]
if fx in LR + UD:
if fx in LR and self.fx in LR:
return
if fx in UD and self.fx in UD:
return
self.fx = fx
def is_dead(self):
if self.body[0].left not in range(WIDTH):
return True
if self.body[0].top not in range(HEIGHT):
return True
if self.body[0] in self.body[1:]:
return True
File "D:\LPro\python\py_projects\project1\pythonProject\games\贪吃蛇\snake.py", line 2, in <module>
import pygame
File "D:\LPro\python\PythonJre\Lib\site-packages\pygame\__init__.py", line 291, in <module>
import pygame.pkgdata
File "D:\LPro\python\PythonJre\Lib\site-packages\pygame\pkgdata.py", line 25, in <module>
from pkg_resources import resource_stream, resource_exists
File "D:\LPro\python\PythonJre\Lib\site-packages\pkg_resources\__init__.py", line 2172, in <module>
register_finder(pkgutil.ImpImporter, find_on_path)
^^^^^^^^^^^^^^^^^^^
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?