version_jeu_poo.py

Created by vgalletramond

Created on December 20, 2024

10.6 KB


from kandinsky import fill_rect as f_r,draw_string as d_s,get_pixel as g_p
from random import randint as rd
from ion import keydown as kd
from time import sleep as sl


class P:
    def __init__(self,n_vie,n_strengh,n_shield,position):
        self.vie=n_vie
        self.strengh=n_strengh
        self.shield=n_shield
        self.pos=position
    def place(self,position):
        self.pos=position
    def vie_restante(self,modif_vie):
        if modif_vie > 0:
            self.vie = round(self.vie + modif_vie,2)
        elif modif_vie < 0:
            self.vie = round(self.vie +(modif_vie+self.shield),2)
    def aug_strengh(self, aug_st):
        self.strengh = round(self.strengh+aug_st,2)
    def aug_shield(self, aug_sh):
        self.shield = round(self.shield+aug_sh,1)
        

col=('white','white','green','red','green','green','red','gray')
zombie = P(3,1.3,0.1,False)
squelette = P(3.5,1.5,0.3,False)
araigne = P(5,2,0.4,False)
gobelin = P(4,2.5,0.2,False)
blob = P(9,1.2,0.7,False)
shadow = P(7,4.2,0.4,False)
hydre = P(20,6,2,False)
en = ((zombie,'Zombie'),(squelette,'Squelette'),(araigne,'Araignée'),(gobelin,'Gobelin'),(blob,'Blob'),(shadow,'Shadow'),(hydre,'Hydre'))




