パーソナルブログメモリ

a = [1, 1]
for _ in "*" * 999: a += [sum(a[-2:])]
print(a)

ゲームソフトを3本つくる

2018-11-20 | DMB-8836(自作ゲーム機)
3本作ってみました。ドットマトリックス、電子ブザー、ボタンの制御はライブラリにして外だしが今後の課題でしょうか?
自分でもなぜソフトを作っているのか、どこに向かって迷走しているのかよくわかりません。

ライフワール

上下左右で空間を動きAボタンで星をおく。
星はライフゲームと同じ法則で誕生し生存し、条件によっては消滅する。
LUDRの同時押しで空間は終わる



ブリリアントスター

お姫様からの使者が辺境の惑星に住む自分の元へ、
なんでも、極秘エージェントが父親だったらしく
息子の私にもなんらかの力が備わっているらしい。
(後日、父親に聞いてみたが、さすが極秘エージェント口は割りませんでした。)
ということで銀河を救うべく10個のブリリアントスターを探す旅にでたのでした。

宇宙船はLRで旋回する。最大10個のブリリアントスターをすべて見つけ出すと
銀河は救われるらしい。
LUDRの同時おしで夢は終わる。



3DMAZE

一体何度はまるのだろうか、気がつくとそこにいた。
LRで旋回、Uで前進、Dで後退
左上からスタート、右下がゴール

LUDRの同時押すと苦悩は終わる

この開発、ずっとPython3 IDELを使っていたのですが、インデントではまります。端末でnanoを使った方が楽そう。



# **** lifeworl.py ****

import time
import RPi.GPIO as GPIO
import threading
import copy
import pigpio as PIGP
import random


#RPiGPIO init
GPIO.setmode(GPIO.BCM)

#pigpio buzzer init
pigp = PIGP.pi()
soundpin = [13,18,19]

for p in soundpin:
  pigp.set_mode(p, PIGP.OUTPUT)
  pigp.hardware_PWM(p, 0, 0)

#buzzer
sstop=[0,0,0]
snum=0
sarr=[]

#onkai
hz=[0]
for i in range(120):
  hz+=[int(27.5*(1.05946364**i))]

#start sound
try:
  for j in range(1):
    for i in range(400,1000,10):
      for p in soundpin:
        pigp.hardware_PWM(p, i, 200000)
      time.sleep(0.01)
  for i in range(120):
    for p in soundpin:
      pigp.hardware_PWM(p, hz[i] ,500000)
    time.sleep(0.01)
except KeyboardInterrupt:
  pass

#moniter
vx=0
vy=0
vw=32
vh=24
vstop=0#1 thread stop
vram=[[0 for i in range(vw)] for j in range(vh)]

#dotmatrix GPIO pin
high=[2,3,4,17,27,22,10,9]
low=[24,25,8,7,12,16,20,21]

#button GPIO pin
for pin in [11,5,6,26,14,23]:
  GPIO.setup(pin,GPIO.IN,pull_up_down=GPIO.PUD_UP)

#dotmatrix GPIO pin
for pin in [2,3,4,17,27,22,10,9,24,25,8,7,12,16,20,21]:
  GPIO.setup(pin, GPIO.OUT) 

#### graphic ####
def dbox(x,y,w,h,n):
  for lx in range(x,x+w):
    for ly in range(y,y+h):
      dprint(lx,ly,n)
          
def dprint(x,y,n):
  global vram,vw,vh
  if 0>x or 0>y or x>vw-1 or y>vh-1:return
  vram[y][x]=n

def monitor():
  global vram,vx,vy,vstop,high,low
  while True:
    for t in [0,4,8,1,3,5,7,9,2,6]:
      for y in range(8):
        for x in range(8):
          if vstop==1:return
          if vram[y+vy][x+vx]>t:
            pinSet(high[x], 1)
          else:
            pinSet(high[x], 0)
            pinSet(low[y], 0)
            time.sleep(0.00001)
            pinSet(low[y], 1)

def pinSet(pin_num,sw):
  if pin_num in [2,3,4,17,27,22,10,9,24,25,8,7,12,16,20,21]:
    if sw==0:
      GPIO.output(pin_num, GPIO.LOW)
    else:
      GPIO.output(pin_num, GPIO.HIGH)

#### sound ####
def play(num,arr):
  global snum,sarr,sstop
  snum=num
  sarr=copy.deepcopy(arr)
  sstop[num]=1
  time.sleep(0.21)
  sstop[num]=0
  thread = threading.Thread(target=playsub)
  thread.start()

