此项目利用童芯派的方向键对 pygame 中的贪吃蛇进行控制。
import pygamefrom sys import exitimport randomimport timeimport cyberpiclass Point():def __init__(self, row, clo):self.row = rowself.clo = clodef copy(self):return Point(row=self.row, clo=self.clo)# 初始化pygame.init()width = 800hight = 400ROW = 30CLO = 50direct = 'left'window = pygame.display.set_mode((width, hight))pygame.display.set_caption('贪吃蛇游戏')# 蛇头坐标定在中间head = Point(row=int(ROW / 2), clo=int(CLO / 2))# 初始化蛇身的元素数量snake = [Point(row=head.row, clo=head.clo + 1),Point(row=head.row, clo=head.clo + 2),Point(row=head.row, clo=head.clo + 3)]# 生成食物并且不让食物生成在蛇的身体里面def gen_food():while 1:position = Point(row=random.randint(0, ROW - 1), clo=random.randint(0, CLO - 1))is_coll = Falseif head.row == position.row and head.clo == position.clo:is_coll = Truefor body in snake:if body.row == position.row and body.clo == position.clo:is_coll = Truebreakif not is_coll:breakreturn position# 定义坐标# 蛇头颜色可以自定义head_color = (0, 158, 128)# 食物坐标snakeFood = gen_food()# 食物颜色snakeFood_color = (255, 255, 0)snake_color = (200, 0, 18)# 需要执行很多步画图操作 所以定义一个函数def rect(point, color):# 定位 画图需要left和topleft = point.clo * width / CLOtop = point.row * hight / ROW# 将方块涂色pygame.draw.rect(window, color, (left, top, width / CLO, hight / ROW))quit = True# 设置帧频率clock = pygame.time.Clock()while quit:# 处理帧频 锁帧clock.tick(30)# cyberpi 遥杆控制if cyberpi.controller.is_press("up"):if direct == 'left' or direct == 'right':direct = 'top'if cyberpi.controller.is_press("down"):if direct == 'left' or direct == 'right':direct = 'bottom'if cyberpi.controller.is_press("left"):if direct == 'top' or direct == 'bottom':direct = 'left'if cyberpi.controller.is_press("right"):if direct == 'top' or direct == 'bottom':direct = 'right'# 键盘控制for event in pygame.event.get():if event.type == pygame.QUIT:quit = Falseelif event.type == pygame.KEYDOWN:if event.key == 273 or event.key == 119:if direct == 'left' or direct == 'right':direct = 'top'if event.key == 274 or event.key == 115:if direct == 'left' or direct == 'right':direct = 'bottom'if event.key == 276 or event.key == 97:if direct == 'top' or direct == 'bottom':direct = 'left'if event.key == 275 or event.key == 100:if direct == 'top' or direct == 'bottom':direct = 'right'# 吃东西eat = (head.row == snakeFood.row and head.clo == snakeFood.clo)# 处理蛇的身子# 1.把原来的头插入到snake的头上# 2.把最后一个snake删掉if eat:snakeFood = Point(row=random.randint(0, ROW - 1), clo=random.randint(0, CLO - 1))snake.insert(0, head.copy())if not eat:snake.pop()# 移动一下if direct == 'left':head.clo -= 1if direct == 'right':head.clo += 1if direct == 'top':head.row -= 1if direct == 'bottom':head.row += 1dead = Falseif head.clo < 0 or head.row < 0 or head.clo >= CLO or head.row >= ROW:dead = Truefor body in snake:if head.clo == body.clo and head.row == body.row:dead = Truebreakif dead:print('Game Over')pygame.quit()exit()quit = False# 背景画图pygame.draw.rect(window, (20, 10, 10), (0, 0, width, hight))# 蛇头rect(head, head_color)# 绘制食物rect(snakeFood, snakeFood_color)# 绘制蛇的身子for body in snake:rect(body, snake_color)# 交还控制权pygame.display.flip()time.sleep(0.05)pygame.quit()exit()
