|
|
|
# Pyxel Studio
|
|
|
|
# Importations ============================================================
|
|
|
|
import pyxel
|
|
|
|
import random as rd
|
|
|
|
# CONSTANTES et variables globales lues directement =========================
|
|
|
|
XMAX = 128
|
|
|
|
YMAX = 128
|
|
|
|
# Déclaration des classes
|
|
|
|
class Chien :
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.x = 60 # coordonnée x du coin haut à gauche du carré
|
|
|
|
self.y = 60 # coordonnée y du coin haut à gauche du carré
|
|
|
|
self.puni = False
|
|
|
|
|
|
|
|
def afficher(self:'Chien') -> None:
|
|
|
|
"""Affiche le chien"""
|
|
|
|
pyxel.blt(self.x,self.y,0,0,0,8,8)
|
|
|
|
|
|
|
|
def dep_x(self,dist) :
|
|
|
|
self.x = self.x + dist
|
|
|
|
if self.x < 0:
|
|
|
|
self.x = 0
|
|
|
|
elif self.x > XMAX:
|
|
|
|
self.x = XMAX
|
|
|
|
|
|
|
|
def dep_y(self,dist) :
|
|
|
|
self.y = self.y + dist
|
|
|
|
if self.y < 0:
|
|
|
|
self.y = 0
|
|
|
|
elif self.y > YMAX:
|
|
|
|
self.y = YMAX
|
|
|
|
|
|
|
|
class Joueur :
|
|
|
|
def __init__(self,chien:'Chien',maitre:'Maitre',poules:list) :
|
|
|
|
self.chien = chien
|
|
|
|
self.maitre = maitre
|
|
|
|
self.poules = poules
|
|
|
|
self.nb_poules = len(poules)
|
|
|
|
|
|
|
|
def se_deplacer(self) :
|
|
|
|
if not self.chien.puni :
|
|
|
|
if pyxel.btn(pyxel.KEY_RIGHT):
|
|
|
|
self.chien.dep_x(1)
|
|
|
|
if pyxel.btn(pyxel.KEY_LEFT):
|
|
|
|
self.chien.dep_x(-1)
|
|
|
|
if pyxel.btn(pyxel.KEY_DOWN):
|
|
|
|
self.chien.dep_y(1)
|
|
|
|
if pyxel.btn(pyxel.KEY_UP):
|
|
|
|
self.chien.dep_y(-1)
|
|
|
|
|
|
|
|
def punition(self) -> bool :
|
|
|
|
if self.maitre.x >= self.chien.x and self.maitre.y >= self.chien.y and self.maitre.x <= self.chien.x + 8 and self.maitre.y <= self.chien.y + 8 :
|
|
|
|
self.chien.puni = True
|
|
|
|
|
|
|
|
def collecter(self) :
|
|
|
|
if self.nb_poules :
|
|
|
|
print(self.nb_poules)
|
|
|
|
for i in range(self.nb_poules) :
|
|
|
|
if self.poules[i].collecte == False and self.poules[i].x >= self.chien.x and self.poules[i].y >= self.chien.y and self.poules[i].x <= self.chien.x + 8 and self.poules[i].y <= self.chien.y + 8 :
|
|
|
|
print("collision")
|
|
|
|
self.poules[i].collecte = True
|
|
|
|
|
|
|
|
|
|
|
|
class Maitre :
|
|
|
|
def __init__(self,chien:'Chien') :
|
|
|
|
self.poursuite = False
|
|
|
|
self.blesse = False
|
|
|
|
self.chien = chien
|
|
|
|
self.x = rd.randint(0,XMAX)
|
|
|
|
self.y = rd.randint(0,YMAX)
|
|
|
|
|
|
|
|
def deplacer(self) :
|
|
|
|
rand_dist_x = rd.randint(0,4)
|
|
|
|
rand_dist_x = rd.choice((-1,0,1))*rand_dist_x
|
|
|
|
rand_dist_y = rd.randint(0,4)
|
|
|
|
rand_dist_y = rd.choice((-1,0,1))*rand_dist_y
|
|
|
|
self.x += rand_dist_x
|
|
|
|
self.y += rand_dist_y
|
|
|
|
if self.x > XMAX :
|
|
|
|
self.x = XMAX
|
|
|
|
elif self.x < 0 :
|
|
|
|
self.x = 0
|
|
|
|
if self.y > YMAX :
|
|
|
|
self.y = YMAX
|
|
|
|
elif self.y < 0 :
|
|
|
|
self.y = 0
|
|
|
|
|
|
|
|
def afficher(self) :
|
|
|
|
pyxel.blt(self.x,self.y,0,8,0,8,8)
|
|
|
|
|
|
|
|
class Poule :
|
|
|
|
def __init__(self) :
|
|
|
|
self.x = rd.randint(0,XMAX)
|
|
|
|
self.y = rd.randint(0,YMAX)
|
|
|
|
self.collecte = False
|
|
|
|
|
|
|
|
def si_collecte(self) :
|
|
|
|
if self.collecte :
|
|
|
|
self.x = None
|
|
|
|
self.y = None
|
|
|
|
|
|
|
|
def afficher(self) :
|
|
|
|
pyxel.blt(self.x,self.y,0,0,8,8,8)
|
|
|
|
|
|
|
|
class Map:
|
|
|
|
def __init__(self,maitre:"Maitre",chien:"Chien") :
|
|
|
|
self.maitre = maitre
|
|
|
|
self.chien = chien
|
|
|
|
self.map = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
|
|
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
|
|
|
|
|
|
|
|
class Jeu:
|
|
|
|
"""Classe intégrant la gestion du jeu."""
|
|
|
|
def __init__(self) -> None:
|
|
|
|
|
|
|
|
# Création de la fenêtre graphique
|
|
|
|
pyxel.init(128, 128, title="Projet chien")
|
|
|
|
pyxel.load("res.pyxres")
|
|
|
|
|
|
|
|
|
|
|
|
self.chien = Chien()
|
|
|
|
self.poules = []
|
|
|
|
for i in range(4) :
|
|
|
|
self.poules.append(Poule())
|
|
|
|
self.maitre = Maitre(self.chien)
|
|
|
|
self.joueur = Joueur(self.chien,self.maitre,self.poules)
|
|
|
|
pyxel.run(self.controler, self.afficher)
|
|
|
|
|
|
|
|
def controler(self:'Jeu') -> None:
|
|
|
|
"""déplacement avec les touches de directions"""
|
|
|
|
|
|
|
|
self.maitre.deplacer()
|
|
|
|
self.joueur.se_deplacer()
|
|
|
|
self.joueur.punition()
|
|
|
|
self.joueur.collecter()
|
|
|
|
#self.deplacer_ennemis()
|
|
|
|
#self.supprimer_ennemis_touches()
|
|
|
|
|
|
|
|
def se_deplacer(self:'Jeu')->None :
|
|
|
|
if pyxel.btn(pyxel.KEY_RIGHT) :
|
|
|
|
self.chien.dep_x(2)
|
|
|
|
if pyxel.btn(pyxel.KEY_LEFT) :
|
|
|
|
self.chien.dep_x(-2)
|
|
|
|
if pyxel.btn(pyxel.KEY_DOWN) :
|
|
|
|
self.chien.dep_y(2)
|
|
|
|
if pyxel.btn(pyxel.KEY_TOP) :
|
|
|
|
self.chien.dep_y(-2)
|
|
|
|
|
|
|
|
def afficher(self:'Jeu') :
|
|
|
|
|
|
|
|
pyxel.cls(0) # efface le contenu de la fenetre
|
|
|
|
|
|
|
|
self.chien.afficher()
|
|
|
|
self.maitre.afficher()
|
|
|
|
for i in range(4) :
|
|
|
|
if not self.poules[i].collecte :
|
|
|
|
self.poules[i].afficher()
|
|
|
|
|
|
|
|
# Programme Principal !
|
|
|
|
application = Jeu()
|