Partage
  • Partager sur Facebook
  • Partager sur Twitter

[]PyGame]Effacer l'écran

Anonyme
    24 août 2014 à 15:32:14

    Bonjour j'ai une image de fond auto générée et je peux la rebliter où je veux, j'ai fais des tests. Mais voilà, j'utilise font et donc écris sur ce fond et je souhaite qu'a chaque tour de boucle on efface cet écran, j'ai testé dans mon code que voici :

    import  pygame
    import  math
    import  time
    import  sys
    import  os
    from    pygame.locals  import *
    
    # Ignore these 3 functions. Scroll down for the relevant code.
    
    def create_background(width, height):
            colors = [(255, 255, 255), (212, 212, 212)]
            background = pygame.Surface((width, height))
            tile_width = 20
            y = 0
            while y < height:
                    x = 0
                    while x < width:
                            row = y // tile_width
                            col = x // tile_width
                            pygame.draw.rect(
                                    background, 
                                    colors[(row + col) % 2],
                                    pygame.Rect(x, y, tile_width, tile_width))
                            x += tile_width
                    y += tile_width
            return background
    
    def is_trying_to_quit(event):
            pressed_keys = pygame.key.get_pressed()
            alt_pressed = pressed_keys[pygame.K_LALT] or pressed_keys[pygame.K_RALT]
            x_button = event.type == pygame.QUIT
            altF4 = alt_pressed and event.type == pygame.KEYDOWN and event.key == pygame.K_F4
            escape = event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
            return x_button or altF4 or escape
    
    def run_demos(width, height, fps):
            pygame.init()
            screen = pygame.display.set_mode((width, height))
            pygame.display.set_caption('Cube 3D')
            background = create_background(width, height)
            clock = pygame.time.Clock()
            the_world_is_a_happy_place = 0
            while True:
                    the_world_is_a_happy_place += 1
                    for event in pygame.event.get():
                            if is_trying_to_quit(event):
                                    return
                            elif event.type == MOUSEBUTTONDOWN and event.button == 5: #la molette descend
                                fps -= 5
                            elif event.type == MOUSEBUTTONDOWN and event.button == 4: #la molette monte
                                fps += 5
                    screen.blit(background, (0, 0))
                    do_line_demo(screen, the_world_is_a_happy_place, background, screen)
                    pygame.display.flip()
                    clock.tick(fps)
                    os_clear_command = 'cls' if sys.platform == 'win32' else 'clear'
                    os.system(os_clear_command) #on efface l'écran
                    print("FPS >>> {}".format(fps))
    
    def rotate_3d_points(points, angle_x, angle_y, angle_z, background ,screen):
            _c1 = (0, 0, 0)
            _c2 = (0, 0, 0)
            _c3 = (0, 0, 0)
            _c4 = (0, 0, 0)
            _c5 = (0, 0, 0)
            _c6 = (0, 0, 0)
            _c7 = (0, 0, 0)
            _c8 = (0, 0, 0)
            font = pygame.font.Font(None, 22)
            new_points = []
            for point in points:
                    x = point[0]
                    y = point[1]
                    z = point[2]
                    new_y = y * math.cos(angle_x) - z * math.sin(angle_x)
                    new_z = y * math.sin(angle_x) + z * math.cos(angle_x)
                    y = new_y
                    # isn't math fun, kids?
                    z = new_z
                    new_x = x * math.cos(angle_y) - z * math.sin(angle_y)
                    new_z = x * math.sin(angle_y) + z * math.cos(angle_y)
                    x = new_x
                    z = new_z
                    new_x = x * math.cos(angle_z) - y * math.sin(angle_z)
                    new_y = x * math.sin(angle_z) + y * math.cos(angle_z)
                    x = new_x
                    y = new_y
                    new_points.append([x, y, z])
                    _cX, _cY, _cZ = (x, y, z)
                    _cX = math.fabs(_cX) * 50 - 1
                    _cY = math.fabs(_cY) * 37.5 - 1
                    if _c1 == (0, 0, 0) : _c1 = (x, y, z) ; print(_c1) ; c1  = font.render("{0}".format(_c1), 1, (10,10,10)) ; background.blit(c1, (_cX,_cY)) ; screen.blit(background,(0,0))
                    if _c2 == (0, 0, 0) : _c2 = (x, y, z) ; print(_c2) ; c2  = font.render("{0}".format(_c2), 1, (10,10,10)) ; background.blit(c2, (_cX,_cY)) ; screen.blit(background,(0,0))
                    if _c3 == (0, 0, 0) : _c3 = (x, y, z) ; print(_c3) ; c3  = font.render("{0}".format(_c3), 1, (10,10,10)) ; background.blit(c3, (_cX,_cY)) ; screen.blit(background,(0,0))
                    if _c4 == (0, 0, 0) : _c4 = (x, y, z) ; print(_c4) ; c4  = font.render("{0}".format(_c4), 1, (10,10,10)) ; background.blit(c4, (_cX,_cY)) ; screen.blit(background,(0,0))
                    if _c5 == (0, 0, 0) : _c5 = (x, y, z) ; print(_c5) ; c5  = font.render("{0}".format(_c5), 1, (10,10,10)) ; background.blit(c5, (_cX,_cY)) ; screen.blit(background,(0,0))
                    if _c6 == (0, 0, 0) : _c6 = (x, y, z) ; print(_c6) ; c6  = font.render("{0}".format(_c6), 1, (10,10,10)) ; background.blit(c6, (_cX,_cY)) ; screen.blit(background,(0,0))
                    if _c7 == (0, 0, 0) : _c7 = (x, y, z) ; print(_c7) ; c7  = font.render("{0}".format(_c7), 1, (10,10,10)) ; background.blit(c7, (_cX,_cY)) ; screen.blit(background,(0,0))
                    if _c8 == (0, 0, 0) : _c8 = (x, y, z) ; print(_c8) ; c8  = font.render("{0}".format(_c8), 1, (10,10,10)) ; background.blit(c8, (_cX,_cY)) ; screen.blit(background,(0,0))
            return new_points
    
    def do_line_demo(surface, counter, background, screen):
            color = (0, 0, 0) # black
            cube_points = [[-1, -1, 1],[-1, 1, 1],[1, 1, 1],[1, -1, 1],[-1, -1, -1],[-1, 1, -1],[1, 1, -1],[1, -1, -1]]
                    
            connections = [(0, 1),(1, 2),(2, 3),(3, 0),(4, 5),(5, 6),(6, 7),(7, 4),(0, 4),(1, 5),(2, 6),(3, 7)]
                    
            t = counter * 2 * 3.141592653589 / 60 # this angle is 1 rotation per second
            
            # rotate about x axis every 2 seconds
            # rotate about y axis every 4 seconds
            # rotate about z axis every 6 seconds
            points = rotate_3d_points(cube_points, t / 2, t / 4, t / 6, background, screen)
            flattened_points = []
            for point in points:
                    flattened_points.append(
                            (point[0] * (1 + 1.0 / (point[2] + 3)),
                             point[1] * (1 + 1.0 / (point[2] + 3))))
            
            for con in connections:
                    p1 = flattened_points[con[0]]
                    p2 = flattened_points[con[1]]
                    x1 = p1[0] * 60 + 200
                    y1 = p1[1] * 60 + 150
                    x2 = p2[0] * 60 + 200
                    y2 = p2[1] * 60 + 150
                    
                    # This is the only line that really matters
                    pygame.draw.line(surface, color, (x1, y1), (x2, y2), 4)
                    
            
    run_demos(800, 600, 1)

    Mais j'obtiens ceci :

    De plus tous mes textes sont au même endroit alors que je veux qu'ils suivent "leur" 'coin' de cube pour afficher ses coordonnées.

    Merci de votre aide, Python34

    • Partager sur Facebook
    • Partager sur Twitter
      24 août 2014 à 15:38:43

      Ben y a pas 36000 solutions, tu reblitte le fond de ta fenetre ;)
      • Partager sur Facebook
      • Partager sur Twitter
      Anonyme
        24 août 2014 à 15:40:49

        Je l'ai fait mais sa marche pas. Voir ligne n°92 à n°98, en fin de ligne (déplacer la barre de défilement vers la droite).

        Il faut peut etre que je flip ?

        edit :  même en flipant ca marche pas, et mon cube clignote

        -
        Edité par Anonyme 24 août 2014 à 15:42:32

        • Partager sur Facebook
        • Partager sur Twitter
          24 août 2014 à 17:10:22

          Désolé mais ton code est trop dégueu (mais rassure toi j'ai vu pire) et je n'ai pas envie de me plonger dedans (sans méchanceté gratuite), par contre tu peux essayer de mettre des prints de debug histoire de voir si les conditions sont bien remplies.

          Ah si j'ai trouvé, c'est juste que tu n'appelle pas dans ta boucle principale la fonction chargée de reblitter le fond, et en plus tu ne flip pas (sisi il faut le faire).

          -
          Edité par Derzal 24 août 2014 à 17:10:48

          • Partager sur Facebook
          • Partager sur Twitter

          []PyGame]Effacer l'écran

          × 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