python贪吃蛇游戏代码


Posted in Python onApril 18, 2020

本文实例为大家分享了python贪吃蛇游戏的具体代码,供大家参考,具体内容如下

贪吃蛇游戏截图:

python贪吃蛇游戏代码

首先安装pygame,可以使用pip安装pygame:

pip install pygame

运行以下代码即可:

#!/usr/bin/env python
import pygame,sys,time,random
from pygame.locals import *
# 定义颜色变量
redColour = pygame.Color(255,0,0)
blackColour = pygame.Color(0,0,0)
whiteColour = pygame.Color(255,255,255)
greyColour = pygame.Color(150,150,150)

# 定义gameOver函数
def gameOver(playSurface):
 gameOverFont = pygame.font.Font('arial.ttf',72)
 gameOverSurf = gameOverFont.render('Game Over', True, greyColour)
 gameOverRect = gameOverSurf.get_rect()
 gameOverRect.midtop = (320, 10)
 playSurface.blit(gameOverSurf, gameOverRect)
 pygame.display.flip()
 time.sleep(5)
 pygame.quit()
 sys.exit()

# 定义main函数
def main():
 # 初始化pygame
 pygame.init()
 fpsClock = pygame.time.Clock()
 # 创建pygame显示层
 playSurface = pygame.display.set_mode((640,480))
 pygame.display.set_caption('Raspberry Snake')

 # 初始化变量
 snakePosition = [100,100]
 snakeSegments = [[100,100],[80,100],[60,100]]
 raspberryPosition = [300,300]
 raspberrySpawned = 1
 direction = 'right'
 changeDirection = direction
 while True:
 # 检测例如按键等pygame事件
 for event in pygame.event.get():
 if event.type == QUIT:
 pygame.quit()
 sys.exit()
 elif event.type == KEYDOWN:
 # 判断键盘事件
 if event.key == K_RIGHT or event.key == ord('d'):
 changeDirection = 'right'
 if event.key == K_LEFT or event.key == ord('a'):
 changeDirection = 'left'
 if event.key == K_UP or event.key == ord('w'):
 changeDirection = 'up'
 if event.key == K_DOWN or event.key == ord('s'):
 changeDirection = 'down'
 if event.key == K_ESCAPE:
 pygame.event.post(pygame.event.Event(QUIT))
 # 判断是否输入了反方向
 if changeDirection == 'right' and not direction == 'left':
 direction = changeDirection
 if changeDirection == 'left' and not direction == 'right':
 direction = changeDirection
 if changeDirection == 'up' and not direction == 'down':
 direction = changeDirection
 if changeDirection == 'down' and not direction == 'up':
 direction = changeDirection
 # 根据方向移动蛇头的坐标
 if direction == 'right':
 snakePosition[0] += 20
 if direction == 'left':
 snakePosition[0] -= 20
 if direction == 'up':
 snakePosition[1] -= 20
 if direction == 'down':
 snakePosition[1] += 20
 # 增加蛇的长度
 snakeSegments.insert(0,list(snakePosition))
 # 判断是否吃掉了树莓
 if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]:
 raspberrySpawned = 0
 else:
 snakeSegments.pop()
 # 如果吃掉树莓,则重新生成树莓
 if raspberrySpawned == 0:
 x = random.randrange(1,32)
 y = random.randrange(1,24)
 raspberryPosition = [int(x*20),int(y*20)]
 raspberrySpawned = 1
 # 绘制pygame显示层
 playSurface.fill(blackColour)
 for position in snakeSegments:
 pygame.draw.rect(playSurface,whiteColour,Rect(position[0],position[1],20,20))
 pygame.draw.rect(playSurface,redColour,Rect(raspberryPosition[0], raspberryPosition[1],20,20))

 # 刷新pygame显示层
 pygame.display.flip()
 # 判断是否死亡
 if snakePosition[0] > 620 or snakePosition[0] < 0:
 gameOver(playSurface)
 if snakePosition[1] > 460 or snakePosition[1] < 0:
 for snakeBody in snakeSegments[1:]:
 if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:
 gameOver(playSurface)
 # 控制游戏速度
 fpsClock.tick(5)

if __name__ == "__main__":
 main()

操作方法:

上下左右键或wsad键控制

ESC键退出游戏

下载代码:贪吃蛇游戏代码

