使用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 相关文章推荐
Python random模块常用方法
Nov 03 Python
python中根据字符串调用函数的实现方法
Jun 12 Python
利用Python开发微信支付的注意事项
Aug 19 Python
python django 原生sql 获取数据的例子
Aug 14 Python
关于python 跨域处理方式详解
Mar 28 Python
如何在django中运行scrapy框架
Apr 22 Python
浅谈python 调用open()打开文件时路径出错的原因
Jun 05 Python
浅析python中的del用法
Sep 02 Python
python 如何实现遗传算法
Sep 22 Python
Python+OpenCV图像处理—— 色彩空间转换
Oct 22 Python
python help函数实例用法
Dec 06 Python
Python编写冷笑话生成器
Apr 20 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 &amp;&amp; 逻辑与运算符使用说明
2010/03/04 PHP
解析php中获取系统信息的方法
2013/06/25 PHP
php的sprintf函数的用法 控制浮点数格式
2014/02/14 PHP
codeigniter中测试通过的分页类示例
2014/04/17 PHP
php之curl设置超时实例
2014/11/03 PHP
PHP嵌套输出缓冲代码实例
2015/05/12 PHP
php自动更新版权信息显示的方法
2015/06/19 PHP
PHP cookie与session会话基本用法实例分析
2019/11/18 PHP
jQuery表格排序组件-tablesorter使用示例
2014/05/26 Javascript
jquery操作对象数组元素方法详解
2014/11/26 Javascript
jQuery中slideUp()方法用法分析
2014/12/24 Javascript
配置Grunt的Task时通配符支持和动态生成文件名问题
2015/09/06 Javascript
nodejs前端自动化构建环境的搭建
2017/07/26 NodeJs
Node.JS 循环递归复制文件夹目录及其子文件夹下的所有文件
2017/09/18 Javascript
vue登录注册及token验证实现代码
2017/12/14 Javascript
Javascript中window.name属性详解
2020/11/19 Javascript
python脚本监控docker容器
2016/04/27 Python
解析Mac OS下部署Pyhton的Django框架项目的过程
2016/05/03 Python
动感网页相册 python编写简单文件夹内图片浏览工具
2016/08/17 Python
Python拼接微信好友头像大图的实现方法
2018/08/01 Python
Python使用QQ邮箱发送邮件报错smtplib.SMTPAuthenticationError
2019/12/20 Python
解决Pycharm中恢复被exclude的项目问题(pycharm source root)
2020/02/14 Python
Pycharm中切换pytorch的环境和配置的教程详解
2020/03/13 Python
keras多显卡训练方式
2020/06/10 Python
thinkphp5 路由分发原理
2021/03/18 PHP
超酷炫 CSS3垂直手风琴菜单
2016/06/28 HTML / CSS
腾讯技术类校园招聘笔试试题
2014/05/06 面试题
四年的个人工作自我评价
2013/12/10 职场文书
个人廉洁自律承诺书
2014/03/27 职场文书
开门红主持词
2014/04/02 职场文书
公司年终奖分配方案
2014/06/16 职场文书
化学专业毕业生求职信
2014/07/28 职场文书
党的群众路线专项整治方案
2014/11/03 职场文书
2015年度学校卫生工作总结
2015/05/12 职场文书
《月光曲》教学反思
2016/02/16 职场文书
导游词之台湾阿里山
2019/10/23 职场文书