#话不多说,直接上代码,有需要的小伙伴可以直接拿去用import pygame
import random
import sys
import time
from pygame.locals import * # 从pygame模块导入常用的函数和常量# 定义颜色变量
black_colour = pygame.Color(0, 0, 0)
white_colour = pygame.Color(255, 255, 255)
red_colour = pygame.Color(255, 0, 0)
grey_colour = pygame.Color(150, 150, 150)# 定义游戏结束函数
def GameOver(gamesurface):# 设置提示字体的格式GameOver_font = pygame.font.SysFont("MicrosoftYaHei", 16)# 设置提示字体的颜色GameOver_colour = GameOver_font.render('Game Over', True, grey_colour)# 设置提示位置GameOver_location = GameOver_colour.get_rect()GameOver_location.midtop = (320, 10)gamesurface.blit(GameOver_colour, GameOver_location)# 提示运行信息pygame.display.flip()# 等待5秒time.sleep(5)# 退出游戏pygame.quit()# 退出程序sys.exit()# 定义主函数
def main():# 初始化pygamepygame.init()pygame.time.Clock()ftpsClock = pygame.time.Clock()# 创建一个窗口gamesurface = pygame.display.set_mode((640, 480))# 设置窗口的标题pygame.display.set_caption('贪吃蛇~')# 初始化变量# 初始化贪吃蛇的起始位置snakeposition = [100, 100]# 初始化贪吃蛇的长度snakelength = [[100, 100], [80, 100], [60, 100]]# 初始化目标方块的位置square_purpose = [300, 300]# 初始化一个数来判断目标方块是否存在square_position = 1# 初始化方向,用来使贪吃蛇移动derection = "right"change_derection = derection# 进行游戏主循环while True:# 检测按键for event in pygame.event.get():if event.type == QUIT:# 退出程序pygame.quit()sys.exit()elif event.type == KEYDOWN:# 判断按的键,用w,s,a,d来表示上下左右if event.key == K_RIGHT or event.key == ord('d'):change_derection = "right"if event.key == K_LEFT or event.key == ord('a'):change_derection = "left"if event.key == K_UP or event.key == ord('w'):change_derection = "up"if event.key == K_DOWN or event.key == ord('s'):change_derection = "down"if event.key == K_ESCAPE:pygame.event.post(pygame.event.Event(QUIT))# 判断移动的方向是否相反if change_derection == 'left' and not derection == 'right':derection = change_derectionif change_derection == 'right' and not derection == 'left':derection = change_derectionif change_derection == 'up' and not derection == 'down':derection = change_derectionif change_derection == 'down' and not derection == 'up':derection = change_derection# 根据方向,改变坐标if derection == 'left':snakeposition[0] -= 20if derection == 'right':snakeposition[0] += 20if derection == 'up':snakeposition[1] -= 20if derection == 'down':snakeposition[1] += 20# 增加蛇的长度snakelength.insert(0, list(snakeposition))# 判断是否吃掉目标方块if snakeposition[0] == square_purpose[0] and snakeposition[1] == square_purpose[1]:square_position = 0else:snakelength.pop()# 重新生成目标方块if square_position == 0:# 随机生成x,y,扩大二十倍,在窗口范围内x = random.randrange(1, 32)y = random.randrange(1, 24)square_purpose = [int(x * 20), int(y * 20)]square_position = 1# 绘制pygame显示层gamesurface.fill(black_colour)for position in snakelength:pygame.draw.rect(gamesurface, white_colour, Rect(position[0], position[1], 20, 20))pygame.draw.rect(gamesurface, red_colour, Rect(square_purpose[0], square_purpose[1], 20, 20))# 刷新pygame显示层pygame.display.flip()# 判断是否死亡if snakeposition[0] < 0 or snakeposition[0] > 620:GameOver(gamesurface)if snakeposition[1] < 0 or snakeposition[1] > 460:GameOver(gamesurface)for snakebody in snakelength[1:]:if snakeposition[0] == snakebody[0] and snakeposition[1] == snakebody[1]:GameOver(gamesurface)# 控制游戏速度ftpsClock.tick(5)if __name__ == "__main__":main()
详细解决方案
python 贪吃蛇(源代码)
热度:31 发布时间:2024-01-30 13:46:51.0
相关解决方案
- ubuntu9.04上安装mic2 出错 (python setup.py build)
- ubuntu9.04下安装mic2 出错 (python setup.py build),该如何解决
- python sybase装配
- python(django框架) + mysql 的编码有关问题,求解
- SAE python web.py bootstrap 开发记要
- 数目字代码把大数据数字口语化(python+js) 数字代码
- python JSON string 的中文有关问题
- python――施用yaml数据格式,PK -> XML,JSON
- Learn Python The Hard Way学习(52) - 开始您的web游戏
- Python 相关有关问题
- 菜鸟,先学JAVA,还是Ruby,python
- 【大哥大姐】python 操作MS SQL Server 2005 效率
- python serial模块运用
- python scp自动登录解决方法
- 【Python】按行读取文件、IOError: [Errno 22] invalid mode ('a+') or filename,处置文件的换行符
- Python 3.x中使用print函数出现语法异常(SyntaxError: invalid syntax)的原因
- 【Error】Python安装gevent时出错:“致命异常: Python.h:没有那个文件或目录”
- [Python]attributeError:'module' object has no attribute 'dump'
- Python 出错:IndentationError: unexpected indent
- Python“TypeError: 'NoneType' object is not iterable"的缘故
- python in操作挑动 TypeError
- 【Error】Python:SyntaxError: Non-ASCII character '\xe5'解决办法
- python 有什么 步骤可以知道函数或类在哪个文件定义的
- Python 将数组转换为 Wav
- Python:文件格式不正确
- 如何在 Bash 的 python 解释器中运行 python 脚本
- python-改进脚本和异常处理
- Python(附加列表时出现内存错误)
- Python:另存为字符串时,保存数字错误
- Python:如何编写代码序列?