Cours 4 Modules

Tuples

Emballage et déballage

t = 1, 2, 3
print(t)

a, b, c = t
print(a, b, c)
(1, 2, 3) 1 2 3

Les énumérations

L = ['un', 'deux', 'trois']
print(enumerate(L))        # affiche <enumerate at 0x10035e570>
print(list(enumerate(L)))  # affiche [(0, 'un'), (1, 'deux'), (2, 'trois')]
for i, value in enumerate(L):
    print("l'élément d'indice", i, "est", value)
l'élément d'indice 0 est un l'élément d'indice 1 est deux l'élément d'indice 2 est trois

Modules

Utilisation d’un module

Définition d’un module

from turtle import color, up, down, goto, done

def polygon(center, nbsides, radius, col='black'):
    # Dessiner un polygone régulier

def square(center, radius, col='black'):
    polygon(center, 4, radius, col)

Dessiner un polygone régulier

Polygone régulier

Polygone: Diagramme

Polygone: Code

from turtle import color, up, down, goto, done
from math import cos, sin, pi


def polygon(center, nb_sides, radius, col="black"):
    x, y = center  # déballage de séquence
    angle = 2 * pi / nb_sides
    up()
    goto(x + radius, y)
    down()
    color(col)
    for i in range(1, nb_sides+1):
        goto(
            x + radius * cos(i * angle),
            y + radius * sin(i * angle),
        )


def square(center, radius, col="black"):
    polygon(center, 4, radius, col)

Définition d’un module

from shape import square, polygone, done

polygon((150, 150), 5, 100, 'red')
square((-50, -50), 50, 'blue')

done()

import égal exécution

La variable __name__

Application interactive

stge.py: Utilisation

import stge

def setup():
  # cette fonction doit renvoyer
  # l'état initial de l'application
  return state

def loop(state):
  # cette fonction reçoit l'état précédent
  # de l'application et renvoie l'état suivant
  return state

stge.run(setup, loop)

Fonctions disponibles

Utilisez la documentation incluse dans Zed pour savoir comment utiliser ces fonctions

Exemple

import stge

def setup():
  return 0

def loop(state):
  width, height = stge.size()

  for key in stge.keypresses():
    if key == "SPACE":
      state += 1
    if key == "q":
      stge.quit()

  stge.write_at(width//2, height//2, state)
  return state

stge.run(setup, loop)

Game Mode: déplacements

import stge

def setup():
    return 0, 0

def loop(state):
    x, y = state
    xn, yn = x, y

    width, height = stge.size()

    for key in stge.keypresses():
        if key == "q":
            stge.quit()
        if key == "RIGHT":
            xn += 1
        if key == "LEFT":
            xn -= 1
        if key == "UP":
            yn -= 1
        if key == "DOWN":
            yn += 1

    if xn >= 0 and xn < width and yn >= 0 and yn < height:
        x, y = xn, yn

    stge.write_at(x, y, "ጰ", fg=(0, 255, 0))

    return x, y

stge.run(setup, loop)

Game Mode: labyrinthe

import stge

maze = [
    "══╦═══════════════╦═════╦═══════════╦═════╦═══════╗",
    "  ║               ║     ║           ║     ║       ║",
    "║ ╚═══╦══ ╔═════╗ ╚═══╗ ║ ══╗ ╔═══╗ ╚══ ║ ║ ══╦══ ║",
    "║     ║   ║     ║     ║ ║   ║ ║   ║     ║ ║   ║   ║",
    "╠═══╗ ║ ╔═╝ ══╗ ╠════ ║ ╚══ ║ ║ ║ ╚═╦═══╬═╩══ ║ ══╣",
    "║   ║   ║     ║ ║     ║     ║   ║   ║   ║     ║   ║",
    "║ ══╩═══╩═════╝ ║ ╔═══╝ ╔═══╬═══╩═╗ ║ ║ ║ ╔═══╣ ║ ║",
    "║               ║ ║     ║   ║     ║   ║   ║   ║ ║ ║",
    "╠══════ ╔══════ ║ ╚═════╝ ║ ║ ╔══ ║ ╔═╬═══╣ ║ ╚═╣ ║",
    "║       ║       ║         ║ ║ ║   ║ ║ ║   ║ ║   ║ ║",
    "║ ╔═════╝ ╔═════╩═══════╦═╣ ║ ║ ╔═╝ ║ ║ ║ ║ ╠══ ║ ║",
    "║ ║       ║             ║ ║   ║ ║     ║ ║   ║   ║ ║",
    "║ ╚═══╗ ══╣ ══╗ ╔═════╗ ║ ╚═╦═╝ ║ ══╦═╝ ╠═══╣ ╔═╝ ║",
    "║     ║   ║   ║ ║     ║ ║   ║   ║   ║   ║   ║ ║   ║",
    "║ ╔═╗ ╠══ ╚══ ║ ║ ══╗ ║ ╚═╗ ║ ══╩═╦═╝ ╔═╝ ╔═╝ ║ ══╣",
    "║ ║ ║ ║       ║ ║   ║ ║   ║ ║     ║   ║   ║   ║   ║",
    "║ ║ ║ ║ ╔═══╗ ╠═╩═╗ ║ ╠══ ║ ╠═══╗ ║ ╔═╣ ══╣ ══╣ ║ ║",
    "║   ║ ║ ║   ║ ║   ║ ║ ║     ║   ║ ║ ║ ║   ║   ║ ║ ║",
    "╠══ ║ ╚═╝ ║ ╚═╝ ║ ║ ║ ╚═════╝ ║ ║ ║ ║ ║ ║ ╚══ ╚═╝ ║",
    "║   ║     ║     ║   ║         ║     ║   ║         E",
    "╚═══╩═════╩═════╩═══╩═════════╩═════╩═══╩══════════",
]


def draw_maze():
    for i, line in enumerate(maze):
        stge.write_at(0, i, line)

def setup():
    return 0, 1

def loop(state):
    x, y = state
    xn, yn = x, y

    for key in stge.keypresses():
        if key == "q":
            stge.quit()
        if key == "RIGHT":
            xn += 1
        if key == "LEFT":
            xn -= 1
        if key == "UP":
            yn -= 1
        if key == "DOWN":
            yn += 1

    if maze[yn][xn] == " ":
        x, y = xn, yn

    draw_maze()
    stge.write_at(x, y, "ጰ", fg=(0, 255, 0))

    return x, y

stge.run(setup, loop)