escape.py

Created by cent20

Created on December 21, 2024

6.88 KB

A tester - Beta3 - Il manque équilibrage


try:
  import os
  if hasattr(os, "environ"):
    os.environ['KANDINSKY_OS_MODE'] = '0'
except: pass

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)]
# 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":(242, 160, 176), "w":(0, 166, 118), "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  # Fonction anti-rebond
                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 miner():
    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"]-21
    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):
    """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 grille():
    """debugage 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, (242,)*3, (148, 113,222))
    draw_string(str(player["vie"]), 285, 202, (242,)*3, (148, 113,222))

def draw_heart(x,y):
    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):
    fill_rect((x)*10+4, (y)*10+2, 2, 6, col["w"] )
    fill_rect((x)*10+2, (y)*10+4, 6, 2, col["w"] )


def go(y,x):
    """Augmente la couleur de chaque case et deplace le joueur"""
    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):
            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):
    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"])
            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():
    print("WIN", player, carte[player["y"]][player["x"]])
    player["vie"] = player["vie"]+1
    player["level"] = True
    game["L"] += 1
    game["E"] += 1

def play():
    pass
    

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 xe > 0 and ye > 0 and xe < 31 and ye < 19 and carte[ye][xe] != 42:
               carte[ye][xe] = 0
    radar(game["R"]+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
    miner()
    bonus()
    ui()
    radar(game["R"])
    grille()

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

def level(n):
    c = col["ui"]
    fill_rect(80,40,160,120,c)
    draw_string( "EXPLORE ESCAPE", 90, 50, (242,)*3, c)
    draw_string( "lvl "+str(n), 90+40*(not 9<n<31), 80, (242,)*3, c)
    if 9<n<31:
        draw_string( str(bin(w["x"]))[1:], 170, 80, (242,)*3, c)
    draw_string( str(w["e"])+"  XMP Bonus", 90, 110, (242,)*3, c)
    draw_string( str(w["v"])+"  life Bonus", 90, 130, (242,)*3, c)
    wait()
    fill_rect(80,40,160,120,(0,0,0))    

while player["vie"]:
    player["level"] = False
    init()
    level(game["L"])
    while not player["level"]:
        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))
               stats()
        else:
            go(player["y"]-1*(k==1)+1*(k==2), player["x"]-1*(k==0)+1*(k==3))
        radar(game["R"])
        grille()
        ui()
    
def escape():
    pass

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.