ombre.py

Created by antarctus

Created on May 08, 2023

3.93 KB

Le jeu ombre est enfin disponible ! Déplacer vous dans un labyrinthe plongé dans les ténèbres ! Et tenter d’atteindre chaque balises afin d’illuminer la zone !
Utiliser les flèches pour vous déplacer.
Utilise la technique de génération de labyrinthe de mod_maze


from kandinsky import *
from time import *
from ion import *
from math import *
from random import *
import micropython as mp

mp.kbd_intr(-1)

pos=(1,1)
last_pos=(1,1)
stats={"time":0,"step":0}

WIDTH,HEIGHT=31,21
HALO_COLORS=[(0,200,200),(250,)*3,(232,196,92),(211,132,65),(0,)*3]

nb_beacons=3
BEACONS_COLORS=[(255,0,0),(0,255,0),(0,0,255),(255,255,0),(0,255,255),(255,0,255)]
activated_beacons=[False,]*nb_beacons
beacons_pos=[]

# CHEAT CODES
NO_WALL=False
EXTRA_LIGHT=False

if EXTRA_LIGHT:
  HALO_COLORS[-1]=HALO_COLORS[-2]

def maze(WIDTH,HEIGHT):
  map_=[[(y%2)*(x%2)*(x//2+y//2*(WIDTH//2)+1) for y in range(HEIGHT)] for x in range(WIDTH)]

  def replace_all(n1,n2):
    nonlocal map_
    for l in map_:
      if n1 in l:
        for i in range(len(l)):
          if l[i]==n1:
            l[i]=n2

  def is_diff(x,y):
    if y%2:
      return (map_[x-1][y],map_[(x+1)%WIDTH][y])
    return (map_[x][y-1],map_[x][(y+1)%HEIGHT])

  poss=[(x*2+(y%2==0),y) for y in range(1,HEIGHT-1) for x in range(y%2,(WIDTH-1)//2)]
  for j in range(len(poss)):
    i=randint(0,len(poss)-1)
    x,y=poss[i]
    d=is_diff(x,y)
    if d[0]!=d[1]:
      replace_all(max(d),min(d))
      map_[x][y]=min(d)
    del poss[i]
  return map_

def zone(x1,y1,x2,y2):
  for x_ in range(x1,x2):
    for y_ in range(y1,y2):
      yield (x_,y_,map_[x_][y_])

def gen_points_coords():
  while True:
    p=(randint(1,WIDTH-2),randint(1,HEIGHT-2))
    if p in beacons_pos:
      continue
    if map_[p[0]][p[1]]==1:
      continue
    if not any([map_[p[0]+[-1,1,0,0][i]][p[1]+[0,0,1,-1][i]] for i in range(4)]):
      continue
    break
  return p

def activate():
  global activated_beacons
  for i in range(len(beacons_pos)):
    if pos in [(beacons_pos[i][0]+[-1,1,0,0][j],beacons_pos[i][1]+[0,0,-1,1][j]) for j in range(4)]:
      activated_beacons[i]=True
      if all(activated_beacons):
        fill_rect(0,0,320,222,(0,)*3)
        for x_,y_,v in zone(0,0,WIDTH,HEIGHT):
          fill(x_,y_,(v*200,)*3)
          if (x_,y_) in beacons_pos:
            fill(x_,y_,BEACONS_COLORS[beacons_pos.index((x_,y_))])

def move():
  global pos,activated_beacons,last_pos
  last_pos=pos
  nx,ny=pos[0]+(keydown(3)-keydown(0)),pos[1]+(keydown(2)-keydown(1))
  if map_[nx][ny] in [(1,),(0,1)][NO_WALL] and pos!=(nx,ny):
    pos=(nx,ny)
    if not all(activated_beacons):
      activate()

def fill(x,y,c):
  fill_rect(5+x*10,6+y*10,10,10,c)

def color_of(x,y):
  possible_colors=[]
  if map_[x][y]==0:
    return (0,)*3
  if max(abs(pos[0]-x),abs(pos[1]-y))<len(HALO_COLORS):
    possible_colors+=[HALO_COLORS[max(abs(pos[0]-x),abs(pos[1]-y))]]
  for i in range(len(beacons_pos)):
    if activated_beacons[i]:
      dist_to_beacon=max(abs(beacons_pos[i][0]-x),abs(beacons_pos[i][1]-y))
      if dist_to_beacon<3:
        possible_colors+=[tuple(BEACONS_COLORS[i][j]*(4-dist_to_beacon)//3 for j in range(3))]
    if i==(x,y):
      possible_colors+=[BEACONS_COLORS[i]]
  return tuple(sum(map(lambda x: x[c_id],possible_colors))//len(possible_colors) for c_id in range(3))

def draw_move():
  if all(activated_beacons):
    fill(last_pos[0],last_pos[1],[(0,)*3,(200,)*3][map_[last_pos[0]][last_pos[1]]])
    fill(pos[0],pos[1],(255,191,63))
    return
  for x_,y_,v in zone(max(pos[0]-4,0),max(pos[1]-4,0),min(WIDTH,pos[0]+5),min(HEIGHT,pos[1]+5)):
    if v:
      fill(x_,y_,color_of(x_,y_))

map_=maze(WIDTH,HEIGHT)

for i in range(nb_beacons):
   beacons_pos+=[gen_points_coords()]

fill_rect(0,0,320,222,(0,)*3)
for i in range(nb_beacons):
  fill(beacons_pos[i][0],beacons_pos[i][1],BEACONS_COLORS[i])
draw_move()

while not keydown(KEY_BACK):
  t=monotonic()
  move()
  if last_pos!=pos:
    draw_move()
    stats["step"]+=1
  sleep(max(0,0.1+t-monotonic()))
  stats["time"]+=0.1
  if stats["time"]>10 and stats["step"]/stats["time"]<1:
    draw_string("{:^32}".format(["walk on the lighted path","switch on all beacons","You are the center of the light"][int(stats["time"]/10-1)%3]),0,206,(255,)*3,(0,)*3)

print(stats)

#dédicace à omby