escape_beta4.py

Created by cent20

Created on December 23, 2024

6.68 KB


from kandinsky import *

from random import randint as r
from random import 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
col = {"b":(248,248,248),"ui":(148, 113, 222), "p":(42, 142, 242), "h":(242, 160, 176), "w":(40, 200, 140), "l":(163,22,33)}
p = {"x":1, "y":5, "vie":3, "level":False} #Player
game = {"L":1, "R":6, "E":0} # Level Radar EMP
w = {"x":0, "y":0, "e":0, "v":0} #win, bonus

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()
    m=22
    while m>0:
        y, x = r(2,18), r(2,29)
        if carte[y][x]!=666:
            carte[y][x]=666
            m -= 1
    n = 10*game["L"]- 30
    while n>0:
        y, x = r(1,18), r(2,29)
        if carte[y][x]!=666 and count(y,x)>0:
            carte[y][x]=666
            n -= 1    
    
    y , x = r(1,18), 1
    p["x"], p["y"], = 1, y
    carte[y][x]=0
    x, y = r(10,42), choice((0,19))
    if x >= 28:
        x, y = 31, r(3,17)
    carte[y][x]=42
    w["x"], w["y"] = x, y

def bonus():
    w["e"], w["v"] = r(1,5-game["L"]//10), r(1,5-game["L"]//8)
    t,e,v = 0, w["e"], w["v"]
    while t<1000 and (e>0 or v>0):
        y, x = r(1,18), r(2,29)
        if e>0 and count(y,x)==0 and carte[y][x]==0:
            carte[y][x]=2
            e-=1
        elif v>0 and count(y,x)==0 and carte[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 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(p["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"""
    p["x"], p["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
        p["vie"] -= 1
        stats()
        if p["vie"]==0:
            p["level"] = True
        stats()
    elif carte[y][x] == 42:        
        return win()
    elif carte[y][x] == 1:        
        carte[y][x] = 0
        p["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 distance(x,y,x0,y0) > game["R"]+0.49 and distance(x,y,x0,y0)<22:
                # (x0,y0) != (x,y)
                g = get_pixel(10*x0,10*y0)[0]
                if g!=0:
                    c = max(0, g-16)
                    fill_rect(x0*10, y0*10, 10, 10, (c,c,c))

def radar(d=1):
    x, y = p["x"], p["y"]
    for i in range(-d,d+1,1):
        for j in range(-d,d+1,1):
            if i == 0 and j == 0:
                draw_robot(x,y)
            elif distance(i,j) <= d+0.5 and 0<=y+j<20 and 0<x+i<32:
                c = 248-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)
    out()

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

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

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

def emp(d=1):
    game["E"] -= 1
    for x in range(-d,d+1,1):
        for y in range(-d,d+1,1):
            xe, ye = p["x"]+x, p["y"]+y
            if distance(x,y)<d+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"],d) +3)
    
def init():
    fill_rect(0,0,320,200,(0,0,0))
    game["R"] = max(2,5-game["L"]//8) # Radar range
    game["E"] = max(game["E"], min(game["L"],9)) # EMP count
    mine()
    bonus()
    ui()
    radar(game["R"])
    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 p["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()
    p["vie"]=-1

while p["vie"]>-1:
    p["level"] = False
    init()
    while not p["level"] and p["vie"]>0:
        k = wait()
        if k == 4 or k == 52:
           if game["E"]>0:
               emp(int(-0.00657*game["L"]**2+0.27619*game["L"]+2.1))
        else:
            go(p["y"]-1*(k==1)+1*(k==2), p["x"]-1*(k==0)+1*(k==3))
        radar(game["R"])
        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.