使用Python第三方库pygame写个贪吃蛇小游戏


Posted in Python onMarch 06, 2020

今天看到几个关于pygame模块的博客和视频,感觉非常有趣,这里照猫画虎写了一个贪吃蛇小游戏,目前还有待完善,但是基本游戏功能已经实现,下面是代码:

# 导入模块
import pygame
import random
 # 初始化
pygame.init()
w = 720   #窗口宽度
h = 600   #窗口高度
ROW = 30  #行数
COL = 36  #列数
#将所有的坐标看作是一个个点,定义点类
class Point:   
  row = 0
  col = 0
  def __init__(self, row, col):
    self.row = row
    self.col = col
  def copy(self):
    return Point(row = self.row,col = self.col)
#显示窗口和标题
size = (w, h)
window = pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
#定义蛇头坐标
head = Point(row = ROW/2, col = COL/2)
#蛇身体
snake_list = [
  Point(row = head.row,col = head.col+1),
  Point(row = head.row,col = head.col+2),
  Point(row = head.row,col = head.col+3)
]
#产生食物
def pro_food():
  #食物不能与蛇重叠
  while True:
    pos = Point(row=random.randint(1,ROW-2), col=random.randint(1,COL-2))
    is_coll = False
    if head.row == pos.row and head.col == pos.col:
      is_coll = True
    for snake in snake_list:
      if snake.col == pos.col and snake.row == pos.row:
        is_coll = True
        break
    if not is_coll:
      return pos
food = pro_food()
#定义颜色
bg_color = (255, 255, 255)
head_color = (0, 128, 128)
food_color = (255, 255, 0)
snake_color = (200,200,200)
#给定初始方向
dire = 'left'
def rect(point, color):
  cell_width = w/COL
  cell_height = h/ROW
  left = point.col*cell_width
  top = point.row*cell_height
  pygame.draw.rect(
    window, color,
    (left,top,cell_width, cell_height, )
  )
  pass
# 游戏循环
quit = True
clock = pygame.time.Clock()
while quit:
  for event in pygame.event.get():
    #退出方式
    if event.type == pygame.QUIT:
      quit = False
    elif event.type == pygame.KEYDOWN:
      #键盘控制
      if event.key == 273 or event.key == 119:
        if dire == 'left' or dire == 'right':
          dire = 'up'
      elif event.key == 274 or event.key == 115:
        if dire == 'left' or dire == 'right':
          dire = 'down'
      elif event.key == 276 or event.key == 97:
        if dire == 'up' or dire == 'down':
          dire = 'left'
      elif event.key == 275 or event.key == 100:
        if dire == 'up' or dire == 'down':
          dire = 'right'
  #吃
  eat=(head.row == food.row and head.col == food.col)
  if eat:
    food = pro_food()
  #处理身体
  #1.原来的头换到身体最前端
  snake_list.insert(0,head.copy())
  #2.删除身体最后一个
  if not eat:
    snake_list.pop()
  #移动
  if dire == 'left':
    head.col -= 1
  elif dire == 'right':
    head.col += 1
  elif dire == 'up':
    head.row -= 1
  elif dire == 'down':
    head.row += 1
  #检测:
  dead=False
  #1.撞墙
  if head.col < 0 or head.row< 0 or head.col >= COL or head.row >= ROW:
    dead=True
  #2.撞自己
  for snake in snake_list:
    if head.col == snake.col and head.row == snake.row:
      dead=True
      break
  if dead:
    print('dead')
    quit = False
  #绘制背景
  pygame.draw.rect(window, bg_color, (0, 0, w, h))
  #蛇头
  rect(head, head_color)
  #食物
  rect(food,food_color)
  #蛇身
  for snake in snake_list:
    rect(snake,snake_color)
  pygame.display.flip()
  #游戏帧数
  clock.tick(20)

效果:

使用Python第三方库pygame写个贪吃蛇小游戏

总结