游戏代码来源于《Raspberry Pi 用户指南》,仅供参考。

更多关于python游戏的精彩文章请点击查看以下专题:

更多有趣的经典小游戏实现专题,分享给大家:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
详解Python中的__init__和__new__
Mar 12 Python
Python3基础之list列表实例解析
Aug 13 Python
Python入门篇之条件、循环
Oct 17 Python
python通过ftplib登录到ftp服务器的方法
May 08 Python
PyQt5每天必学之工具提示功能
Apr 19 Python
Python学习笔记之视频人脸检测识别实例教程
Mar 06 Python
深入了解Django中间件及其方法
Jul 26 Python
Python中模块(Module)和包(Package)的区别详解
Aug 07 Python
原生python实现knn分类算法
Oct 24 Python
基于Tensorflow的MNIST手写数字识别分类
Jun 17 Python
python中四舍五入的正确打开方式
Jan 18 Python
Python使用MapReduce进行简单的销售统计
Apr 22 Python
DRF跨域后端解决之django-cors-headers的使用
Jan 27 #Python
在numpy矩阵中令小于0的元素改为0的实例
Jan 26 #Python
pandas把所有大于0的数设置为1的方法
Jan 26 #Python
python 判断矩阵中每行非零个数的方法
Jan 26 #Python
对python 判断数字是否小于0的方法详解
Jan 26 #Python
python opencv 读取本地视频文件 修改ffmpeg的方法
Jan 26 #Python
在Python中调用Ping命令,批量IP的方法
Jan 26 #Python
You might like
PHP删除数组中特定元素的两种方法
2013/07/02 PHP
php 判断过去离现在几年的函数(实例代码)
2016/11/15 PHP
Yii2框架数据验证操作实例详解
2018/05/02 PHP
用js实现计算加载页面所用的时间
2010/04/02 Javascript
jquery方法+js一般方法+js面向对象方法实现拖拽效果
2012/08/30 Javascript
js里怎么取select标签里的值并修改
2012/12/10 Javascript
JS实现为表格动态添加标题的方法
2015/03/31 Javascript
jQuery实现新消息在网页标题闪烁提示
2015/06/23 Javascript
jQuery实现根据滚动条位置加载相应内容功能
2016/07/18 Javascript
jQuery检查元素存在性(推荐)
2016/09/17 Javascript
JS声明式函数与赋值式函数实例分析
2016/12/13 Javascript
Vue.js上下滚动加载组件的实例代码
2017/07/17 Javascript
angularjs利用directive实现移动端自定义软键盘的示例
2017/09/20 Javascript
vue组件父与子通信详解(一)
2017/11/07 Javascript
jQuery实现碰到边缘反弹的动画效果
2018/02/24 jQuery
javascript实现计算指定范围内的质数示例
2018/12/29 Javascript
JS使用Prim算法和Kruskal算法实现最小生成树
2019/01/17 Javascript
JavaScript实现拖拽效果
2020/03/16 Javascript
ES2020让代码更优美的运算符 (?.) (??)
2021/01/04 Javascript
vue常用高阶函数及综合实例
2021/02/25 Vue.js
python使用nntp读取新闻组内容的方法
2015/05/08 Python
浅析Python基础-流程控制
2016/03/18 Python
Python正则表达式匹配日期与时间的方法
2019/07/07 Python
使用Python轻松完成垃圾分类(基于图像识别)
2019/07/09 Python
Html5之webcoekt播放JPEG图片流
2020/09/22 HTML / CSS
HearthSong官网:儿童户外玩具、儿童益智玩具
2017/10/16 全球购物
GafasWorld西班牙:购买太阳镜、眼镜和隐形眼镜
2019/09/08 全球购物
《绿色蝈蝈》教学反思
2014/03/02 职场文书
小学向国旗敬礼活动方案
2014/09/27 职场文书
群众路线剖析材料(四风)
2014/11/05 职场文书
2014年心理健康教育工作总结
2014/12/06 职场文书
中秋节主题班会
2015/08/14 职场文书
大学生创业,为什么都会选择快餐饮?
2019/08/08 职场文书
导游词之舟山普陀山
2019/11/06 职场文书
CSS3点击按钮圆形进度打钩效果的实现代码
2021/03/30 HTML / CSS
详细谈谈JavaScript中循环之间的差异
2021/08/23 Javascript