此实例为Python写的一个简单的贪吃蛇游戏,提供Python入门者学习
 # 蛇类# 点以25为单位class Snake(object):  # 初始化各种需要的属性 [开始时默认向右/身体块x5]  def __init__(self):    self.dirction = pygame.K_RIGHT    self.body = []    for x in range(5):      self.addnode()      # 无论何时 都在前端增加蛇块  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)    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
# 蛇类# 点以25为单位class Snake(object):  # 初始化各种需要的属性 [开始时默认向右/身体块x5]  def __init__(self):    self.dirction = pygame.K_RIGHT    self.body = []    for x in range(5):      self.addnode()      # 无论何时 都在前端增加蛇块  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)    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

 
  
					
				
评论