def  playsub():
  global snum,sarr,sstop,soundpin
  n=snum
  arr=copy.deepcopy(sarr)
  for i in arr:
    if sstop[n]==1:
      pigp.hardware_PWM(soundpin[n], 0 ,0)
      break
    pigp.hardware_PWM(soundpin[n], hz[i] ,500000)
    time.sleep(0.2)
  pigp.hardware_PWM(soundpin[n], 0 ,0)

#### button ####
def button():
  r=""
  for b,p in zip("LUDRAB",[11,5,6,26,14,23]):
    if GPIO.input(p)==0:r+=b
  return r

#monitor start
thread_1 = threading.Thread(target=monitor)
thread_1.start()


#game
world=[[0 for i in range(100)] for j in range(100)]
cx=10
cy=10

def nextG():
  newWorld=[[0 for i in range(100)] for j in range(100)]
  for x in range(1,99):
    for y in range(1,99):
      c=0
      for x1 in  [x-1,x,x+1]:
        for y1 in  [y-1,y,y+1]:
          c+=world[y1][x1]
      if world[y][x]==1:
        if c==3 or c==4:
          newWorld[y][x]=1
      else:
        if c==3:
          newWorld[y][x]=1
  return newWorld

def writeWorld():
  for x in range(8):
    for y in range(8):
      if world[cy-4+y][cx-4+x]==1:
        vram[y][x]=random.randint(3,9)
      else:
        vram[y][x]=0

tn=0
while True:
  tn+=1
  if "L" in button():
    cx=cx-1
    if 10>cx:cx=90 
  if "U" in button():
    cy=cy-1
    if 10>cy:cy=90
  if "D" in button():
    cy=cy+1
    if cy>90:cy=10
  if "R" in button():
    cx=cx+1
    if cx>90:cx=10
  if "A" in button():
    for i in range(5):
      world[cy+random.randint(-3,3)][cx+random.randint(-3,3)]=1
  if "B" in button():
    play(1,[j for j in range(50,70)])
  if button()=="LUDR":
    vstop=1
    time.sleep(0.01)
    break
  if tn%5==0:
    world=nextG()
    writeWorld()
  time.sleep(0.01)

#end clear
GPIO.cleanup()
sstop=[1,1,1]
time.sleep(0.3)
for p in soundpin:
  pigp.hardware_PWM(p, 0, 0)
  pigp.set_mode(p, PIGP.INPUT)
pigp.stop()




# **** bstar.py ****

import time
import RPi.GPIO as GPIO
import threading
import copy
import pigpio as PIGP
import random


#RPiGPIO init
GPIO.setmode(GPIO.BCM)

#pigpio buzzer init
pigp = PIGP.pi()
soundpin = [13,18,19]

for p in soundpin:
  pigp.set_mode(p, PIGP.OUTPUT)
  pigp.hardware_PWM(p, 0, 0)

#buzzer
sstop=[0,0,0]
snum=0
sarr=[]

#onkai
hz=[0]
for i in range(120):
  hz+=[int(27.5*(1.05946364**i))]

#start sound
try:
  for j in range(1):
    for i in range(400,1000,10):
      for p in soundpin:
        pigp.hardware_PWM(p, i, 200000)
      time.sleep(0.01)
  for i in range(120):
    for p in soundpin:
      pigp.hardware_PWM(p, hz[i] ,500000)
    time.sleep(0.01)
except KeyboardInterrupt:
    pass

#moniter
vx=0
vy=0
vw=32
vh=24
vstop=0#1 thread stop
vram=[[0 for i in range(vw)] for j in range(vh)]

#dotmatrix GPIO pin
high=[2,3,4,17,27,22,10,9]
low=[24,25,8,7,12,16,20,21]

#button GPIO pin
for pin in [11,5,6,26,14,23]:
  GPIO.setup(pin,GPIO.IN,pull_up_down=GPIO.PUD_UP)

#dotmatrix GPIO pin
for pin in [2,3,4,17,27,22,10,9,24,25,8,7,12,16,20,21]:
  GPIO.setup(pin, GPIO.OUT) 

#### graphic ####
def dbox(x,y,w,h,n):
  for lx in range(x,x+w):
    for ly in range(y,y+h):
      dprint(lx,ly,n)
          
