pip install pygame
使用上下左右的箭头按键操控,不是用WSAD按键操作的!!!
不废话直接给代码
import pygame
import sys
import random
# 全局定义
SCREEN_X = 600
SCREEN_Y = 600
# 蛇类
# 点以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)
# 删除最后一个块
de

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



