【天启教育M1开发板试用体验】贪吃蛇3

描述

本文来源电子发烧友社区,作者:刘建华, 帖子地址:https://bbs.elecfans.com/jishu_2310612_1_1.html


前面生成了运动、生成食物的算法。这次增加吃食物的算法:
1、创建生成食物的标志build_food_state,在主程序中判断,如果标志需要创建食物,则生成。如果食物还存在,就不生成食物。
2、吃到食物的判断:如果生成下一个食物的坐标数组与食物的坐标数组相等,则不消除最后尾的块,同时更新生成食物的标志为真。如果没有吃到食物,则消除尾部块。

# 如果加入的方块等于食物,则不删去尾块
  if disp_List[-1] == disp_food:
    buid_food_state = 1
  else:
    del disp_List[0]

整体程序如下:

import os
import tqs1
import tqmain
import tqmath
#value= '1000000,1000000,1000000,1000000,1000000,1000000'
#mylist =[0x08,0x08,0x08,0x08,0x08,0x08]
myItem =[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]
#tqmain.serial_write_line("start")
state = True
disp_List = [[0,0]]
disp_food = []
time_state = 0
move_state = 0
buid_food_state = 1

def disp_tq():
  global state,myItem
  state = False
  mystr =[]
  for j in range(0,6):
    strit = ''.join(str(i) for i in myItem[j])
    mystr.append(strit)
  print(mystr)
  value = ','.join(mystr)
  tqmain.matri_strfill_display(value)
  state = False
  
def get_key():
  global state,move_state
  key_value1 = tqs1.key_get_status(2)
  if key_value1 == 0:
    move_state = move_state +1
    if move_state > 3:
      move_state = 0
    print("KeyB changed,move_state:" + str(move_state))
    state = True
  key_value2 = tqs1.key_get_status(1)
  if key_value2 == 0:
    move_state = move_state -1
    if move_state < 0:
      move_state = 3
    print("KeyA changed,move_state:" + str(move_state))
    state = True
def move():
  global move_state,myItem,disp_List,disp_food,buid_food_state
  #先把同最前一个灭了
  fisrt = disp_List[0]
  myItem[fisrt[0]][fisrt[1]]=0
  if move_state == 0:
    tail = disp_List[-1]
    print(tail)
    x=tail[0]
    y=tail[1]+1
    if(y>6):
      y=0
    disp_List.append([x,y])
  elif move_state == 1:
    tail = disp_List[-1]
    print(tail)
    x=tail[0]+1
    y=tail[1]
    if(x>5):
      x=0
    disp_List.append([x,y])
  elif move_state == 2:
    tail = disp_List[-1]
    print(tail)
    x=tail[0]
    y=tail[1]-1
    if(y<0):
      y=6
    disp_List.append([x,y])
  elif move_state == 3:
    tail = disp_List[-1]
    print(tail)
    x=tail[0]-1
    y=tail[1]
    if(x<0):
      x=5
    disp_List.append([x,y])
  # 如果加入的方块等于食物,则不删去尾块
  if disp_List[-1] == disp_food:
    buid_food_state = 1
  else:
    del disp_List[0]

  for item in disp_List:
    myItem[item[0]][item[1]]=1
  if len(disp_food) == 2:
    myItem[disp_food[0]][disp_food[1]] = 1
# 随机生成 坐标 食物
def build_food():
  global disp_List,disp_food,buid_food_state
  disp_food=[]
  disp_food.append(tqmath.random(0, 5))
  disp_food.append(tqmath.random(0, 6))
  if disp_food in disp_List:
    print(disp_food)
  buid_food_state = 0
while True:
  get_key()
  if state == True:
    move()
    if buid_food_state == 1:
      build_food()
    disp_tq()
    os.sleep(0.1)
  else:
    time_state = time_state + 1
    if time_state == 5:
      move()
      if buid_food_state == 1:
        build_food()
      disp_tq()
      time_state = 0
    os.sleep(0.1)

效果见视频。

【贪吃蛇4 视频演示】详见作者原贴文章

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分