def dprint(x,y,n):
  global vram,vw,vh
  if 0>x or 0>y or x>vw-1 or y>vh-1:return
  vram[y][x]=n

def monitor():
  global vram,vx,vy,vstop,high,low
  while True:
    for t in [0,4,8,1,3,5,7,9,2,6]:
      for y in range(8):
        for x in range(8):
          if vstop==1:return
          if vram[y+vy][x+vx]>t:
             pinSet(high[x], 1)
          else:
             pinSet(high[x], 0)
        pinSet(low[y], 0)
        time.sleep(0.00001)
        pinSet(low[y], 1)

def pinSet(pin_num,sw):
  if pin_num in [2,3,4,17,27,22,10,9,24,25,8,7,12,16,20,21]:
    if sw==0:
      GPIO.output(pin_num, GPIO.LOW)
    else:
      GPIO.output(pin_num, GPIO.HIGH)

#### sound ####
def play(num,arr):
  global snum,sarr,sstop
  snum=num
  sarr=copy.deepcopy(arr)
  sstop[num]=1
  time.sleep(0.21)
  sstop[num]=0
  thread = threading.Thread(target=playsub)
  thread.start()

def  playsub():
  global snum,sarr,sstop,soundpin
  n=snum
  arr=copy.deepcopy(sarr)
  for  i in arr:
    if sstop[n]==1:
      pigp.hardware_PWM(soundpin[n], 0 ,0)
      break
    pigp.hardware_PWM(soundpin[n], hz[i] ,500000)
    time.sleep(0.2)
  pigp.hardware_PWM(soundpin[n], 0 ,0)

#### button ####
def button():
  r=""
  for b,p in zip("LUDRAB",[11,5,6,26,14,23]):
    if GPIO.input(p)==0:r+=b
  return r

#monitor start
thread_1 = threading.Thread(target=monitor)
thread_1.start()


#game
world=[[0 for i in range(100)] for j in range(100)]
back=[[0 for i in range(100)] for j in range(100)]
cx=50
cy=50
hx=[-1,0,1,1,1,0,-1,-1]
hy=[1,1,1,0,-1,-1,-1,0]
cr=5

for i in range(10):
  world[random.randint(30,70)][random.randint(30,70)]=1
for i in range(2000):
  back[random.randint(0,99)][random.randint(0,99)]=1


def writeWorld():
  for x in range(8):
    for y in range(8):
      if world[cy-3+y][cx-3+x]==1:
        vram[y][x]=9
      elif back[cy-3+y][cx-3+x]==1:
        vram[y][x]=1
      else:
        vram[y][x]=0

def hit():
  global cx,cy,world
  if world[cy][cx]==1:
   play(2,[j for j in range(40,80,2)])
   world[cy][cx]=0
        
  if world[cy+1][cx]==1:
   play(2,[j for j in range(50,90,2)])
   world[cy+1][cx]=0

  if world[cy][cx+1]==1:
   play(2,[j for j in range(45,85,2)])
   world[cy][cx+1]=0

  if world[cy+1][cx+1]==1:
   play(2,[j for j in range(35,75,2)])
   world[cy+1][cx+1]=0

def megahit():
  global cx,cy,world
  for x1 in range(cx-8,cx+8):
    for y1 in range(cy-8,cy+8):
      if world[y1][x1]==1:
        world[y1][x1]=0

tn=0
while True:
  tn+=1
  if "L" in button():
    if cr<7:cr=cr+1
    else:cr=0
  if "R" in button():
    if cr>0:cr=cr-1
    else:cr=7
  cx+=hx[cr]
  cy+=hy[cr]
  if button()=="UA":megahit()#testyou
  if 20>cx:cx=80
  if 20>cy:cy=80
  if cx>80:cx=20
  if cy>80:cy=20
  if "A" in button():
    play(1,[j for j in range(40,70)])
  if "B" in button():
    play(1,[j for j in range(70,40,-1)])
  if sum([sum(world[i]) for i in range(30,71)])==0:
    play(0,[j for j in range(40,120)])
    play(1,[j for j in range(42,120)])
    play(2,[j for j in range(44,120)])
    time.sleep(5)
    vstop=1
    time.sleep(0.02)
    break
  hit()
  if button()=="LUDR":
    vstop=1
    time.sleep(0.01)
    break
  writeWorld()
  time.sleep(0.05)

#end clear
GPIO.cleanup()
sstop=[1,1,1]
time.sleep(0.3)
for p in soundpin:
  pigp.hardware_PWM(p, 0, 0)
  pigp.set_mode(p, PIGP.INPUT)
