Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Cube 3D][PyGame][Régulation des FPS (molette)]

Anonyme
    24 août 2014 à 12:10:43

    Bonjour je vous présente un code de cube en 3D, et je ne sais pas comment l'améliorer :

    import pygame
    from    pygame.locals  import *
    import math
    import time
    
    # 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()
            demos = [do_line_demo]
            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
                            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                                    demos = demos[1:]
                            elif event.type == MOUSEBUTTONDOWN and event.button == 5: #la molette descend
                                fps -= 10
                            elif event.type == MOUSEBUTTONDOWN and event.button == 4: #la molette monte
                                fps += 10
                    screen.blit(background, (0, 0))
                    if len(demos) == 0:
                            return
                    demos[0](screen, the_world_is_a_happy_place)
                    pygame.display.flip()
                    clock.tick(fps)
                    print("FPS >>> {}".format(fps))
    
    # Everything above this line is irrelevant to this tutorial.
    
    def rotate_3d_points(points, angle_x, angle_y, angle_z):
            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])
            return new_points
    
    def do_line_demo(surface, counter):
            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)
            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(400, 300, 60)



    • Partager sur Facebook
    • Partager sur Twitter
    Anonyme
      25 août 2014 à 11:33:32

      Salut,

      et je ne sais pas comment l'améliorer

      c'est-à-dire ? Quels sont les améliorations que tu voudrais effectuer ?

      • Partager sur Facebook
      • Partager sur Twitter
      Anonyme
        25 août 2014 à 13:00:41

        Aucunes idées ! Je ne sais pas, peut être le passer en POO, pouvoir mettre des images dynamiques utilisant un module type 'camera' sur les faces, régler depuis l'interface la taille du cube (donc pas par la console) et changeable a tout moment . . .
        • Partager sur Facebook
        • Partager sur Twitter
        Anonyme
          25 août 2014 à 15:40:34

          On "améliore" pas un code en ajoutant des fonctionnalités au pifomètre... Ça dépend de comment tu te sers de ce code et des fonctionnalités dont tu as réellement besoin en plus.

          • Partager sur Facebook
          • Partager sur Twitter

          [Cube 3D][PyGame][Régulation des FPS (molette)]

          × 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