|
| 1 | +from pyray import * |
| 2 | +import pyray |
| 3 | +import math |
| 4 | + |
| 5 | +# Program main entry point |
| 6 | +# Initialization |
| 7 | +screenWidth = 800 |
| 8 | +screenHeight = 450 |
| 9 | + |
| 10 | +init_window(screenWidth, screenHeight, "raylib [models] example - waving cubes") |
| 11 | + |
| 12 | +# Initialize the camera |
| 13 | +camera = Camera3D() |
| 14 | +camera.position = Vector3(30.0, 20.0, 30.0) # Camera position |
| 15 | +camera.target = Vector3(0.0, 0.0, 0.0) # Camera looking at point |
| 16 | +camera.up = Vector3(0.0, 1.0, 0.0) # Camera up vector (rotation towards target) |
| 17 | +camera.fovy = 70.0 # Camera field-of-view Y |
| 18 | +camera.projection = pyray.CAMERA_PERSPECTIVE # Camera projection type |
| 19 | + |
| 20 | +# Specify the amount of blocks in each direction |
| 21 | +numBlocks = 15 |
| 22 | + |
| 23 | +set_target_fps(60) |
| 24 | + |
| 25 | +# Main game loop |
| 26 | +while not window_should_close(): # Detect window close button or ESC key |
| 27 | + # Update |
| 28 | + time = get_time() |
| 29 | + |
| 30 | + # Calculate time scale for cube position and size |
| 31 | + scale = (2.0 + math.sin(time)) * 0.7 |
| 32 | + |
| 33 | + # Move camera around the scene |
| 34 | + cameraTime = time * 0.3 |
| 35 | + camera.position.x = math.cos(cameraTime) * 40.0 |
| 36 | + camera.position.z = math.sin(cameraTime) * 40.0 |
| 37 | + |
| 38 | + # Draw |
| 39 | + begin_drawing() |
| 40 | + |
| 41 | + clear_background(RAYWHITE) |
| 42 | + |
| 43 | + begin_mode_3d(camera) |
| 44 | + |
| 45 | + draw_grid(10, 5.0) |
| 46 | + |
| 47 | + for x in range(numBlocks): |
| 48 | + for y in range(numBlocks): |
| 49 | + for z in range(numBlocks): |
| 50 | + # Scale of the blocks depends on x/y/z positions |
| 51 | + blockScale = (x + y + z) / 30.0 |
| 52 | + |
| 53 | + # Scatter makes the waving effect by adding blockScale over time |
| 54 | + scatter = math.sin(blockScale * 20.0 + time * 4.0) |
| 55 | + |
| 56 | + # Calculate the cube position |
| 57 | + cubePos = Vector3( |
| 58 | + (x - numBlocks / 2) * (scale * 3.0) + scatter, |
| 59 | + (y - numBlocks / 2) * (scale * 2.0) + scatter, |
| 60 | + (z - numBlocks / 2) * (scale * 3.0) + scatter, |
| 61 | + ) |
| 62 | + |
| 63 | + # Pick a color with a hue depending on cube position for the rainbow color effect |
| 64 | + cubeColor = color_from_hsv(((x + y + z) * 18) % 360, 0.75, 0.9) |
| 65 | + |
| 66 | + # Calculate cube size |
| 67 | + cubeSize = (2.4 - scale) * blockScale |
| 68 | + |
| 69 | + # And finally, draw the cube! |
| 70 | + draw_cube(cubePos, cubeSize, cubeSize, cubeSize, cubeColor) |
| 71 | + |
| 72 | + end_mode_3d() |
| 73 | + |
| 74 | + draw_fps(10, 10) |
| 75 | + |
| 76 | + end_drawing() |
| 77 | + |
| 78 | +# De-Initialization |
| 79 | +close_window() # Close window and OpenGL context |
0 commit comments