用python玩转贪吃蛇

一:实验需求

实验环境:python3.8,pycharm
需要的库:pygame,random

需要下载64位对应python37版本的pygame网址
https://pypi.org/project/Pygame/1.9.4/#files
这里贴个安装方法。
https://blog.csdn.net/weixin_40673655/article/details/80990559

二:实验思路

这里介绍下贪吃蛇原理和pygame常用方法
pygame三大对象

pygame.display 显示
pygame.time 时间
pygame.event 事件
pygame.draw 绘制

颜色——RGB
red=(255,0,0)
white=(255,255,255)
black=(0,0,0)

#格子
0 0 0 0 3 0 0
0 0 0 1 2 3 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

#画出来–核心
left=c *width
top=r * height

#移动
向左
row
col-=1

向右
col+=1

#正常移动
1.把原来的头插到前面
2.把原来的尾巴删掉

#蛇身
head=(14,7)
l=[
(14,8)
(14,9)
(14,10)
(14,11)
(14,12)
(14,13)
(14,14)
]

head=(14,6)
l=[

(14,7)#原来的位置

(14,8)
(14,9)
(14,10)
(14,11)
(14,12)
(14,13)

(14,14)#原来的尾巴

]

#食物随机位置

三:源码

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)

#初始框架
import pygame
import random

#初始化
pygame.init()
W=800
H=600

ROW=30
COL=40

size=(W,H)
window=pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')

bg_color=(255,255,255)
snake_color=(200,200,200)

head=Point(row=int(ROW/2), col=int(COL/2))
head_color=(0,128,128)

snakes=[
  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 gen_food():
  while 1:
    pos=Point(row=random.randint(0,ROW-1), col=random.randint(0,COL-1))

    #
    is_coll=False

    #是否跟蛇碰上了
    if head.row==pos.row and head.col==pos.col:
      is_coll=True

    #蛇身子
    for snake in snakes:
      if snake.row==pos.row and snake.col==pos.col:
        is_coll=True
        break

    if not is_coll:
      break

  return pos


#定义坐标


food=gen_food()
food_color=(255,255,0)



direct='left'       #left,right,up,down

#
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 direct=='left' or direct=='right':
          direct='up'
      elif event.key==274 or event.key==115:
        if direct == 'left' or direct == 'right':
          direct='down'
      elif event.key==276 or event.key==97:
        if direct == 'up' or direct == 'down':
          direct='left'
      elif event.key==275 or event.key==100:
        if direct == 'up' or direct == 'down':
          direct='right'

  #吃东西
  eat=(head.row==food.row and head.col==food.col)

  #重新产生食物
  if eat:
    food = gen_food()

  #处理身子
  #1.把原来的头,插入到snakes的头上
  snakes.insert(0, head.copy())
  #2.把snakes的最后一个删掉
  if not eat:
    snakes.pop()

  #移动
  if direct=='left':
    head.col-=1
  elif direct=='right':
    head.col+=1
  elif direct=='up':
    head.row-=1
  elif direct=='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 snakes:
    if head.col==snake.col and head.row==snake.row:
      dead=True
      break

  if dead:
    print('死了')
    quit=False

  #渲染——画出来
  #背景
  pygame.draw.rect(window, bg_color, (0,0,W,H))

  #蛇头
  for snake in snakes:
    rect(snake, snake_color)
  rect(head, head_color)
  rect(food, food_color)

  #
  pygame.display.flip()

  #设置帧频
  clock.tick(20)

#收尾工作

完成啦!在贴一个我的源码和笔记供大家参考。
链接:https://pan.baidu.com/s/1F8D_dDdydgeLWyzKuyWRSw
提取码:ds0l

复制这段内容后打开百度网盘手机App,操作更方便哦

发布了17 篇原创文章 · 获赞 24 · 访问量 4184

猜你喜欢

转载自blog.csdn.net/qq_45714272/article/details/105224058