def event(n_e):
    if n_e == 2:
        h.vie += rd(1,4-(n//10))
    elif n_e == 3:
        h.vie -= rd(1,1+(n//10))-h.shield
        if h.vie <= 0:
            sl(1)
            init_jeu()
    elif n_e == 4:
        h.strengh += 0.5
    elif n_e == 5:
        h.shield += 0.2
    elif n_e == 6:
        combat(en[rd(0,1+(n//4)*(n<24)+5*(n>=24))])
    return stats_h()

def combat(m_i):
    e_c = 1
    stats_h(1,m_i[0],m_i[1])  
    sl(1)
    while e_c == 1:
        m_i[0].vie -= h.strengh-m_i[0].shield
        stats_h(1,m_i[0],m_i[1])
        sl(1)
        if m_i[0].vie <= 0:
            e_c = 0
            return stats_h(0,m_i[0],m_i[1])
        h.vie -= m_i[0].strengh-h.shield
        stats_h(1,m_i[0],m_i[1])
        sl(1)
        if h.vie <= 0:
            e_c = 0
            sl(1)
            return init_jeu()


def acceuil():
    f_r(0,0,320,222,'black')
    title()
    d_s("OK --> jouer",105,150,'white','black')
    while True:
        if kd(4) or kd(52):
            sl(0.3)
            global n
            n = 1
            return init_jeu()


def jeu():
    f_r(0,0,320,222,'black')
    title(100,10,0.75)
    for i in range(t_p):
        for j in range(t_p):
            red=(80+int((144-2)/t_p+1)*j,30+int((144-2)/t_p+1)*i,int((144-2)/t_p),int((144-2)/t_p))
            if g[i][j] == 'P':
                f_r(red[0],red[1],red[2],red[3],'gray')
                d_c(red[0],red[1],red[2])
            else:
                f_r(red[0],red[1],red[2],red[3],'white')
                d_q_m(red[0],red[1],red[2])
    a_g()
    stats_h()
    barre_bas()
    
        

def stats_h(fight=0,m='',n_m=''):
    d_s("Heros",5,5,'white','black')
    d_s("PV:"+str(round((h.vie*(h.vie>0)+0.0),1)),5,25,'white','black')
    d_s("AT:"+str(round(h.strengh,1)),5,45,'white','black')
    d_s("DEF:"+str(round(h.shield,1)),5,65,'white','black')
    if fight == 1:
        d_s(str(n_m),230,5,'white','black')
        d_s("PV:"+str(round((m.vie*(m.vie>0)+0.0),1)),230,25,'white','black')
        d_s("AT:"+str(round(m.strengh,1)),230,45,'white','black')
        d_s("DEF:"+str(round(m.shield,1)),230,65,'white','black')
    elif fight == 0:
        f_r(230,4,100,100,'black')
        if (len(n_m)>0):
            m.vie = 10*(n_m=='Hydre')+4*(n_m=='Shadow')+7*(n_m=='Blob')+5*(n_m=='Araignée')+3*(n_m=='Gobelin' or n_m=='Squelette')+2*(n_m=='Zombie')
    return
        

def a_g(ori=[0,0]):
    for char in range(len(g)):
        for pers in range(len(g[char])):
            if g[char][pers] == 'P':
                g[char][pers] = 1
    eve = g[h.pos[1]][h.pos[0]]
    g[h.pos[1]][h.pos[0]] = 'P'
    f_r(80+int((144-2)/t_p+1)*ori[0],30+int((144-2)/t_p+1)*ori[1],int((144-2)/t_p),int((144-2)/t_p),'white')
    f_r(80+int((144-2)/t_p+1)*h.pos[0],30+int((144-2)/t_p+1)*h.pos[1],int((144-2)/t_p),int((144-2)/t_p),'gray')
    d_c(80+int((144-2)/t_p+1)*h.pos[0]+9*(t_p==3)+7*(t_p==4)+5*(t_p==5)+3*(t_p==6),30+int((144-2)/t_p+1)*h.pos[1]+9*(t_p==3)+7*(t_p==4)+5*(t_p==5)+3*(t_p==6),30*(t_p==3)+21*(t_p==4)+18*(t_p==5)+18*(t_p==6))
    a_g2()
    return eve
def a_g2():
    for i in range(2):
        for j in range(2):
            compact=(h.pos[1]-1*((j==0) and (i==0) and (h.pos[1]-1>=0))+1*((j==1) and (i==0) and (h.pos[1]+1<t_p)))
            compact2=(h.pos[0]-1*((j==0) and (i==1) and (h.pos[0]-1>=0))+1*((j==1) and (i==1) and (h.pos[0]+1<t_p)))
            comp=(80+int((144-2)/t_p+1)*compact2,30+int((144-2)/t_p+1)*compact,int((144-2)/t_p),int((144-2)/t_p))
            comp2=(80+int((144-2)/t_p+1)*compact2+9*(t_p==3)+7*(t_p==4)+5*(t_p==5)+3*(t_p==6),30+int((144-2)/t_p+1)*compact+9*(t_p==3)+7*(t_p==4)+5*(t_p==5)+3*(t_p==6),30*(t_p==3)+21*(t_p==4)+18*(t_p==5)+18*(t_p==6))
            elem=g[compact][compact2]
            if (not(g_p(int(comp2[0]+comp2[2]/2),int(comp2[1]-1))[0])):
                if elem == 'P':
                    f_r(comp[0],comp[1],comp[2],comp[3],'gray')
                else:
                    f_r(comp[0],comp[1],comp[2],comp[3],col[elem])
                if elem !=1:
                    f_t[str(elem)](comp2[0],comp2[1],comp2[2])
                




def pixel_heart(x,y,t):
    for j in range(9):
        for i in range((j==0)+(j==3)+(j==4)*2+(j==5)+(j==8),4+1*((j==1)+(j==7))+2*((j==2)+(j==6))+3*((j==3)+(j==5))+4*(j==4)):
            f_r(x+int(t/9)*j+int(1/2*((t+5)/9)),y+int(t/9)*i+int(1/2*((t+15)/7)),int(t/9),int(t/9),'red')

def draw_cross(x,y,t):
    for j in range(5):
        for i in range(((j==1)+(j==3))+((j==2)+(j==5))*2,5,4*((j==0)+(j==4))+2*((j==1)+(j==3))+3*(j==2)):
            f_r(x+int(t/5)*j,y+int(t/5)*i,int(t/5),int(t/5),'red')

def draw_sword(x,y,t):
    for j in range(7):
        for i in range(6*(j==0)+3*((j==1)+(j==2))+2*(j==3)+1*(j==4),7-2*(j==2)-1*(j==3)-3*(j==4)-4*(j==5)-5*(j==6),1+1*(j==1)):
            f_r(x+int(t/7)*j,y+int(t/7)*i,int(t/7),int(t/7),'gray')

def draw_sword_m(x,y,t):
    for j in range(7):
        for i in range(6*(j==6)+3*((j==5)+(j==4))+2*(j==3)+1*(j==2),7-2*(j==4)-1*(j==3)-3*(j==2)-4*(j==1)-5*(j==0),1+1*(j==5)):
            f_r(x+int(t/7)*j,y+int(t/7)*i,int(t/7),int(t/7),'gray')

def draw_spike(x,y,t):
    for j in range(5):
        for i in range(3*((j==0)+(j==4))+1*((j==1)+(j==3)),4):
            f_r(x+int(t/5)*j,y+int(t/5)*i+int(1/2*(t/5)),int(t/5),int(t/5),'gray')

def d_c(x,y,t):
    for j in range(5):
        for i in range(5*((j==0)+(j==4)),6*((j==0)+(j==4))+7*(j==2)+1*((j==1)+(j==3))):
            if (j==1)+(j==3):
                for k in range(3):
                    f_r(x+int(t/9)*j+int(2*(t/9)),y+int(t/9)*k,int(t/9),int(t/9),'black')
                f_r(x+int(t/9)*j+int(2*(t/9)),y+int(t/9)*4,int(t/9),int(t/9),'black')
                for k in range(2):
                    f_r(x+int(t/9)*j+int(2*(t/9)),y+int(t/9)*(k+7),int(t/9),int(t/9),'black')
                break
            else:
                f_r(x+int(t/9)*j+int(2*(t/9)),y+int(t/9)*i,int(t/9),int(t/9),'black')


def d_q_m(x,y,t):
    for j in range(5):
        for i in range(1*((j==0)+(j==4)),1*((j==2)+(j==1))+3*((j==0)+(j==4))+4*(j==3),1*(j!=3)+3*(j==3)):
            if (j==2):
                f_r(x+int(t/8)*j+int(1.9*(t/8)),y+int(t/8),int(t/8),int(t/8),'black')
                for k in range(2):
                    f_r(x+int(t/8)*j+int(1.9*(t/8)),y+int(t/8)*(k+4)+int(t/8),int(t/8),int(t/8),'black')
                f_r(x+int(t/8)*j+int(1.9*(t/8)),y+int(t/8)*7+int(t/8),int(t/8),int(t/8),'black')
                break
            else:
               f_r(x+int(t/8)*j+int(1.9*(t/8)),y+int(t/8)*i+int(t/8),int(t/8),int(t/8),'black')
               
def barre_bas():
    f_r(0, 200, 320, 22, (148, 113, 222))
    d_s( "nsi.xyz/themap", 0, 202, (242,)*3, (148, 113,222))
    txt = "Niveau "+str(n)
    d_s(txt, 170, 202, (242,)*3, (148, 113,222))
    d_s("",320, 202,(242,)*3, (148, 113,222))

def draw_shield(x,y,t):
    for j in range(5):
        for i in range(6-2*((j==0)+(j==4))-1*((j==1)+(j==3))):
            f_r(x+int(t/6)*j+int(1/2*(t/6)),y+int(t/6)*i,int(t/6),int(t/6),'brown')


def title(x=90,y=35,t=1):
    for j in range(5):
        for i in range(5-4*((j==0) or (j==1) or (j==3) or (j==4))):
            f_r(int(x+4*j*t),int(y+4*i*t),int(4*t),int(4*t),'gray')
    for j in range(4):
        for i in range(2*((j==1)+(j==2)),5-2*((j==1)+(j==2))):
            f_r(int(x+25*t+4*j*t),int(y+4*i*t),int(4*t),int(4*t),'gray')
    for j in range(3):
        for i in range(0,5,1*(j==0)+2*(j==1)+4*(j==2)):
            f_r(int(x+50*t+4*j*t),int(y+4*i*t),int(4*t),int(4*t),'gray')
    for j in range(5):
        for i in range(1*((j==1) or (j==3))+2*(j==2),5-3*((j==1) or (j==3))-2*(j==2)):
            f_r(int(x+75*t+4*j*t),int(y+4*i*t),int(4*t),int(4*t),'gray')
    for j in range(5):
        for i in range(3*((j==0) or (j==4))+1*((j==1) or (j==3)),5-2*((j!=0) and (j!=4)),1+1*(j==2)):
            f_r(int(x+100*t+4*j*t),int(y+4*i*t),int(4*t),int(4*t),'gray')
    for j in range(3):
        for i in range(1*(j==2),5-2*(j==1)-3*(j==2),1*(j!=1)+2*(j==1)):
            f_r(int(x+125*t+4*j*t),int(y+4*t*i),int(4*t),int(4*t),'gray')

def init_jeu():
    global t_p,g,h
    t_p = (3+n//(4+n//7)*(n<32)+3*(n>=32))
    g = [[rd(1,6) for i in range(t_p)] for j in range(t_p)]
    g[rd(2,t_p-1)][rd(2,t_p-1)] = 0
    g[0][0] = 'P'
    h = P(round(5+0.5*(n//5),2),round(1+0.1*(n//5),2),round(0+0.1*(n//10),1),[0,0])
    return jeu()

f_t = {'P':d_c,'0':draw_cross,'2':pixel_heart,'3':draw_spike,'4':draw_sword,'5':draw_shield,'6':draw_sword_m}
acceuil()

while True:
    sl(0.5)
    stop = 0
    while stop == 0:
        orig=[h.pos[0],h.pos[1]]
        if kd(2):
            stop = 1
            h.pos[1] += 1*(h.pos[1]+1<t_p)
            eve = a_g(orig)
            if eve == 0:
                n += 1
                init_jeu()
            elif eve !=1:
                event(eve)
            stop = 0
            sl(0.4)
        elif kd(3):
            stop = 1
            h.pos[0] += 1*(h.pos[0]+1<t_p)
            eve = a_g(orig)
            if eve == 0:
                n += 1
                init_jeu()
            elif eve !=1:
                event(eve)
            stop = 0
            sl(0.4)
        elif kd(1):
            stop = 1
            h.pos[1] -= 1*(h.pos[1]-1>=0)
            eve = a_g(orig)
            if eve == 0:
                n += 1
                init_jeu()
            elif eve !=1:
                event(eve)
            stop = 0
            sl(0.4)
        elif kd(0):
            stop = 1
            h.pos[0] -= 1*(h.pos[0]-1>=0)
            eve = a_g(orig)
            if eve == 0:
                n += 1
                init_jeu()
            elif eve !=1:
                event(eve)
            stop = 0
            sl(0.4)

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.