到此这篇关于使用Python第三方库pygame写个贪吃蛇小游戏的文章就介绍到这了,更多相关python 贪吃蛇游戏内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
phpsir 开发 一个检测百度关键字网站排名的python 程序
Sep 17 Python
python中的reduce内建函数使用方法指南
Aug 31 Python
Python检测一个对象是否为字符串类的方法
May 21 Python
windows下python连接oracle数据库
Jun 07 Python
10个Python小技巧你值得拥有
Sep 29 Python
在python中只选取列表中某一纵列的方法
Nov 28 Python
使用python telnetlib批量备份交换机配置的方法
Jul 25 Python
python django model联合主键的例子
Aug 06 Python
python web框架Flask实现图形验证码及验证码的动态刷新实例
Oct 14 Python
pytorch使用tensorboardX进行loss可视化实例
Feb 24 Python
一文解决django 2.2与mysql兼容性问题
Jul 15 Python
请求模块urllib之PYTHON爬虫的基本使用
Apr 08 Python
Python修改列表值问题解决方案
Mar 06 #Python
浅谈matplotlib.pyplot与axes的关系
Mar 06 #Python
python-xpath获取html文档的部分内容
Mar 06 #Python
关于python中的xpath解析定位
Mar 06 #Python
Python网络爬虫信息提取mooc代码实例
Mar 06 #Python
appium+python adb常用命令分享
Mar 06 #Python
Python+appium框架原生代码实现App自动化测试详解
Mar 06 #Python
You might like
探讨:如何编写PHP扩展
2013/06/13 PHP
PHP学习笔记(一) 简单了解PHP
2014/08/04 PHP
PHP利用APC模块实现文件上传进度条的方法
2015/01/26 PHP
php生成RSS订阅的方法
2015/02/13 PHP
PHP快速生成各种信息提示框的方法
2016/02/03 PHP
Laravel框架实现多数据库连接操作详解
2019/07/12 PHP
js 判断checkbox是否选中的实现代码
2010/11/23 Javascript
javascript复制对象使用说明
2011/06/28 Javascript
js去字符串前后空格5种实现方法及比较
2013/04/03 Javascript
js 自动播放的实例代码
2013/11/19 Javascript
jQuery随机密码生成的方法
2015/03/09 Javascript
jQuery时间插件jquery.clock.js用法实例(5个示例)
2016/01/14 Javascript
jQuery链式调用与show知识浅析
2016/05/11 Javascript
jQuery File Upload文件上传插件使用详解
2016/12/06 Javascript
js控制一个按钮是否可点击(可使用)disabled的实例
2017/02/14 Javascript
JavaScript中双向数据绑定详解
2017/05/03 Javascript
30分钟快速实现小程序语音识别功能
2018/11/27 Javascript
Python splitlines使用技巧
2008/09/06 Python
Python调用C/C++动态链接库的方法详解
2014/07/22 Python
Python中处理时间的几种方法小结
2015/04/09 Python
使用IPython下的Net-SNMP来管理类UNIX系统的教程
2015/04/15 Python
Python 普通最小二乘法(OLS)进行多项式拟合的方法
2018/12/29 Python
Python 硬币兑换问题
2019/07/29 Python
Django实现celery定时任务过程解析
2020/04/21 Python
Django调用百度AI接口实现人脸注册登录代码实例
2020/04/23 Python
Python学习笔记之装饰器
2020/08/06 Python
CSS3实现大小不一的粒子旋转加载动画
2016/04/21 HTML / CSS
GLAMGLOW香港官网:明星出镜前的秘密武器
2017/03/16 全球购物
爱尔兰领先的在线体育用品零售商:theGAAstore
2018/04/16 全球购物
Goodee官方商店:迷你投影仪
2021/03/15 全球购物
工程造价专业大学生自荐信
2013/10/01 职场文书
实习协议书
2015/01/27 职场文书
2015年法院工作总结范文
2015/04/28 职场文书
首席执行官观后感
2015/06/03 职场文书
Nginx域名转发使用场景代码实例
2021/03/31 Servers
Django使用channels + websocket打造在线聊天室
2021/05/20 Python