treasure.py

Created by cent20

Created on April 22, 2021

6.84 KB

𝗧𝗿𝗲𝗮𝘀𝘂𝗿𝗲


Python Game for Numworks N0110 and N0100,
code by Robert V., october 2020.
nsi.xyz/treasure (FR)
Un trésor a été caché. Déplace toi et pars à sa recherche. Evite les rochers et les trous. Appuies sur [OK] pour creuser un trou. Un sonar t’aide à te guider, sauras-tu l’utiliser ? 🧐


# Treasure v1 NW 17/10/2020
# https://nsi.xyz/treasure
# par Robert Vincent (cent20)

from math import sqrt
from kandinsky import *
from ion import *
from random import randint
from time import sleep

position = [2, 2]
mvt = [2, 2]
score = 666
tresor = [0, 0]

def wait(buttons=(0,1,2,3,4,5,52)):  # Attends qu'une des touches précisées soit pressée
    while True:
        for i in buttons:
            if keydown(i):
                while keydown(i): True  # Fonction anti-rebond
                return i


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 col_os(test=0):
    '''Coloration de l'interface, respecte le thème Omega ou Epsilon
    test = 1 pour utilisation dans get_pixel (debug)'''
    try:
        get_keys()
        return (192, 53 - 1 * test, 53 - 5 * test)
    except:
        return (255 - 7 * test, 183 - 3 * test, 52 - 4 * test)


def personnage(aff=0):
    '''Affiche ou efface le personnage'''
    if aff == 0:
        fill_rect(position[0], position[1], 10, 10, (255, 255, 255))
    else:
        fill_rect(position[0], position[1], 10, 10, col_os())
        fill_rect(position[0] + 3, position[1] + 3, 1, 2, (255, 255, 255))
        fill_rect(position[0] + 6, position[1] + 3, 1, 2, (255, 255, 255))
        fill_rect(position[0] + 3, position[1] + 7, 4, 1, (255, 255, 255))
        fill_rect(position[0] + 2, position[1] + 6, 1, 1, (255, 255, 255))
        fill_rect(position[0] + 7, position[1] + 6, 1, 1, (255, 255, 255))
        # On peut d'ailleurs faire une vrai dessin


def start():
    '''Lancement du jeu. Texte introductif, génération des obstacles'''
    global tresor
    texte_center("A treasure has been hidden", 160, 12, col_os())
    texte_center("Move around to look for it,", 160, 42, (120, 120, 120))
    texte_center("avoid rocks and holes,", 160, 62, (120, 120, 120))
    texte_center("Press OK to dig a hole", 160, 82, (120, 120, 120))
    texte_center("Press on an arrow", 160, 122, col_os())
    wait()
    fill_rect(1, 1, 320, 204, (255, 255, 255))
    for i in range(32):  # On place 32 obstacles
        fill_rect(2 + 10 * randint(2, 29), 2 + 10 * randint(2, 18), 10, 10, (42, 42, 42))
    for i in range(16):  # On place 16 trous
        x = randint(2, 29)
        y = randint(2, 18)
        for j in range(4):
            fill_rect(2 + 10 * x + j, 2 + 10 * y + j, 10 - 2 * j, 10 - 2 * j,
                      (196 - j * 32, 196 - j * 32, 196 - j * 32))
    while tresor == [0, 0]:
        x = 2 + 10 * randint(2, 29)
        y = 2 + 10 * randint(2, 18)
        if get_pixel(x, y) == (248, 252, 248):
            tresor = [x, y]
    personnage(1)


def info(penalite=0, init=0):
    global score
    score -= penalite
    if init == 1:
        fill_rect(1, 1, 320, 204, (255, 255, 255))
        fill_rect(0, 204, 320, 20, col_os())
        texte_center("nsi.xyz/treasure par cent20 ", 160, 184, col_os())
        texte_center("x", 20, 204, (255, 255, 255), col_os())
        texte_center("y", 50, 204, (255, 255, 255), col_os())
        texte_center("score", 100, 204, (255, 255, 255), col_os())
        texte_center("event", 160, 204, (255, 255, 255), col_os())
        texte_center("distance", 250, 204, (255, 255, 255), col_os())

    else:
        fill_rect(0, 204, 320, 20, col_os())
        texte_center(str(position[0] // 10), 20, 204, (255, 255, 255), col_os())
        texte_center(str(position[1] // 10), 50, 204, (255, 255, 255), col_os())
        texte_center(str(score), 100, 204, (255, 255, 255), col_os())
        # Afficher le score par ex


def collision():
    '''on vérifie les colisions, si colisions, on retourne False, sinon True
    On teste mvt[0] et mvt[1]
    '''
    # Rocher
    if get_pixel(mvt[0], mvt[1]) == (40, 40, 40):
        info(10)
        texte_center("rock", 160, 204, (255, 255, 255), col_os())
        return False
    # Trou
    elif get_pixel(mvt[0], mvt[1]) == (192, 196, 192):
        info(32)
        texte_center("hole", 160, 204, (255, 255, 255), col_os())
        return False
    # texte_center(str(get_pixel(mvt[0], mvt[1])), 80, 2, (120,120,120))
    return True


def game():
    '''gère les actions sur les touches'''
    global score, position, mvt
    bordure = (2, 2, 192, 302)  # x min, x max, y min, y max
    win = False
    creuse = False

    while not keydown(5) and not win and score > 0:

        is_key_pressed = False

        if keydown(1):  # fleche haut
            mvt[1] = max(position[1] - 10, bordure[1])
            mvt[0] = position[0]
            is_key_pressed = True
            sleep(0.120)

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

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

        elif keydown(0):  # fleche Gauche
            mvt[0] = max(position[0] - 10, bordure[0])
            mvt[1] = position[1]
            is_key_pressed = True
            sleep(0.120)

        elif keydown(4) or keydown(52):  # Touche OK
            if position[0] == tresor[0] and position[1] == tresor[1]:
                texte_center("win !", 160, 204, (255, 255, 255), col_os())
                win = True
            else:
                texte_center("nothing", 160, 204, (255, 255, 255), col_os())
                info(32)
                distance = int(sqrt((position[0]//10 - tresor[0]//10) ** 2 + (position[1]//10 - tresor[1]//10) ** 2))
                texte_center(str(bin(distance)[2:]), 250, 204, (255, 255, 255), col_os())
                sleep(0.666)
                creuse = True
            sleep(0.120)

        if is_key_pressed and collision():
            personnage(0)
            if creuse == True:
                for j in range(4):
                    fill_rect(position[0] + j, position[1] + j, 10 - 2 * j, 10 - 2 * j,
                              (196 - j * 32, 196 - j * 32, 196 - j * 32))
                creuse = False

            position[0] = mvt[0]
            position[1] = mvt[1]
            personnage(1)
            info(1)
            
    if score < 1:
        fill_rect(80, 60, 160, 80, col_os())
        texte_center("You have loose", 160, 92, (255, 255, 255), col_os())
    if score > 1:
      fill_rect(80, 60, 160, 80, col_os())
      texte_center("You have won", 160, 80, (255, 255, 255), col_os())
      texte_center("score = " + str(score), 160, 105, (255, 255, 255), col_os())

def play(nb):
    global position, mvt, score, tresor
    while nb > 0:
        position = [2, 2]
        mvt = [2, 2]
        score = 666
        tresor = [0, 0]
        info(0, 1)
        start()
        wait()
        info()
        game()
        nb -= 1
        wait()

play(1)

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.