escape1.py

Created by cent20

Created on January 12, 2025

7.46 KB

Version 1 : rame …


from kandinsky import *

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



carte = [[0] * 32 for i in range(20)] # 640 tiles to explore
# 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 = {"b":(248,248,248),"ui":(148, 113, 222), "p":(42, 142, 242), "h":(242, 160, 176), "w":(42, 242, 178), "l":(163,22,33)}
player = {"x":1, "y":5, "vie":3, "level":False}
game = {"L":1, "R":6, "E":0}
w = {"x":0, "y":0, "e":0, "v":0}

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

def reset():
    for y in range(0,20,1):
        for x in range(0,32,1):
            if x==0 or x==31 or y==0 or y==19 :
                carte[y][x] = 666
            else:
                carte[y][x] = 0

def mine():
    reset()
    """Place n bombes sur la carte, win condition, start"""
    m=22
    while m>0:
        y, x = randint(2,18), randint(2,29)
        if carte[y][x]!=666:
            carte[y][x]=666
            m -= 1
    n = 10*game["L"]- 30
    while n>0:
        y, x = randint(1,18), randint(2,29)
        if carte[y][x]!=666 and count(y,x)>0:
            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,42), choice((0,19))
    if x >= 28:
        x, y = 31, randint(3,17)
    carte[y][x]=42
    w["x"], w["y"] = x, y

def bonus():
    w["e"], w["v"] = randint(0,5-game["L"]//10), randint(0,5-game["L"]//8)
    t,e,v = 0, w["e"], w["v"]
    while t<1000 and (e>0 or v>0):
        y, x = randint(1,18), randint(2,29)
        if e>0 and count(y,x)==0:
            carte[y][x]=2
            e-=1
        elif v>0 and count(y,x)==0:
            carte[y][x]=1
            v-=1
    w["e"], w["v"] = w["e"]-e, w["v"]-v 

def count(y,x):
    """Number of bombs in the neighborhood"""
    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 grille():
    """debug only"""
    return None
    for y in range(20):
        for x in range(32):
            if carte[y][x]==666:
                fill_rect(x*10,y*10,10,10,col["l"])

def ui():
    fill_rect(0, 200, 320, 22, (148, 113, 222))
    draw_string( "nsi.xyz/escape", 10, 202, (242,)*3, (148, 113,222))
    stats()
    draw_heart(27,20.7)    
    
def stats():
    txt = ""
    for k in game.keys():
        txt += k+str(game[k])+" "
    draw_string(txt, 270-10*len(txt), 202, col["b"], col["ui"])
    draw_string(str(player["vie"]), 285, 202, col["b"], col["ui"])

def draw_heart(x,y):
    """heart Pixel Art 10x10"""
    xi,yi = 0,0
    for i, j in enumerate((1,2,3,2,3,3,1,4,1,39,2,8,3,6,5,4,7,2)): #heart
        while j>0:
            if i % 2 == 1 :
                fill_rect(int(10*x)+(xi%10),int(10*y)+yi,1,1,col["h"])
            xi,yi,j = xi+1, (xi+1)//10, j-1

def draw_emp(x,y):
    """Plus sign Pixel Art 10x10"""
    fill_rect(int(x*10+4), int(y*10+2), 2, 6, col["w"] )
    fill_rect(int(x*10+2), int(y*10+4), 6, 2, col["w"] )

def draw_robot(x,y):
    """Robot head Pixel Art 10x10"""
    fill_rect(x*10,y*10,10,10, col["p"])
    fill_rect(x*10+2, y*10+2, 2, 2, col["b"] )
    fill_rect(x*10+6, y*10+2, 2, 2, col["b"] )
    fill_rect(x*10+2, y*10+6, 6, 2, col["b"] )
    fill_rect(x*10+4, y*10+6, 2, 1, col["p"] )

def go(y,x):
    """Darken each square and move the player"""
    player["x"], player["y"] = x, y
    if x<0 or x>31 or y<0 or y>19:        
        return restart()
    elif carte[y][x] == 666:        
        carte[y][x] = 0
        player["vie"] -= 1
        stats()
        if player["vie"]==0:
            player["level"] = True
        stats()
    elif carte[y][x] == 42:        
        return win()
    elif carte[y][x] == 1:        
        carte[y][x] = 0
        player["vie"] += 1
    elif carte[y][x] == 2:        
        carte[y][x] = 0
        game["E"] += 1
    radar()
    for x0 in range(32):
        for y0 in range(20):
            if (x0,y0) != (x,y):
                c = max(0, get_pixel(10*x0,10*y0)[0]-16)
                fill_rect(x0*10, y0*10, 10, 10, (c,c,c))

def radar(p=1):
    x, y = player["x"], player["y"]
    for i in range(-p,p+1,1):
        for j in range(-p,p+1,1):
            if i == 0 and j == 0:
                #fill_rect(x*10, player["y"]*10, 10, 10, col["p"])
                draw_robot(x,y)
                #fill_rect(x*10, player["y"]*10, 10, 10, col["p"])
            elif sqrt(abs(i)**2+abs(j)**2) <=p+0.5 and 0<=y+j<20 and 0<x+i<32:
                c = 216-24*count(y+j,x+i)
                fill_rect((x+i)*10, (y+j)*10, 10, 10, (c,c,c))
                if carte[y+j][x+i]==2:
                    draw_emp(x+i,y+j)
                if carte[y+j][x+i]==1:
                    draw_heart(x+i,y+j)
                #c = col["h"] if carte[y+j][x+i]==1 else col["w"] 
                #fill_rect((x+i)*10, (y+j)*10, 10, 10, c)
    out()

def out():
    "If the exit is within a reasonable distance from the player it is displayed"
    # w["x"], w["y"] 
    if sqrt((w["x"]-player["x"])**2+(w["y"]-player["y"])**2) <= game["R"]+1:
        fill_rect(w["x"]*10, w["y"]*10, 10, 10, col["w"])
   

def restart():
    player["vie"] -= 1
    player["level"] = True

def win():
    player["vie"] = player["vie"]+1
    player["level"] = True
    game["L"] += 1

def emp(p=1):
    game["E"] -= 1
    for x in range(-p,p+1,1):
        for y in range(-p,p+1,1):
            xe, ye = player["x"]+x, player["y"]+y
            if distance(x,y)<p+0.5 and 0<xe<31 and 0<ye<19 and carte[ye][xe] not in {42,1}:
               carte[ye][xe] = 0
    radar(max(game["R"],p) +3)
    
def init():
    fill_rect(0,0,320,200,(0,0,0))
    game["R"] = max(2,5-game["L"]//4) # Radar range
    game["E"] = max(game["E"], min(game["L"],9)) # EMP count
    mine()
    bonus()
    ui()
    radar(game["R"])
    grille()
    level()

def distance(a,b,c=0,d=0):
    return sqrt((a-c)**2+(b-d)**2)

def level():
    n = game["L"]
    end = False
    if n==43 or player["vie"]<1:
        end = True
    ui,b = col["ui"], col["b"]
    fill_rect(80,40,160,120,ui)
    draw_string( "EXPLORE ESCAPE", 90, 50, b, ui)
    draw_string( "lvl "+str(min(n,42)), 90+40*(not 9<n<31), 80, b, ui)
    if 9<n<31:
        draw_string( str(bin(w["x"]))[1:], 170, 80, b, ui)
    if not end:
        draw_string( str(w["e"])+"   XMP Bonus", 90, 110, b, ui)
        draw_string( str(w["v"])+"  life Bonus", 90, 130, b, ui)
        draw_heart(10.5,13.3)
        draw_emp(10.5,11.3)
        wait()
        fill_rect(80,40,160,120,(0,0,0))
    elif n==43:
        draw_string( "GAME COMPLETE", 90, 120, b, ui)
        q()
    else:
        draw_string( "GAME OVER", 110, 120, b, ui)
        q()

def q():
    wait()
    player["vie"]=-1

while player["vie"]>-1:
    player["level"] = False
    init()
    while not player["level"] and player["vie"]>0:
        k = wait()
        if k == 4 or k == 52:
           if game["E"]>0: # http://f.sincere.free.fr/equation_parabole/equation_parabole.html (0,2.1) (21,5) (42,2.1)
               emp(int(-0.00657*game["L"]**2+0.27619*game["L"]+2.1))
        else:
            go(player["y"]-1*(k==1)+1*(k==2), player["x"]-1*(k==0)+1*(k==3))
        radar(game["R"])
        # grille()
        stats()

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.