cubito_adventures_b1.py

Created by lorem-ipsum-42

Created on October 27, 2020

7.7 KB

Premiere beta des Aventures de Cubito


from math import *
from kandinsky import *
from ion import *
from random import *
from time import *

def texte_center(texte, x=160, y=110, col1=(255, 255, 255), col2=(255, 255, 255)):
    '''Cette fonction affiche un texte et le centre'''
    draw_string(texte, x - len(texte) * 10 // 2, y, col1, col2)

def accueil():
    global niveau
    fill_rect(0,0,320,222,color(0,0,0))
    texte_center("Welcome to:", 160, 12, (255, 255, 0), (0, 0, 0))
    for i in range(0,320,10):
        set_pixel(i,40,color(255,255,0))
        sleep(0.05)
    texte_center("Cubito's Adventures", 160, 52, (255, 255, 0), (0, 0, 0))
    texte_center("To move, press the arrows,", 160, 72, (255, 255, 0), (0, 0, 0))
    texte_center("But warning! If you have a hole", 160, 92, (255, 255, 0), (0, 0, 0))
    texte_center("under you, you will fall!", 160, 112, (255, 255, 0), (0, 0, 0))
    texte_center("For each level, try to have", 160, 132, (255, 255, 0), (0, 0, 0))
    texte_center("the most of points, using scales", 160, 152, (255, 255, 0), (0, 0, 0))
    sleep(4.2)
    texte_center("To start the game on level 1,", 160, 182, (255, 255, 0), (0, 0, 0))
    texte_center('Press "OK" or "clear"', 160, 202, (255, 255, 0), (0, 0, 0))
    while not keydown(5):
        if keydown(4) or keydown(17):
            jeu()
        elif keydown(36):
            while not keydown(5):
                if keydown(43):
                    fill_rect(0,0,320,222,(0,0,0))
                    niveau=42
                    parties()

def initiation():
    fill_rect(0,0,320,222,color(0,0,0))
    for j in range(25,222,10):
        for i in range(5,320,10):
            set_pixel(i,j,color(255,255,255))
    initialisation()
            
position = [0, 20] # x, y 
mvt = [0, 20] # x, y 
global score
score=0
niveau=1

def initialisation(etat=0):
    global score, niveau, echelle
    try:
        get_keys()
        os = (192,53,53)
    except:
        os = (255,183,52)
    if etat == 0:
        fill_rect(0,204,320,18,os)
        fill_rect(0,0,320,18,os)
        texte_center("nsi.xyz/cubito par Gab & Raph ",160,204,(255,255,255),os)
        draw_string("Score :",0,0,(255,255,255),os)
        draw_string("Niveau :",160,0,(255,255,255),os)
    else:
        draw_string(str(score),80,0,(255,255,255),os)
        draw_string(str(niveau),250,0,(255,255,255),os)

def personnage(aff=0):
    if aff == 0:
        fill_rect(position[0], position[1], 10 , 10, (0,0,0))
        # On peut d'ailleurs faire une vrai dessin
    else:
        fill_rect(position[0], position[1], 10 , 10, (255,255,0))
        # On peut d'ailleurs faire une vrai dessin

def collision():

    '''on vérifie les colisions, si colisions, on retourne False, sinon True
    On teste mvt[0] et mvt[1] 
    ''' 
    return True

def points():
    global score, position, mvt, longueur_echelle
    if get_pixel(position[0]+4,position[1]-39)==(248,252,248):
        personnage(0)
        position[1]-=longueur_echelle+10
        position[0]=position[0]
        mvt[1]=position[1]
        personnage(1)
    elif get_pixel(mvt[0]+5,mvt[1]+5)!=(248,252,248):
        score-=1
    else:
        score+=1

def videe():
    global position, mvt, score
    while get_pixel(position[0]+5,position[1]+15)!=(248,252,248) and position[1]!=190:
        personnage(0)
        position[0]=position[0]
        position[1]+=10
        mvt[1]=position[1]
        score-=1
        personnage(1)
        sleep(1)

def choses():
    global niveau, position, mvt, score
    niveau_suivant=False
    if niveau==1 and position[0]==310 and position[1]==190:
        texte_center("Tu as reussi le niveau 1!", 160, 42, (255, 255, 0), (0, 0, 0))
        texte_center("Avec un score de "+str(score), 160, 62, (255, 255, 0), (0, 0, 0))
        niveau_suivant=True

    elif niveau==2 and position[0]==150 and position[1]==70:
        texte_center("Tu as reussi le niveau 2!", 160, 42, (255, 255, 0), (0, 0, 0))
        texte_center("Avec un score de "+str(score), 160, 62, (255, 255, 0), (0, 0, 0))
        niveau_suivant=True

    elif niveau==3 and position[0]==50 and position[1]==100:
        texte_center("Tu as reussi le niveau 3!", 160, 42, (255, 255, 0), (0, 0, 0))
        texte_center("Avec un score de "+str(score), 160, 62, (255, 255, 0), (0, 0, 0))
        niveau_suivant=True

    elif niveau==4 and position[0]==240 and position[1]==180:
        texte_center("Tu as reussi le niveau 4!", 160, 42, (255, 255, 0), (0, 0, 0))
        texte_center("Avec un score de "+str(score), 160, 62, (255, 255, 0), (0, 0, 0))
        niveau_suivant=True

    elif niveau==5 and position[0]==130 and position[1]==40:
        texte_center("Tu as reussi le niveau 5!", 160, 42, (255, 255, 0), (0, 0, 0))
        texte_center("Avec un score de "+str(score), 160, 62, (255, 255, 0), (0, 0, 0))
        niveau_suivant=True

    if niveau_suivant:
        niveau+=1
        position = [0, 20] 
        mvt = [0, 20]
        score=0
        sleep(4.2)
        jeu()

def echelle():
    global abscisse_echelle, longueur_echelle, ordonnee_echelle, ordonnee_echelle42
    longueur_echelle=40
    if niveau==1:
        ordonnee_echelle=60
        fill_rect(abscisse_echelle,60,10,longueur_echelle,(255,255,255))
    elif niveau==2:
        ordonnee_echelle=80
        fill_rect(abscisse_echelle,80,10,longueur_echelle,(255,255,255))
    elif niveau==3:
        ordonnee_echelle=50
        fill_rect(abscisse_echelle,50,10,longueur_echelle,(255,255,255))
    elif niveau==4:
        ordonnee_echelle=20
        fill_rect(abscisse_echelle,20,10,longueur_echelle,(255,255,255))
    elif niveau==5:
        ordonnee_echelle=130
        fill_rect(abscisse_echelle,130,10,longueur_echelle,(255,255,255))
    elif niveau==42:
        ordonnee_echelle=ordonnee_echelle42
        fill_rect(abscisse_echelle,ordonnee_echelle42,10,longueur_echelle,(255,255,255))
        


def move():  
    global postion, mvt 
    bordure = [0, 20, 190, 310] #x min, x max, y min, y max
    while not keydown(5):
        videe()
        echelle()   
        is_key_pressed = False
        if keydown(0): #Gauche
            mvt[0] = max(position[0] - 10, bordure[0])
            is_key_pressed = True

        elif keydown(1): #haut
            mvt[1] = max(position[1] - 10, bordure[1])
            is_key_pressed = True

        elif keydown(2): #bas
            mvt[1] = min(position[1] + 10, bordure[2])
            is_key_pressed = True

        elif keydown(3): #Droite
            mvt[0] = min(position[0] + 10, bordure[3])
            is_key_pressed = True

        if is_key_pressed and collision():
            points()
            sleep(0.1)
            choses()
            initialisation(1)
            personnage(0)
            position[0] = mvt[0]
            position[1] = mvt[1]
            personnage(1)

def parties():
    global niveau, position, score, abscisse_echelle, ordonnee_echelle42
    initiation()
    v=[]
    for i in range(0,320,10):
        v.append(i)#Pas mettre sur la même ligne que l'arrivée
    abscisse_echelle=choice(v)
    ordonnee_echelle42=choice(v[0:19])
    if niveau==1:
        fill_rect(310,190,10,10,color(0,255,255))
    elif niveau==2:
        fill_rect(150,70,10,10,color(0,255,255))
    elif niveau==3:
        fill_rect(50,100,10,10,color(0,255,255))
    elif niveau==4:
        fill_rect(240,180,10,10,color(0,255,255))
    elif niveau==5:
        fill_rect(130,40,10,10,color(0,255,255))
    elif niveau==42:
        x, y = [], []
        for i in range(0,310,10):
            x.append(i)
        for j in range(20,190,10):
            y.append(j)
        fill_rect(choice(x),choice(y),10,10,(0,255,0))
    move()

def jeu():
    global niveau
    while niveau!=6:
        fill_rect(0,0,320,222,color(0,0,0))
        sleep(2)
        parties()
    draw_string("Bien joue tu as gagneeee",0, 62, (255, 255, 0), (0, 0, 0))

accueil()