python贪吃蛇游戏代码
作者:qiu2013 时间:2023-07-22 04:19:17
本文实例为大家分享了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 用户指南》,仅供参考。
来源:http://www.cnblogs.com/qiu2013/p/6087627.html
标签:python,贪吃蛇
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 根据字典的键值进行排序的方法
2023-10-07 00:07:56
![](https://img.aspxhome.com/file/2023/9/98909_0s.png)
SQL Server之JSON 函数详解
2024-01-12 14:10:20
![](https://img.aspxhome.com/file/2023/2/96202_0s.png)
Java面试题冲刺第十二天--数据库(2)
2024-01-21 18:34:27
![](https://img.aspxhome.com/file/2023/6/119176_0s.jpg)
python 嵌套型partials的使用
2021-12-24 09:01:46
W3C优质网页小贴士(四)
2008-04-17 13:34:00
PHP日志LOG类定义与用法示例
2023-09-10 20:02:56
解决tensorflow添加ptb库的问题
2022-05-28 07:44:02
Django如何使用redis作为缓存
2022-12-30 19:19:20
解决keras backend 越跑越慢问题
2022-05-27 17:36:58
利用laravel+ajax实现文件上传功能方法示例
2024-05-03 15:28:02
python中列表对象pop()方法的使用说明
2021-10-25 09:45:40
![](https://img.aspxhome.com/file/2023/1/75281_0s.png)
Pycharm内置终端及远程SSH工具的使用教程图文详解
2023-03-26 09:58:30
![](https://img.aspxhome.com/file/2023/6/101726_0s.png)
python写一个md5解密器示例
2023-05-24 14:45:10
![](https://img.aspxhome.com/file/2023/4/134694_0s.jpg)
vue项目动态设置页面title及是否缓存页面的问题
2024-04-10 10:23:52
![](https://img.aspxhome.com/file/2023/8/139778_0s.png)
layDate插件设置开始和结束时间
2024-05-03 15:05:03
在sql中实现取一行最大值或者最小值
2024-01-24 21:34:28
![](https://img.aspxhome.com/file/2023/2/112872_0s.jpg)
HTML+JavaScript实现扫雷小游戏
2024-04-17 10:03:58
![](https://img.aspxhome.com/file/2023/3/136393_0s.jpg)
Python实现爬取并分析电商评论
2022-11-21 17:18:47
![](https://img.aspxhome.com/file/2023/1/131201_0s.png)
Win11平台安装和配置NeoVim0.8.2编辑器搭建Python3开发环境详细过程(2023最新攻略)
2023-06-06 21:28:07
![](https://img.aspxhome.com/file/2023/1/111871_0s.png)
JS实现局部选择打印和局部不选择打印
2024-05-11 09:05:49