123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- #!/usr/bin/env python
- #version : 2024.03.07
- #language : en
- #hardware : PI
- from maix import display
- from maix import image
- from maix import camera
- import os
- import time
- import random
- import sys
- sys.path.append("/root/")
- from CocoPi import QMI8658
- from CocoPi import BUTTON
- from maix import mjpg
- from maix import utils
- import base64
- import time
- cameraSize = True
- def CAMERATYPE():
- global cameraSize
- try:
- if os.path.exists("/etc/cameraSize.cfg"):
- cameraSize = True
- else:
- cameraSize = False
- except:
- cameraSize = False
- CAMERATYPE()
- def v831_display_show_canvas(displayShow):
- global _canvas_y,_canvas_x,ScreenOrientation,cameraSize
- CANVASSHOWIMGAGE = ""
- if ScreenOrientation:
- displayShowCanvas = image.new(size = (240, 320))
- displayShowCanvas.draw_rectangle(0,0,240,320, color=(0,0,0), thickness=-1)
- displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
- displayShowVER = displayShowCanvas.crop(0,0,240,320)
- displayShowVER = displayShowVER.rotate(-90, adjust=1)
- display.show(displayShowVER)
- else:
- displayShowCanvas = image.new(size = (320, 240))
- displayShowCanvas.draw_rectangle(0,0,320,240, color=(0,0,0), thickness=-1)
- displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
- display.show(displayShowCanvas)
- ScreenOrientation = False
- _canvas_x = 0
- _canvas_y = 0
- key_C = BUTTON(13)
- image.load_freetype("/root/preset/fonts/botPixel.ttf")
- if cameraSize==True:
- camera.camera.config(size=(320,240))
- else:
- camera.camera.config(size=(240,320))
- x = random.randrange(100, 141, 10)
- y = random.randrange(100, 141, 10)
- x1 = random.randrange(100, 141, 10)
- y1 = random.randrange(100, 141, 10)
- a = [{"x":x - 30, "y":y}, {"x":x - 20, "y":y}, {"x":x - 10, "y":y}, {"x":x, "y":y}]
- canvas = image.new(size = (320, 240))
- #-------------------------------
- #初始化色彩配置
- splash_theme_color = (255,255,255)
- splash_front_color = (60,60,60)
- fontcolor=255
- fontsize=1.4
- #开机显示
- splash = image.new(size=(320, 240),mode = "RGB")
- splash.draw_rectangle(0,0,320,240,color=splash_theme_color,thickness=-1)
- splash.draw_string(60, 48, "Gluttonous", 1.5, color=(255, 200, 0))
- splash.draw_string(120, 88, "Snake", 1.5, color=(255, 128, 64))
- display.show(splash)
- time.sleep(1)
- #初始化设置
- qmi8658=QMI8658()
- #校准
- qmi8658.calibrate()
- #标定初始方位
- initData={}
- initData["AcX"]=qmi8658.get_accel(10)["AcX"]
- initData["AcY"]=qmi8658.get_accel(10)["AcY"]
- initData["AcZ"]=qmi8658.get_accel(10)["AcZ"]
- initData["GyX"]=qmi8658.get_accel(10)["GyX"]
- initData["GyY"]=qmi8658.get_accel(10)["GyY"]
- initData["GyZ"]=qmi8658.get_accel(10)["GyZ"]
- angle_x = 0
- angle_y = 0
- score_count = 0
- direction = "RIGHT"
- isFinsh = False
- b = {"x":250, "y":250}
- c = {"x":250, "y":250}
- while True:
- angle_x = round(qmi8658.getPitchYawRollGxGyGz(initData)[0],2)
- angle_y = round(qmi8658.getPitchYawRollGxGyGz(initData)[1],2)
- canvas.clear()
- if x == x1 and y == y1:
- x1 = random.randrange(10, 311, 10)
- y1 = random.randrange(10, 231, 10)
- score_count = score_count + 1
- for j in a:
- if j.get("x") == x1 and j.get("y") == y1:
- x1 = x1 + 10
- y1 = y1 + 10
- else:
- if angle_y > 30 and direction != "DOWN":
- direction = "UP"
- y = y - 10
- b = {"x":x, "y":y}
- if x == x1 and y == y1:
- x1 = random.randrange(10, 311, 10)
- y1 = random.randrange(10, 231, 10)
- score_count = score_count + 1
- a.append({"x":x, "y":y})
- for j in a:
- if j.get("x") == x1 and j.get("y") == y1:
- x1 = x1 + 10
- y1 = y1 + 10
- elif y == -10:
- isFinsh = True
- else:
- c = a[0]
- a.pop(0)
- for k in a:
- if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
- isFinsh = True
- a.append({"x":x, "y":y})
- elif angle_y < -30 and direction != "UP":
- direction = "DOWN"
- y = y + 10
- b = {"x":x, "y":y}
- if x == x1 and y == y1:
- x1 = random.randrange(10, 311, 10)
- y1 = random.randrange(10, 231, 10)
- score_count = score_count + 1
- a.append({"x":x, "y":y})
- for j in a:
- if j.get("x") == x1 and j.get("y") == y1:
- x1 = x1 + 10
- y1 = y1 + 10
- elif y == 240:
- isFinsh = True
- else:
- c = a[0]
- a.pop(0)
- for k in a:
- if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
- isFinsh = True
- a.append({"x":x, "y":y})
- elif angle_x < -30 and direction != "RIGHT":
- direction = "LEFT"
- x = x - 10
- b = {"x":x, "y":y}
- if x == x1 and y == y1:
- x1 = random.randrange(10, 311, 10)
- y1 = random.randrange(10, 231, 10)
- score_count = score_count + 1
- a.append({"x":x, "y":y})
- for j in a:
- if j.get("x") == x1 and j.get("y") == y1:
- x1 = x1 + 10
- y1 = y1 + 10
- elif x == -10:
- isFinsh = True
- else:
- c = a[0]
- a.pop(0)
- for k in a:
- if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
- isFinsh = True
- a.append({"x":x, "y":y})
- elif angle_x > 30 and direction != "LEFT":
- direction = "RIGHT"
- x = x + 10
- b = {"x":x, "y":y}
- if x == x1 and y == y1:
- x1 = random.randrange(10, 311, 10)
- y1 = random.randrange(10, 231, 10)
- score_count = score_count + 1
- a.append({"x":x, "y":y})
- for j in a:
- if j.get("x") == x1 and j.get("y") == y1:
- x1 = x1 + 10
- y1 = y1 + 10
- elif x == 320:
- isFinsh = True
- else:
- c = a[0]
- a.pop(0)
- for k in a:
- if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
- isFinsh = True
- a.append({"x":x, "y":y})
- else:
- if direction == "UP":
- y = y - 10
- b = {"x":x, "y":y}
- if y == -10:
- isFinsh = True
- else:
- c = a[0]
- a.pop(0)
- for k in a:
- if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
- isFinsh = True
- a.append({"x":x, "y":y})
- elif direction == "DOWN":
- y = y + 10
- b = {"x":x, "y":y}
- if y == 240:
- isFinsh = True
- else:
- c = a[0]
- a.pop(0)
- for k in a:
- if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
- isFinsh = True
- a.append({"x":x, "y":y})
- elif direction == "LEFT":
- x = x - 10
- b = {"x":x, "y":y}
- if x == -10:
- isFinsh = True
- else:
- c = a[0]
- a.pop(0)
- for k in a:
- if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
- isFinsh = True
- a.append({"x":x, "y":y})
- else:
- x = x + 10
- b = {"x":x, "y":y}
- if x == 320:
- isFinsh = True
- else:
- c = a[0]
- a.pop(0)
- for k in a:
- if k.get("x") == b.get("x") and k.get("y") == b.get("y"):
- isFinsh = True
- a.append({"x":x, "y":y})
- if isFinsh == True:
- canvas.clear()
- canvas.draw_string(60,40, "Gameover!", scale = 1.5, color = (255,255,255) , thickness = 1)
- canvas.draw_string(85,90, "Score:", scale = 1.5, color = (255,0,0) , thickness = 1)
- canvas.draw_string(210,90, (str(score_count)), scale = 1.5, color = (255,0,0) , thickness = 1)
- canvas.draw_string(30,150, "Press", scale = 1, color = (255,255,255) , thickness = 1)
- canvas.draw_string(105,150, "C", scale = 1, color = (255,255,255) , thickness = 1)
- canvas.draw_string(125,150, "TO", scale = 1, color = (255,255,255) , thickness = 1)
- canvas.draw_string(165,150, "Restart!", scale = 1, color = (255,255,255) , thickness = 1)
- v831_display_show_canvas(canvas)
- if (key_C.is_pressed()):
- while key_C.is_pressed():
- time.sleep(0.001)
- angle_x = 0
- angle_y = 0
- score_count = 0
- direction = "RIGHT"
- isFinsh = False
- x = random.randrange(100, 141, 10)
- y = random.randrange(100, 141, 10)
- x1 = random.randrange(100, 141, 10)
- y1 = random.randrange(100, 141, 10)
- a = [{"x":x - 30, "y":y}, {"x":x - 20, "y":y}, {"x":x - 10, "y":y}, {"x":x, "y":y}]
- b = {"x":250, "y":250}
- c = {"x":250, "y":250}
- else:
- canvas.draw_rectangle(c.get("x"),c.get("y"),10, 10, color=(0,0,0), thickness=-1)
- canvas.draw_rectangle(x1,y1, x1+10,y1+ 10, color=(255,255,255), thickness=-1)
- for i in a:
- x = i.get("x")
- y = i.get("y")
- canvas.draw_rectangle(x,y, x+10,y+ 10, color=(255,255,255), thickness=-1)
- v831_display_show_canvas(canvas)
- if isFinsh == False:
- time.sleep(0.2)
- else:
- pass
|