![]()
lien à conserver:
www.nuitducode.net/pyxel/studio/8u3lxb5g
|
Durant l'épreuve de la Nuit du Code, vous disposerez de 6 heures pour créer un jeu avec Python / Pyxel. Pour cela, vous pouvez utiliser les ressources (fichiers LES RÈGLES
QUELQUES CONSEILS
ndc.py
import pyxel
CELL_SIZE = 40
GRID_SIZE = 3
WINDOW_SIZE = CELL_SIZE * GRID_SIZE
class Morpion:
def __init__(self):
pyxel.init(WINDOW_SIZE, WINDOW_SIZE, title="Morpion de Débutant")
self.board = [['' for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]
self.current_player = 'X'
self.winner = None
pyxel.mouse(True)
pyxel.run(self.update, self.draw)
def update(self):
if pyxel.btnp(pyxel.MOUSE_LEFT_BUTTON) and not self.winner:
col = pyxel.mouse_x // CELL_SIZE
row = pyxel.mouse_y // CELL_SIZE
if self.board[row][col] == '':
self.board[row][col] = self.current_player
self.winner = self.check_winner()
if not self.winner:
self.current_player = 'O' if self.current_player == 'X' else 'X'
def draw(self):
pyxel.cls(0)
# Grille
for i in range(1, GRID_SIZE):
pyxel.line(i * CELL_SIZE, 0, i * CELL_SIZE, WINDOW_SIZE, 7)
pyxel.line(0, i * CELL_SIZE, WINDOW_SIZE, i * CELL_SIZE, 7)
# Symboles
for row in range(GRID_SIZE):
for col in range(GRID_SIZE):
x = col * CELL_SIZE + CELL_SIZE // 2
y = row * CELL_SIZE + CELL_SIZE // 2
val = self.board[row][col]
if val == 'X':
pyxel.text(x - 4, y - 3, 'X', 8)
elif val == 'O':
pyxel.text(x - 4, y - 3, 'O', 11)
# Fin de partie
if self.winner:
msg = f"Gagnant: {self.winner}" if self.winner != 'Égalité' else "Égalité !"
pyxel.text(10, WINDOW_SIZE - 10, msg, 10)
def check_winner(self):
b = self.board
# Lignes / Colonnes
for i in range(GRID_SIZE):
if b[i][0] == b[i][1] == b[i][2] != '':
return b[i][0]
if b[0][i] == b[1][i] == b[2][i] != '':
return b[0][i]
# Diagonales
if b[0][0] == b[1][1] == b[2][2] != '':
return b[0][0]
if b[0][2] == b[1][1] == b[2][0] != '':
return b[0][2]
# Égalité
if all(b[r][c] != '' for r in range(GRID_SIZE) for c in range(GRID_SIZE)):
return 'Égalité'
return None
Morpion()
Documentation
BIBLIOTHÈQUES
Si vous utilisez des bibliothèques tierces pour votre projet (c'est-à-dire de bibliothèques qui ne sont pas des bibliothèques standard de Python), indiquez-les ci-dessous (noms séparés par des virgules). Exemple: numpy,pandas Remarque: ne déclarer que des bibliothèques tierces, pas des bibliothèques standard de Python. Par exemple, random est une bibliothèque standard, pas une bibliothèque tierce. DOCUMENTATION PYXEL
|