2 Star 3 Fork 1

fu / 3D_Snake

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Snake.py 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
fu 提交于 2020-08-06 19:39 . first commit
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: fu
"""
import Common
class Snake(object):
def __init__(self):
self.curdir = Common.pygame.K_RIGHT
self.body = []
self.colors = []
self.length = 0
for x in range(5):
self.addnode()
self.addcolor()
self.length += 1
def nextnode(self):
left,top = (0,0)
if self.body:
left,top = (self.body[0].left,self.body[0].top)
node = Common.pygame.Rect(left, top, 40, 40)
if self.curdir == Common.pygame.K_LEFT:
node.left -= 20
node.top -= 10
elif self.curdir == Common.pygame.K_RIGHT:
node.left += 20
node.top += 10
elif self.curdir == Common.pygame.K_UP:
node.left += 20
node.top -= 10
elif self.curdir == Common.pygame.K_DOWN:
node.left -= 20
node.top += 10
return node
# add node at 0
def addnode(self):
node = self.nextnode()
self.body.insert(0, node)
# delete node at -1
def delnode(self, index = -1):
self.body.pop(index)
# add color at 0
def addcolor(self):
self.colors.insert(0, 'img/Blocks/' + Common.random_color() + ' 3' + '.png')
def isdead(self, edges):
# hits edge
if (self.body[0].x, self.body[0].y) in edges:
return True
# hits itself
if self.body[0] in self.body[1:]:
return True
return False
def move(self):
self.addnode()
self.delnode()
def changedirection(self, curkey):
LR = [Common.pygame.K_LEFT, Common.pygame.K_RIGHT]
UD = [Common.pygame.K_UP, Common.pygame.K_DOWN]
if curkey in LR+UD:
if not (((curkey in LR) and (self.curdir in LR)) or ((curkey in UD) and (self.curdir in UD))):
self.curdir = curkey
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fu23/Adapted-game-3DSnakePostman.git
git@gitee.com:fu23/Adapted-game-3DSnakePostman.git
fu23
Adapted-game-3DSnakePostman
3D_Snake
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891