dessin_cercle.py

Created by dan-tabet

Created on January 02, 2025

3.95 KB


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

def draw_line(x1, y1, x2, y2, c):
    width = x2 - x1
    height = y2 - y1
    if abs(width) >= abs(height):
        div = height / width
        for i in range(0, width, (width > 0) * 2 - 1):
            set_pixel(x1 + i, y1 + int(div * i + 0.5), c)
    else:
        div = width / height
        for i in range(0, height, (height > 0) * 2 - 1):
            set_pixel(x1 + int(div * i + 0.5), y1 + i, c)

def draw_circle(center_x, center_y, rayon, couleur):
    for x in range(center_x - rayon, center_x + rayon + 1):
        for y in range(center_y - rayon, center_y + rayon + 1):
            if (x - center_x) ** 2 + (y - center_y) ** 2 <= rayon ** 2:
                set_pixel(x, y, couleur)

def initialisation(etat=0):
    try:
        get_keys()
        os_color = (192, 53, 53)
    except:
        os_color = (255, 183, 52)
    if etat == 0:
        fill_rect(1, 204, 320, 18, os_color)
        draw_string("Documents retrouvés :", 2, 204, (255, 255, 255), os_color)
    else:
        fill_rect(1, 204, 320, 18, os_color)

def personnage(position, aff=0):
    color = (255, 0, 0) if aff else (0, 0, 0)
    fill_rect(position[0] - 5, position[1] - 1, 10, 2, color)
    fill_rect(position[0] - 1, position[1] - 5, 2, 10, color)

def collision(point_rouge, position):
    return (point_rouge[0] >= position[0] - 5 and point_rouge[0] <= position[0] + 5 and
            point_rouge[1] >= position[1] - 5 and point_rouge[1] <= position[1] + 5)

def move(position, bordure, vitesse):
    mvt = position[:]
    is_key_pressed = False

    if keydown(1):  # Haut
        mvt[1] = max(position[1] - vitesse, bordure[2])
        is_key_pressed = True

    if keydown(2):  # Bas
        mvt[1] = min(position[1] + vitesse, bordure[3])
        is_key_pressed = True

    if keydown(3):  # Droite
        mvt[0] = min(position[0] + vitesse, bordure[1])
        is_key_pressed = True

    if keydown(0):  # Gauche
        mvt[0] = max(position[0] - vitesse, bordure[0])
        is_key_pressed = True

    return mvt if is_key_pressed else position

def jeu(point, temps_a_faire):
    position = [160, 111]
    start_time = None
    bordure = (5, 319, 5, 201)  # x_min, x_max, y_min, y_max

    # Affichage initial
    initialisation(etat=0)
    personnage(position, 1)

    # Choix d'une ville aléatoire
    liste_villes = list(emplacement_villes.keys())
    index_aleatoire = randint(0, len(liste_villes) - 1)
    ville = liste_villes[index_aleatoire]
    point_rouge = emplacement_villes[ville]

    # Dessine le point rouge
    draw_circle(point_rouge[0], point_rouge[1], 3, (255, 0, 0))
    draw_string(ville, point_rouge[0] + 10, point_rouge[1] - 20, (255, 0, 0))

    while True:
        sleep(0.1)
        personnage(position, 0)  # Efface ancienne position
        position = move(position, bordure, vitesse=5)
        personnage(position, 1)  # Dessine nouvelle position

        if collision(point_rouge, position):
            end_time = time()
            if start_time is None:
                start_time = time()
            elapsed_time = end_time - start_time

            if elapsed_time <= temps_a_faire:
                point += 1
                temps_a_faire -= 0.1
                draw_string("Bravo, tu gagnes 1 point!", 100, 100, (0, 255, 0))
            else:
                draw_string("Dommage, un peu trop lent...", 100, 100, (255, 0, 0))
            break

    return point, temps_a_faire

# Variables initiales
point = 0
temps_a_faire = 3
emplacement_villes = {
    "Paris": (143, 45), "New York": (80, 60), "Mexico": (60, 95), "Moscou": (170, 43),
    "Tokyo": (265, 60), "Vancouver": (40, 50), "Mumbai": (213, 95), "Buenos Aires": (93, 160),
    "Lagos": (143, 107), "Johannesburg": (168, 145)
}

# Boucle principale du jeu
while point < 10:
    point, temps_a_faire = jeu(point, temps_a_faire)

# Fin du jeu
draw_string("Félicitations, vous avez gagné !", 100, 100, (0, 255, 0))

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.