pigp.stop()




# **** 3dmaze.py ****

import time
import RPi.GPIO as GPIO
import threading
import copy
import pigpio as PIGP
import random

#RPiGPIO init
GPIO.setmode(GPIO.BCM)

#pigpio buzzer init
pigp = PIGP.pi()
soundpin = [13,18,19]

for p in soundpin:
  pigp.set_mode(p, PIGP.OUTPUT)
  pigp.hardware_PWM(p, 0, 0)

#buzzer
sstop=[0,0,0]
snum=0
sarr=[]

#onkai
hz=[0]
for i in range(120):
  hz+=[int(27.5*(1.05946364**i))]

#start sound
try:
  for j in range(1):
    for i in range(400,1000,10):
      for p in soundpin:
        pigp.hardware_PWM(p, i, 200000)
      time.sleep(0.01)
  for i in range(120):
    for p in soundpin:
      pigp.hardware_PWM(p, hz[i] ,500000)
    time.sleep(0.01)
except KeyboardInterrupt:
  pass

#moniter
vx=0
vy=0
vw=32
vh=24
vstop=0#1 thread stop
vram=[[0 for i in range(vw)] for j in range(vh)]

#dotmatrix GPIO pin
high=[2,3,4,17,27,22,10,9]
low=[24,25,8,7,12,16,20,21]

#button GPIO pin
for pin in [11,5,6,26,14,23]:
  GPIO.setup(pin,GPIO.IN,pull_up_down=GPIO.PUD_UP)

#dotmatrix GPIO pin
for pin in [2,3,4,17,27,22,10,9,24,25,8,7,12,16,20,21]:
  GPIO.setup(pin, GPIO.OUT) 

#### graphic ####
def dbox(x,y,w,h,n):
  for lx in range(x,x+w):
    for ly in range(y,y+h):
      dprint(lx,ly,n)
          
def dprint(x,y,n):
  global vram,vw,vh
  if 0>x or 0>y or x>vw-1 or y>vh-1:return
  vram[y][x]=n

def monitor():
  global vram,vx,vy,vstop,high,low
  while True:
    for t in [0,4,8,1,3,5,7,9,2,6]:
      for y in range(8):
        for x in range(8):
          if vstop==1:return
          if vram[y+vy][x+vx]>t:
            pinSet(high[x], 1)
          else:
            pinSet(high[x], 0)
        pinSet(low[y], 0)
        time.sleep(0.00001)
        pinSet(low[y], 1)

def pinSet(pin_num,sw):
  if pin_num in [2,3,4,17,27,22,10,9,24,25,8,7,12,16,20,21]:
    if sw==0:
      GPIO.output(pin_num, GPIO.LOW)
    else:
      GPIO.output(pin_num, GPIO.HIGH)

#### sound ####
def play(num,arr):
  global snum,sarr,sstop
  snum=num
  sarr=copy.deepcopy(arr)
  sstop[num]=1
  time.sleep(0.2)
  sstop[num]=0
  thread = threading.Thread(target=playsub)
  thread.start()

def  playsub():
  global snum,sarr,sstop,soundpin
  n=snum
  arr=copy.deepcopy(sarr)
  for i in arr:
    if sstop[n]==1:
      pigp.hardware_PWM(soundpin[n], 0 ,0)
      break
    pigp.hardware_PWM(soundpin[n], hz[i] ,500000)
    time.sleep(0.2)
  pigp.hardware_PWM(soundpin[n], 0 ,0)

#### button ####
def button():
  r=""
  for b,p in zip("LUDRAB",[11,5,6,26,14,23]):
    if GPIO.input(p)==0:r+=b
  return r

#monitor start
thread_1 = threading.Thread(target=monitor)
thread_1.start()


#game
world=[[0 for i in range(22)] for j in range(22)]
cx=4
cy=4
cd=2
cp=[]
hx=[0,1,0,-1]
hy=[1,0,-1,0]
size=8# 10,12,14,16
epos=4+size-2

