使用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输出当前目录下index.html文件路径的方法
Apr 28 Python
python执行get提交的方法
Apr 29 Python
浅谈Python中的闭包
Jul 08 Python
JPype实现在python中调用JAVA的实例
Jul 19 Python
使用Python实现windows下的抓包与解析
Jan 15 Python
详解python使用递归、尾递归、循环三种方式实现斐波那契数列
Jan 16 Python
Python实现PS滤镜Fish lens图像扭曲效果示例
Jan 29 Python
python正则表达式之对号入座篇
Jul 24 Python
这可能是最好玩的python GUI入门实例(推荐)
Jul 19 Python
使用 tf.nn.dynamic_rnn 展开时间维度方式
Jan 21 Python
简单的Python人脸识别系统
Jul 14 Python
python3实现无权最短路径的方法
May 12 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
9个PHP开发常用功能函数小结
2011/07/15 PHP
解析php通过cookies获取远程网页的指定代码
2013/06/25 PHP
PHP的switch判断语句的“高级”用法详解
2014/10/01 PHP
大家须知简单的php性能优化注意点
2016/01/04 PHP
PHP微信开发之查询城市天气
2016/06/23 PHP
利用PHP访问MySql数据库的逻辑操作以及增删改查的实例讲解
2017/08/30 PHP
PHP实现对数字分隔加千分号的方法
2019/03/18 PHP
jquery数组过滤筛选方法grep()简介
2014/06/06 Javascript
IE中鼠标经过option触发mouseout的解决方法
2015/01/29 Javascript
JS+CSS实现仿触屏手机拨号盘界面及功能模拟完整实例
2015/05/16 Javascript
JavaScript实现点击按钮切换网页背景色的方法
2015/10/17 Javascript
Javascript基础知识盲点总结之函数
2016/05/15 Javascript
javascript超过容器后显示省略号效果的方法(兼容一行或者多行)
2016/07/14 Javascript
Sortable.js拖拽排序使用方法解析
2016/11/04 Javascript
利用Vue.js框架实现火车票查询系统(附源码)
2017/02/27 Javascript
javascript html5轻松实现拖动功能
2017/03/01 Javascript
Bootstrap实现的经典栅格布局效果实例【附demo源码】
2017/03/30 Javascript
vue.js前后端数据交互之提交数据操作详解
2018/04/24 Javascript
JavaScript装饰者模式原理与用法实例详解
2020/03/09 Javascript
python encode和decode的妙用
2009/09/02 Python
python实现图片变亮或者变暗的方法
2015/06/01 Python
Python爬虫中urllib库的进阶学习
2018/01/05 Python
利用Python将每日一句定时推送至微信的实现方法
2018/08/13 Python
Anaconda2 5.2.0安装使用图文教程
2018/09/19 Python
Python 单例设计模式用法实例分析
2019/09/23 Python
Python使用turtle库绘制小猪佩奇(实例代码)
2020/01/16 Python
python“静态”变量、实例变量与本地变量的声明示例
2020/11/13 Python
css3 中的新特性加强记忆详解
2016/04/16 HTML / CSS
用canvas画心电图的示例代码
2018/09/10 HTML / CSS
音乐学个人的自荐书范文
2013/11/26 职场文书
体育教学随笔感言
2014/02/24 职场文书
中学校庆方案
2014/03/17 职场文书
勾股定理课后反思
2014/04/26 职场文书
2014年社会实践活动总结范文
2014/04/29 职场文书
班级班风口号大全
2015/12/25 职场文书
《曹冲称象》教学反思
2016/02/20 职场文书