|
| 1 | +import pygame |
| 2 | +from pygame.locals import * |
| 3 | +from gameobjects.vector3 import Vector3 |
| 4 | +from math import * |
| 5 | +from random import randint |
| 6 | + |
| 7 | +SCREEN_SIZE = (640, 480) |
| 8 | +CUBE_SIZE = 300 |
| 9 | + |
| 10 | +def calculate_viewing_distance(fov, screen_width): |
| 11 | + |
| 12 | + d = (screen_width/2.0) / tan(fov/2.0) |
| 13 | + return d |
| 14 | + |
| 15 | + |
| 16 | +def run(): |
| 17 | + |
| 18 | + pygame.init() |
| 19 | + screen = pygame.display.set_mode(SCREEN_SIZE, 0) |
| 20 | + |
| 21 | + default_font = pygame.font.get_default_font() |
| 22 | + font = pygame.font.SysFont(default_font, 24) |
| 23 | + |
| 24 | + ball = pygame.image.load("ball.png").convert_alpha() |
| 25 | + |
| 26 | + # The 3D points |
| 27 | + points = [] |
| 28 | + |
| 29 | + fov = 90. # Field of view |
| 30 | + viewing_distance = calculate_viewing_distance(radians(fov), SCREEN_SIZE[0]) |
| 31 | + |
| 32 | + # Create a list of points along the edge of a cube |
| 33 | + for x in range(0, CUBE_SIZE+1, 20): |
| 34 | + edge_x = x == 0 or x == CUBE_SIZE |
| 35 | + |
| 36 | + for y in range(0, CUBE_SIZE+1, 20): |
| 37 | + edge_y = y == 0 or y == CUBE_SIZE |
| 38 | + |
| 39 | + for z in range(0, CUBE_SIZE+1, 20): |
| 40 | + edge_z = z == 0 or z == CUBE_SIZE |
| 41 | + |
| 42 | + if sum((edge_x, edge_y, edge_z)) >= 2: |
| 43 | + |
| 44 | + point_x = float(x) - CUBE_SIZE/2 |
| 45 | + point_y = float(y) - CUBE_SIZE/2 |
| 46 | + point_z = float(z) - CUBE_SIZE/2 |
| 47 | + |
| 48 | + points.append(Vector3(point_x, point_y, point_z)) |
| 49 | + |
| 50 | + # Sort points in z order |
| 51 | + def point_z(point): |
| 52 | + return point.z |
| 53 | + points.sort(key=point_z, reverse=True) |
| 54 | + |
| 55 | + center_x, center_y = SCREEN_SIZE |
| 56 | + center_x /= 2 |
| 57 | + center_y /= 2 |
| 58 | + |
| 59 | + ball_w, ball_h = ball.get_size() |
| 60 | + ball_center_x = ball_w / 2 |
| 61 | + ball_center_y = ball_h / 2 |
| 62 | + |
| 63 | + camera_position = Vector3(0.0, 0.0, -700.) |
| 64 | + camera_speed = Vector3(300.0, 300.0, 300.0) |
| 65 | + |
| 66 | + clock = pygame.time.Clock() |
| 67 | + |
| 68 | + while True: |
| 69 | + |
| 70 | + for event in pygame.event.get(): |
| 71 | + if event.type == QUIT: |
| 72 | + pygame.quit() |
| 73 | + quit() |
| 74 | + |
| 75 | + screen.fill((0, 0, 0)) |
| 76 | + |
| 77 | + pressed_keys = pygame.key.get_pressed() |
| 78 | + |
| 79 | + time_passed = clock.tick() |
| 80 | + time_passed_seconds = time_passed / 1000. |
| 81 | + |
| 82 | + direction = Vector3() |
| 83 | + if pressed_keys[K_LEFT]: |
| 84 | + direction.x = -1.0 |
| 85 | + elif pressed_keys[K_RIGHT]: |
| 86 | + direction.x = +1.0 |
| 87 | + |
| 88 | + if pressed_keys[K_UP]: |
| 89 | + direction.y = +1.0 |
| 90 | + elif pressed_keys[K_DOWN]: |
| 91 | + direction.y = -1.0 |
| 92 | + |
| 93 | + if pressed_keys[K_q]: |
| 94 | + direction.z = +1.0 |
| 95 | + elif pressed_keys[K_a]: |
| 96 | + direction.z = -1.0 |
| 97 | + |
| 98 | + if pressed_keys[K_w]: |
| 99 | + fov = min(179., fov+1.) |
| 100 | + w = SCREEN_SIZE[0] |
| 101 | + viewing_distance = calculate_viewing_distance(radians(fov), w) |
| 102 | + elif pressed_keys[K_s]: |
| 103 | + fov = max(1., fov-1.) |
| 104 | + w = SCREEN_SIZE[0] |
| 105 | + viewing_distance = calculate_viewing_distance(radians(fov), w) |
| 106 | + |
| 107 | + camera_position += direction * camera_speed * time_passed_seconds |
| 108 | + |
| 109 | + # Draw the 3D points |
| 110 | + for point in points: |
| 111 | + |
| 112 | + x, y, z = point - camera_position |
| 113 | + |
| 114 | + if z > 0: |
| 115 | + x = x * viewing_distance / z |
| 116 | + y = -y * viewing_distance / z |
| 117 | + x += center_x |
| 118 | + y += center_y |
| 119 | + screen.blit(ball, (x-ball_center_x, y-ball_center_y)) |
| 120 | + |
| 121 | + # Draw the field of view diagram |
| 122 | + diagram_width = SCREEN_SIZE[0] / 4 |
| 123 | + col = (50, 255, 50) |
| 124 | + diagram_points = [] |
| 125 | + diagram_points.append( (diagram_width/2, 100+viewing_distance/4) ) |
| 126 | + diagram_points.append( (0, 100) ) |
| 127 | + diagram_points.append( (diagram_width, 100) ) |
| 128 | + diagram_points.append( (diagram_width/2, 100+viewing_distance/4) ) |
| 129 | + diagram_points.append( (diagram_width/2, 100) ) |
| 130 | + pygame.draw.lines(screen, col, False, diagram_points, 2) |
| 131 | + |
| 132 | + # Draw the text |
| 133 | + white = (255, 255, 255) |
| 134 | + cam_text = font.render("camera = "+str(camera_position), True, white) |
| 135 | + screen.blit(cam_text, (5, 5)) |
| 136 | + fov_text = font.render("field of view = %i"%int(fov), True, white) |
| 137 | + screen.blit(fov_text, (5, 35)) |
| 138 | + txt = "viewing distance = %.3f"%viewing_distance |
| 139 | + d_text = font.render(txt, True, white) |
| 140 | + screen.blit(d_text, (5, 65)) |
| 141 | + |
| 142 | + pygame.display.update() |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + run() |
| 147 | + |
| 148 | + |
| 149 | + |
0 commit comments