Partage
  • Partager sur Facebook
  • Partager sur Twitter

Programme Pygame

    23 janvier 2015 à 14:15:26

    Bonjour je fais un petit programme pour faire bouger une boule et la faire rebondir sur les bords, mais je suis confronté à un bug que je ne comprends pas. Voici le programme :

    import math
    import pygame
    import random
    from math import *
    
    pygame.init()
    
    BLACK=[0,0,0]
    WHITE=[255,255,255]
    RED=[255,0,0]
    GREEN=[0,255,0]
    
    SIZE=[800,500]
    screen=pygame.display.set_mode(SIZE)
    pygame.display.set_caption("cercle")
    
    screen.fill(BLACK)
    pygame.display.flip()
    
    pi=math.pi
    
    #########################################################################################################################
    #------------------------- Fonction dessCercle --------------------------------------------------------------------------
    #########################################################################################################################
    
    def dessCercle(couleur, nbRebonds):
        direction=1  # 1: haut-droite ; 2: bas-droite ; 3: bas-gauche ; 4:haut-gauche
        
        ########### choix bord ############
        absCentre=0
        ordCentre=0
        bord=random.randint(1,4) # 1: haut ; 2: droite ; 3: bas ; 4: gauche
        if bord==1:
            absCentre=random.randint(20,780)
            ordCentre=20
        elif bord==2:
            absCentre=780
            ordCentre=random.randint(20,480)
        elif bord==3:
            absCentre=random.randint(20,780)
            ordCentre=480
        elif bord==4:
            absCentre=20
            ordCentre=random.randint(20,480)
        ########### choix bord ############
        direction=bord
    #////////////////////////////////////////////////////////////////////////////////
    
        for i in range(nbRebonds):
            if direction==1 and bord==1:     #direction avant choc, et bord touché
                direction=2    #direction prise
                while ordCentre<=480 and absCentre<=780:
                        ordCentre=ordCentre+int(round((sqrt(2)/2)))
                        absCentre=absCentre+int(round((sqrt(2)/2)))
                        pygame.draw.circle(screen,couleur,[absCentre,ordCentre],20,0)
                        pygame.display.flip()
                        pygame.draw.circle(screen,BLACK,[absCentre,ordCentre],20,0)
                        pygame.time.delay(5)
                if absCentre>=780:
                    bord=2
                elif ordCentre>=480:
                    bord=3
                continue
    
            if direction==1 and bord==2:
                direction=4
                while ordCentre>=20 and absCentre>=20:
                        ordCentre=ordCentre-int(round((sqrt(2)/2)))
                        absCentre=absCentre-int(round((sqrt(2)/2)))
                        pygame.draw.circle(screen,couleur,[absCentre,ordCentre],20,0)
                        pygame.display.flip()
                        pygame.draw.circle(screen,BLACK,[absCentre,ordCentre],20,0)
                        pygame.time.delay(5)
                if absCentre<=20:
                    bord=4
                elif ordCentre<=20:
                    bord=1
                continue
            
            if direction==2 or bord==2:
                direction=3
                while absCentre>=20 and ordCentre<=480:
                        absCentre=absCentre-int(round((sqrt(2)/2)))
                        ordCentre=ordCentre+int(round((sqrt(2)/2)))
                        pygame.draw.circle(screen,couleur,[absCentre,ordCentre],20,0)
                        pygame.display.flip()
                        pygame.draw.circle(screen,BLACK,[absCentre,ordCentre],20,0)
                        pygame.time.delay(5)
                if absCentre<=20:
                    bord=4
                elif ordCentre>=480:
                    bord=3
                continue
    
            if direction==2 and bord==3:
                direction=1
                while absCentre<=780 and ordCentre>=20:
                        absCentre=absCentre+int(round((sqrt(2)/2)))
                        ordCentre=ordCentre-int(round((sqrt(2)/2)))
                        pygame.draw.circle(screen,couleur,[absCentre,ordCentre],20,0)
                        pygame.display.flip()
                        pygame.draw.circle(screen,BLACK,[absCentre,ordCentre],20,0)
                        pygame.time.delay(5)
                if absCentre>=780:
                    bord=2
                elif ordCentre<=20:
                    bord=1
                continue
    
            if direction==3 and bord==3:
                direction=4
                while ordCentre>=20 and absCentre>=20:
                        ordCentre=ordCentre-int(round((sqrt(2)/2)))
                        absCentre=absCentre-int(round((sqrt(2)/2)))
                        pygame.draw.circle(screen,couleur,[absCentre,ordCentre],20,0)
                        pygame.display.flip()
                        pygame.draw.circle(screen,BLACK,[absCentre,ordCentre],20,0)
                        pygame.time.delay(5)
                if absCentre<=20:
                    bord=4
                elif ordCentre<=20:
                    bord=1
                continue
    
            if direction==3 and bord==4:
                direction=2
                while ordCentre<=480 and absCentre<=780:
                        ordCentre=ordCentre+int(round((sqrt(2)/2)))
                        absCentre=absCentre+int(round((sqrt(2)/2)))
                        pygame.draw.circle(screen,couleur,[absCentre,ordCentre],20,0)
                        pygame.display.flip()
                        pygame.draw.circle(screen,BLACK,[absCentre,ordCentre],20,0)
                        pygame.time.delay(5)
                if absCentre>=780:
                    bord=2
                elif ordCentre>=480:
                    bord=3
                continue
                    
            if direction==4 and bord==4:
                direction=1
                while absCentre<=780 and ordCentre>=20:
                        absCentre=absCentre+int(round((sqrt(2)/2)))
                        ordCentre=ordCentre-int(round((sqrt(2)/2)))
                        pygame.draw.circle(screen,couleur,[absCentre,ordCentre],20,0)
                        pygame.display.flip()
                        pygame.draw.circle(screen,BLACK,[absCentre,ordCentre],20,0)
                        pygame.time.delay(5)
                if absCentre>=780:
                    bord=2
                elif ordCentre<=20:
                    bord=1
                continue
    
            if direction==4 and bord==1:
                direction=3
                while absCentre>=20 and ordCentre<=480:
                        absCentre=absCentre-int(round((sqrt(2)/2)))
                        ordCentre=ordCentre+int(round((sqrt(2)/2)))
                        pygame.draw.circle(screen,couleur,[absCentre,ordCentre],20,0)
                        pygame.display.flip()
                        pygame.draw.circle(screen,BLACK,[absCentre,ordCentre],20,0)
                        pygame.time.delay(5)
                if absCentre<=20:
                    bord=4
                elif ordCentre>=480:
                    bord=3
                continue
    
    #########################################################################################################################
    #------------------------------------------------------------------------------------------------------------------------
    #########################################################################################################################
    
    dessCercle(GREEN,10)
    dessCercle(RED,10)
    
    
    
    done=False
    while done==False:
        for event in pygame.event.get():
            if event.type==pygame.MOUSEBUTTONDOWN:
                done=True
        screen.fill(WHITE)
    
    pygame.quit()



    Je trouve un probleme quand la boule arrive sur le bord 3 (bas), avec la direction 2 (bas-droite). La balle repart avec la direction 4 (haut-gauche), alors qu'elle devrait prendre la direction 1 (haut-droite). Je n'arrive pas à trouver pourquoi il me fait ca, et je ne me résout pas à copier le programme d'un autre... Pouvez vous m'aider si possible.
    Merci,
    Maxime

    -
    Edité par maximetoquebiau 24 janvier 2015 à 16:37:18

    • Partager sur Facebook
    • Partager sur Twitter
      23 janvier 2015 à 14:38:59

      Salut

      Je sais que tu es débutant et tout ça, mais tu vas prendre pour les autres désolé.

      Est-ce que ça t'ennuierai de lire un peu l'interface où tu écris ton message bon sang ? Il y a un bouton qui te permet de rentrer ton code pour qu'il soit formater afin d'être lu correctement, j'ai pas envie de faire des copier/coller tout le temps moi ! :colere2:

      • Partager sur Facebook
      • Partager sur Twitter
        23 janvier 2015 à 14:53:49

        ok



        -
        Edité par maximetoquebiau 23 janvier 2015 à 14:56:37

        • Partager sur Facebook
        • Partager sur Twitter

        Programme Pygame

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown