z_episode5.py

Created by cent20

Created on April 13, 2023

2.53 KB

Joue avec ta #NumWorks : épisode 3

Coder un jeu sur NumWorks.

Pour faire un jeu sur une calculatrice, il faut savoir utiliser les modules graphiques, aléatoires et les touches de la calculatrice. Ceci est une démo. A partir de cette démo il est facile de réaliser un “memory” par exemple.. https://twitter.com/nsi_xyz/status/1646195559470972928


# Joue avec ta NumWorks : episode 5
# https://twitter.com/nsi_xyz/status/1646195559470972928

from kandinsky import fill_rect, draw_string
from random import randint
from time import sleep
from ion import keydown



def quadrillage(xi, yi, xn, yn, larg, haut, e1, col):
    for x in range(xn):
        for y in range(yn):
            fill_rect(xi+x*(larg+e1), yi+y*(haut+e1), larg, haut, col)         

# 3*3*3 = 27 couleurs = 9 * 3
color = [(r,g,b) for r in [42,142,242] for g in [42,142,242] for b in [42,142,242]]

def grille(xi, yi, xn, yn, larg, haut, e1):
    for x in range(xn):
        for y in range(yn):
            col = color[9*y+x]
            fill_rect(xi+x*(larg+e1), yi+y*(haut+e1), larg, haut, col)

# xmax = 8 / 9-1, de 0 à 8...  
# ymax = 2 / 3-1, ...

grille(7,7,9,3,23,23,12)

pos = [4,1] # départ
pmax = (9,3) # modulo



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

def deplacement():
    # On efface sélection précédente
    x, y = pos_old
    col = color[9*y+x]
    fill_rect(7+x*(23+12)-5,7+y*(23+12)-5,34,34,(255,255,255)) # Blanc
    fill_rect(7+x*(23+12),7+y*(23+12),23,23,col)
    # On affiche la nouvelle selection
    x, y = pos
    col = color[9*y+x]
    fill_rect(7+x*(23+12)-5,7+y*(23+12)-5,34,34,col)

def affichage():
    draw_string("pos_old = " + str(pos_old), 40, 130)        
    draw_string("pos = "+str(pos), 80, 150)
    draw_string("couleur = "+str(color[pos[0]+pos[1]*pmax[1]])+"   ", 40, 170)


while True:
    key_pressed = None    
    """Pour l'instant les actions sont alétoires mais il suffit de décommenter la ligne
    # key_pressed = wait() ci dessous et de supprimer les 2 suivantes pour prendre en compte les appuis sur le clavier"""
    # key_pressed = wait()
    key_pressed = randint(1,4)    # A supprimer
    sleep(0.42)                   # A supprimer
    
    pos_old = list(pos) # On crée une copie indépendante
    
    if key_pressed == 1:  # fleche haut
        pos[1] = (pos[1]-1) % pmax[1]
    
    elif key_pressed == 2: # fleche bas
        pos[1] = (pos[1]+1) % pmax[1]
    
    elif key_pressed == 3: # fleche droite
        pos[0] = (pos[0]+1) % pmax[0]
    
    elif key_pressed == 0: # fleche gauche
        pos[0] = (pos[0]-1) % pmax[0]  
        
    elif key_pressed == 4: # Touche OK
        pass # Pas d'action ici
        
    if key_pressed != None:
        deplacement()
        affichage()

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.