
#le personnage
class Personnage():
    
    def __init__(self):
        self.x=50
        self.y=250
        self.vx=20
        self.vy=0
        self.ax=0
        self.ay=9
        self.dt=0.1
        self.rayon=10

        self.mur=False
        self.debout=False
        
    def evoluer(self):
        self.vx=self.vx+self.ax*self.dt
        self.vy=self.vy+self.ay*self.dt
        self.x=self.x+self.vx*self.dt
        self.y=self.y+self.vy*self.dt       


    def afficher(self):
        print("(x,y): "+str(self.x)+","+str(self.y)+"  vx,vy: "+str(self.vx)+","+str(self.vy))

    def collision(self,obs):
        haut=(self.y+self.rayon<obs.y)
        bas=(self.y-self.rayon>obs.y+obs.ly)
        gauche=(self.x+self.rayon<obs.x)
        droite=(self.x-self.rayon>obs.x+obs.lx)
        collision=not(haut or bas or gauche or droite)
        return collision


#un obstacle est betement un carre
class Obstacle():

    def __init__(self,dx,dy,dx1,dy1):
        self.x=dx
        self.y=dy
        self.lx=dx1
        self.ly=dy1


#un jeu est un personnage et des obstacles
class Jeu():

    def __init__(self):
        self.pj=Personnage()
        self.obstacles=[]
        self.obstacles+=[Obstacle(50,400,400,50)]
        self.obstacles+=[Obstacle(0,0,50,450)]
        self.obstacles+=[Obstacle(450,0,50,450)]

    def evoluer(self):
        global done
        # -- gestion evenements
        for event in pygame.event.get():

            #utilisateur quitte
            if event.type == pygame.QUIT: 
                done = True
                
            #saut du personnage
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    print("up")
                    self.pj.vy=-50

        self.pj.evoluer()

    #dessiner le jeu        
    def dessiner(self):
        WHITE = (0xFF, 0xFF, 0xFF)
        RED = (0xFF, 0x00, 0x00)
        BLUE = (0x00, 0xFF, 0x00)
        BLACK = (0x00, 0x00, 0x00)
        screen.fill(WHITE)

        #dessin obstacle
        for obs in self.obstacles:
            pygame.draw.rect(screen,BLACK,(obs.x,obs.y,obs.lx,obs.ly))

        #dessin personnage
        rayon=self.pj.rayon
        collision=False
        for obs in self.obstacles:
            if (self.pj.collision(obs)):
                collision=True

        if (collision == True):
            pygame.draw.rect(screen,RED,(self.pj.x-rayon,self.pj.y-rayon,2*rayon,2*rayon))
        else:
            pygame.draw.rect(screen,BLUE,(self.pj.x-rayon,self.pj.y-rayon,2*rayon,2*rayon))

       
        


import pygame
pygame.init()
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Moteur")


#boolean boucle
done = False
#creation du jeu
jeu=Jeu()

#gestion horloge
clock = pygame.time.Clock()
# -------- boucle principale
while not done:
    
        
            
    # --- evolution du jeu
    jeu.evoluer()
        
    # --- affichage
    jeu.dessiner()
       
    # --- mise a jour graphique
    pygame.display.flip()
    # --- attente
    clock.tick(60)

pygame.quit()
   

