赞
踩
#!/usr/bin/env python # -*- coding: utf-8 -*- # author:Wangdali time:2021年1月20日16:08:44 #python实现:贪吃蛇 ''' 游戏玩法:回车开始游戏;空格暂停游戏/继续游戏;方向键/wsad控制小蛇走向 ''' ''' 思路:用列表存储蛇的身体;用浅色表示身体,深色背景将身体凸显出来; 蛇的移动:仔细观察,是:身体除头和尾不动、尾部消失,头部增加,所以,新添加的元素放在列表头部、删除尾部元素; 游戏结束判定策略:超出边界;触碰到自己的身体:蛇前进的下一格子为身体的一部分(即在列表中)。 ''' #注:因为在列表中需要频繁添加和删除元素,所以用deque容器代替列表;是因为deque具有高效的插入和删除效率 #初始化蛇,长度为3,放置在屏幕左上角; #导包 import random import sys import time import pygame from pygame.locals import * from collections import deque #基础设置 Screen_Height=480 Screen_Width=600 Size=20#小方格大小 Line_Width=1 #游戏区域的坐标范围 Area_x=(0,Screen_Width//Size-1) #0是左边界,1是右边界 #注:python中//为整数除法;/为浮点数除法 Area_y=(2,Screen_Height//Size-1) #食物的初步设置 #食物的分值+颜色 Food_Style_List=[(10,(255,100,100)),(20,(100,255,100)),(30,(100,100,255))] #整体颜色设置 Light=(100,100,100) Dark=(200,200,200) Black=(0,0,0) Red=(200,30,30) Back_Ground=(40,40,60) #文本输出格式设置 def Print_Txt(screen,font,x,y,text,fcolor=(255,255,255)): #font.render参数意义:.render(内容,是否抗锯齿,字体颜色,字体背景颜色) Text=font.render(text,True,fcolor) screen.blit(Text,(x,y)) #初始化蛇 def init_snake(): snake=
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。