mzs=[]
mzs+=[(2,3),(1,3),(0,3),(-1,3),(-2,3), (1,2),(0,2),(-1,2), (1,1),(0,1),(-1,1), (1,0),(-1,0), (0,-1)]#0
mzs+=[(3,-2),(3,-1),(3,0),(3,1),(3,2), (2,-1),(2,0),(2,1), (1,-1),(1,0),(1,1), (0,-1),(0,1), (-1,0)]#1
mzs+=[(-2,-3),(-1,-3),(0,-3),(1,-3),(2,-3), (-1,-2),(0,-2),(1,-2), (-1,-1),(0,-1),(1,-1), (-1,0),(1,0), (0,1)]#2
mzs+=[(-3,2),(-3,1),(-3,0),(-3,-1),(-3,-2), (-2,1),(-2,0),(-2,-1), (-1,1),(-1,0),(-1,-1), (0,1),(0,-1), (1,0)]#3

def any(arr):
  return random.randint(0,len(arr)-1)

def createMaze():
  global world,size
  for x in range(3,size+3+1):
    for y in range(3,size+3+1):
      world[y][x]=1
  world[4][4]=0
  sp=[(4,4)]

  while len(sp)>0:
    p=any(sp)
    sx,sy=sp[p]
    md=[]
    for d in range(4):
      nx=sx+hx[d]*2
      ny=sy+hy[d]*2
      if world[ny][nx]==1:
        md+=[d]
    if len(md)==0:
      del sp[p]
      continue
    d=md[any(md)]
    nx=sx+hx[d]*2
    ny=sy+hy[d]*2
    sp+=[(nx,ny)]
    world[sy+hy[d]][sx+hx[d]]=0
    world[ny][nx]=0
  for w in world:
    print(w)

def writeMaze(p):
  writePatt(p)

def setPatt():
  global cx,cy,cd
  p=[]
  for i in range(14):
    dx,dy=mzs[14*cd+i]
    p+=[world[cy+dy][cx+dx]]
  return p

def writePatt(p):
  global cd
  dbox(0,0,8,8,0)
  if cd==2:dbox(3,0,2,1,9)
  
  if p[0]==1:dbox(0,3,1,2,2)
  if p[1]==1:dbox(1,3,2,2,2)
  if p[2]==1:dbox(3,3,2,2,2)
  if p[3]==1:dbox(5,3,2,2,2)
  if p[4]==1:dbox(7,3,1,2,2)
    
  if p[5]==1:
    dbox(0,2,2,4,4)
    dbox(2,3,1,2,3)    
  if p[7]==1:
    dbox(6,2,2,4,4)
    dbox(5,3,1,2,3)
  if p[6]==1:dbox(2,2,4,4,4)
    
  if p[8]==1:
    dbox(0,1,1,6,6)    
    dbox(1,2,1,4,5)    
  if p[10]==1:
    dbox(7,1,6,6,6)
    dbox(6,2,1,4,5)    
  if p[9]==1:dbox(1,1,6,6,6)

  if p[11]==1:dbox(0,0,1,8,9)
  if p[12]==1:
    dbox(7,0,1,8,9)

createMaze()

while True:
  if "L" ==button():
    if cd==3:cd=1
    else:cd+=1
    play(1,[j for j in [40,42,40] ])    
  if "R"==button():
    if cd==0:cd=3
    else:cd-=1
    play(2,[j for j in [40,42,40] ])
  if "U" ==button():
    if cp[9]==1:
      play(1,[j for j in [20,30,20,30] ])
    else:
      play(1,[j for j in [40,42,44,46] ])
      cx+=hx[cd]
      cy+=hy[cd]
  if "D"==button():
    if cp[13]==1:
      play(1,[j for j in [20,30,20,30] ])
    else:
      play(1,[j for j in [46,44,42,40] ])
      bd=cd+2
      if bd>3:bd-=4
      cx+=hx[bd]
      cy+=hy[bd]

  if "A"==button():
    print(cx,cy,cd)
  if cx==epos and cy==epos:
    play(0,[j for j in range(40,80) ])
    time.sleep(1)
    play(1,[j for j in range(40,80) ])
    time.sleep(1)
    play(2,[j for j in range(40,80) ])
    time.sleep(3)
    vstop=1
    time.sleep(0.01)
    break    
  if button()=="LUDR":
    vstop=1
    time.sleep(0.01)
    break
  cp=setPatt()
  writeMaze(cp)
  time.sleep(0.2)

#end clear
GPIO.cleanup()
sstop=[1,1,1]
time.sleep(0.3)
for p in soundpin:
  pigp.hardware_PWM(p, 0, 0)
  pigp.set_mode(p, PIGP.INPUT)
pigp.stop()



最新の画像もっと見る

コメントを投稿

ブログ作成者から承認されるまでコメントは反映されません。