mario.py

Created by vieirajerome31

Created on October 14, 2023

1.31 KB


import kandinsky as kd
import ion
import numpy as np
from time import sleep

# Configuration de l'écran de la calculatrice NumWorks
WIDTH = 320
HEIGHT = 240

# Taille du niveau
LEVEL_WIDTH = 64
LEVEL_HEIGHT = 64

# Couleurs
WHITE = kd.color(255, 255, 255)
BLUE = kd.color(0, 0, 255)
GREEN = kd.color(0, 255, 0)

# Niveau de Mario (matrice de 64x64)
level = [[WHITE] * LEVEL_WIDTH for _ in range(LEVEL_HEIGHT)]

# Dessiner le sol
for x in range(LEVEL_WIDTH):
    level[LEVEL_HEIGHT - 1][x] = GREEN

# Position initiale de Mario
x_mario = 10
y_mario = LEVEL_HEIGHT - 2

# Tableau de touches
keys = np.zeros(256, dtype=bool)

# Fonction pour effacer l'écran
def effacer_ecran():
    kd.fill_rect(0, 0, WIDTH, HEIGHT, WHITE)

# Boucle de jeu
while True:
    effacer_ecran()

    # Déplacement de Mario
    if keys[ion.keypad.LEFT] and x_mario > 0:
        x_mario -= 1
    if keys[ion.keypad.RIGHT] and x_mario < LEVEL_WIDTH - 1:
        x_mario += 1

    # Dessin du niveau
    for y in range(LEVEL_HEIGHT):
        for x in range(LEVEL_WIDTH):
            kd.fill_rect(x * 5, y * 5, 5, 5, level[y][x])

    # Dessin de Mario
    kd.fill_rect(x_mario * 5, y_mario * 5, 5, 5, BLUE)

    # Rafraîchir l'écran
    kd.display()

    # Pause pour contrôler la vitesse de rafraîchissement
    sleep(0.05)  # 50 millisecondes

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.