labyrinth.py

Created by cent20

Created on December 15, 2024

4.93 KB


from kandinsky import *
from random import randint, choice
from time import sleep
from math import sqrt
from ion import keydown

carte = [[666] * 32 if i in {0, 19} else [666]+[0 for _ in range(30)]+[666] for i in range(20)]
# https://coolors.co/ffb531-9471de-2ac5f2-00a676-ee7674
# (255, 181, 49)  (148, 113, 222) (42, 197, 242) (0, 166, 118) (163, 22, 33)
# Orange          violet          bleu            vert          rouge
col = {"ui":(148, 113, 222), "p":(42, 142, 242), "h":(163, 22, 33), "w":(0, 166, 118), "l":(142,142,142)}


player = {"x":1, "y":5, "vie":3, "level":False}
game = {"L":1, "R":6, "E":1}

def wait(keys=(0,1,2,3,4)):  # input keyboard
    while True:
        for k in keys:
            if keydown(k):
                while keydown(k): True  # Fonction anti-rebond
                return k

def miner():
    for y in range(2,19,1):
        for x in range(2,31,1):
            carte[y][x] = 0     
    n = 21*game["L"]
    """Place n bombes sur la carte, win condition, start"""
    while n>0:
        y, x = randint(1,18), randint(2,29)
        if carte[y][x]!=666:
            carte[y][x]=666
            n -= 1
    y , x = randint(1,18), 1
    player["x"], player["y"], = 1, y
    carte[y][x]=0
    x, y = randint(10,28), choice((0,19))
    if x == 31:
        y = randint(3,17)
    carte[y][x]=42
    print(x,y)
    draw_string(str(x)+" "+str(y), 0, 0, (242,)*3, (148, 113,222))
    draw_string(str(x)+" "+str(y), 180, 0, (242,)*3, (148, 113,222))

def count(y,x):
    """Nombre de bombe dans le voisinage"""
    b = 0
    for i in range(-1,2,1):
        for j in range(-1,2,1):
            if x+i<0 or x+i>31 or y+j<0 or y+j>19:
                b += 1
            if 0<=x+i<=31 and 0<=y+j<=19 and carte[y+j][x+i]==666:
                b += 1    
    return b

def color(y,x):
    if y in {0, 19}:        
        fill_rect(x*10,y*10,10,10,(0,0,0))
    elif x in {0, 31}:
        fill_rect(x*10,y*10,10,10,(255,255,255))
        if y in {1, 18}:
            fill_rect(x*10,y*10,10,10,(192,192,192))
    else:
        fill_rect(x*10,y*10,10,10,(240-48*count(y,x),240-48*count(y,x),240-48*count(y,x)))     

def grille():
    """debugage only"""
    for y in range(20):
        for x in range(32):
            color(y,x)

def ui():
    fill_rect(0, 200, 320, 22, (148, 113, 222))
    draw_string( "nsi.xyz/labyrinth", 0, 202, (242,)*3, (148, 113,222))
    txt = ""
    for k in game.keys():
        txt += k+str(game[k])+" "
    draw_string(txt, 270-10*len(txt), 202, (242,)*3, (148, 113,222))
    heart("h")
    draw_string(str(player["vie"]), 320-10*len(str(player["vie"])), 202, (242,)*3, (148, 113,222))

def heart(h="h",n=1):
    x,y = 0,0
    for i, j in enumerate((1,3,2,3,1,4,2,24,1,8,3,6,5,4,7,2,4)): #heart
        while j>0:
            c = col["ui"] if i % 2 == 0 else col[h]
            fill_rect(295-10*len(str(player["vie"]))+(x%10)*2,203+y*2,2,2,c)
            x,y,j = x+1, (x+1)//10, j-1
    

def go(y,x):
    """Augmente la couleur de chaque case et deplace le joueur"""
    print(player["x"], player["y"], x, y, carte[y][x])
    player["x"], player["y"] = x, y
    if x<0 or x>31 or y<0 or y>19 or carte[y][x] == 666:        
        return loose()
    if carte[y][x] == 42:        
        return win()
        
    radar()
    for x0 in range(32):
        for y0 in range(20):
            c = max(0, get_pixel(10*x0,10*y0)[0]-24)
            fill_rect(x0*10, y0*10, 10, 10, (c,c,c))
    
def radar(p=1):
    for x in range(-p,p+1,1):
        for y in range(-p,p+1,1):
            if x == 0 and y == 0:
                fill_rect(player["x"]*10, player["y"]*10, 10, 10, col["p"])
            elif sqrt(abs(x)**2+abs(y)**2) <=p and player["y"]+y<20:
                c = 216-24*count(player["y"]+y,player["x"]+x)
                fill_rect((player["x"]+x)*10, (player["y"]+y)*10, 10, 10, (c,c,c))
  
def loose():
    player["vie"] -= 1
    print("Boom", player, carte[player["y"]][player["x"]])
    player["level"] = True

def win():
    print("WIN", player, carte[player["y"]][player["x"]])
    player["vie"] += 1
    player["level"] = True
    game["L"] += 1
    game["E"] += 1

def play():
    pass
    

def emp(p=1):
    for x in range(-p,p+1,1):
        for y in range(-p,p+1,1):
            xe = player["x"]+x
            ye = player["y"]+y
            if xe > 0 and ye > 0 and xe < 31 and ye < 19 and carte[ye][xe] != 42:
               carte[ye][xe] = 0
    radar(game["R"]+2)
    
def init():
    fill_rect(0,0,320,200,(0,0,0))
    n = game["L"]
    game["R"] = max(2,5-n//2) # Radar range
    #game["B"] = 21*game["L"] # Bomb nb
    #game["B"] = 15
    game["E"] = min(1+n, game["E"]+1) # EMP nb
    miner()
    ui()
    radar(game["R"])

while player["vie"]:
    player["level"] = False
    init()
    
    while not player["level"]:
        k = wait()
        if k == 4:
           emp(2)
        else:
            player["x"], player["y"] = player["x"]-1*(k==0)+1*(k==3), player["y"]-1*(k==1)+1*(k==2)
            go(player["y"],player["x"])
        radar(game["R"])

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.