Advertisement
DeaD_EyE

raylib [core][python] example - 3d cmaera split screen

Jan 14th, 2025 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.96 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. """
  3. raylib [core][python] example - 3d cmaera split screen
  4. https://github.com/raysan5/raylib/blob/master/examples/core/core_3d_camera_split_screen.c
  5.  
  6. The code is a close a possible to the C version, except the loop uses itertools.product
  7. """
  8. from itertools import product
  9.  
  10. import pyray as pr
  11.  
  12. SCREEN_WIDTH = 800
  13. SCREEN_HEIGHT = 450
  14.  
  15.  
  16. def main():
  17.     # Initialization
  18.     pr.init_window(
  19.         SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - 3d camera split screen"
  20.     )
  21.     camera_player1 = pr.Camera3D(
  22.         pr.Vector3(0, 1, -3),
  23.         pr.Vector3(0, 1),
  24.         pr.Vector3(0, 1),
  25.         45,
  26.     )
  27.     screen_player1 = pr.load_render_texture(SCREEN_WIDTH // 2, SCREEN_HEIGHT)
  28.  
  29.     # Setup player two camera and screen
  30.     camera_player2 = pr.Camera3D(
  31.         pr.Vector3(-3, 3),
  32.         pr.Vector3(0, 3),
  33.         pr.Vector3(0, 1),
  34.         45,
  35.     )
  36.     screen_player2 = pr.load_render_texture(SCREEN_WIDTH // 2, SCREEN_HEIGHT)
  37.  
  38.     # Build a flipped rectangle the size of the split view to use for drawing later
  39.     split_screen_rect = pr.Rectangle(
  40.         0,
  41.         0,
  42.         screen_player1.texture.width,
  43.         -screen_player1.texture.height,
  44.     )
  45.  
  46.     # Grid data
  47.     count = 5
  48.     spacing = 4
  49.  
  50.     # Set our game to run at 60 frames-per-second
  51.     pr.set_target_fps(60)
  52.  
  53.     # Main game loop
  54.     while not pr.window_should_close():
  55.         # Detect window close button or ESC key
  56.  
  57.         # Update
  58.         # If anyone moves this frame, how far will they move based on the time since the last frame
  59.         # this moves things at 10 world units per second, regardless of the actual FPS
  60.         offset_this_frame = 10.0 * pr.get_frame_time()
  61.  
  62.         # Move Player1 forward and backwards (no turning)
  63.         if pr.is_key_down(pr.KEY_W):
  64.             camera_player1.position.z += offset_this_frame
  65.             camera_player1.target.z += offset_this_frame
  66.         elif pr.is_key_down(pr.KEY_S):
  67.             camera_player1.position.z -= offset_this_frame
  68.             camera_player1.target.z -= offset_this_frame
  69.  
  70.         # Move Player2 forward and backwards (no turning)
  71.         if pr.is_key_down(pr.KEY_UP):
  72.             camera_player2.position.x += offset_this_frame
  73.             camera_player2.target.x += offset_this_frame
  74.         elif pr.is_key_down(pr.KEY_DOWN):
  75.             camera_player2.position.x -= offset_this_frame
  76.             camera_player2.target.x -= offset_this_frame
  77.  
  78.         # Draw Player1 view to the render texture
  79.         pr.begin_texture_mode(screen_player1)
  80.         pr.clear_background(pr.SKYBLUE)
  81.         pr.begin_mode_3d(camera_player1)
  82.  
  83.         # Draw scene: grid of cube trees on a plane to make a "world"
  84.         # Simple world plane
  85.         pr.draw_plane(
  86.             pr.Vector3(0, 0, 0),
  87.             pr.Vector2(50, 50),
  88.             pr.BEIGE,
  89.         )
  90.  
  91.         xz_iter = product(
  92.             range(-count * spacing, count * spacing, spacing),
  93.             range(-count * spacing, count * spacing, spacing),
  94.         )
  95.  
  96.         # for x in range(-count*spacing, count*spacing, spacing):
  97.         #    for z in range(-count*spacing, count*spacing, spacing):
  98.         for x, z in xz_iter:
  99.             pr.draw_cube(pr.Vector3(x, 1.5, z), 1, 1, 1, pr.LIME)
  100.             pr.draw_cube(pr.Vector3(x, 0.5, z), 0.25, 1, 0.25, pr.BROWN)
  101.  
  102.         # Draw a cube at each player's position
  103.         pr.draw_cube(camera_player1.position, 1, 1, 1, pr.RED)
  104.         pr.draw_cube(camera_player2.position, 1, 1, 1, pr.BLUE)
  105.         pr.end_mode_3d()
  106.         pr.draw_rectangle(
  107.             0, 0, pr.get_screen_width() // 2, 40, pr.fade(pr.RAYWHITE, 0.8)
  108.         )
  109.         pr.draw_text("PLAYER1: W/S to move", 10, 10, 20, pr.MAROON)
  110.         pr.end_texture_mode()
  111.  
  112.         # Draw Player2 view to the render texture
  113.         pr.begin_texture_mode(screen_player2)
  114.         pr.clear_background(pr.SKYBLUE)
  115.         pr.begin_mode_3d(camera_player2)
  116.  
  117.         # Draw scene: grid of cube trees on a plane to make a "world"
  118.         # Simple world plane
  119.         pr.draw_plane(
  120.             pr.Vector3(0, 0, 0),
  121.             pr.Vector2(50, 50),
  122.             pr.BEIGE,
  123.         )
  124.  
  125.         xz_iter = product(
  126.             range(-count * spacing, count * spacing, spacing),
  127.             range(-count * spacing, count * spacing, spacing),
  128.         )
  129.  
  130.         # for x in range(-count*spacing, count*spacing, spacing):
  131.         #    for z in range(-count*spacing, count*spacing, spacing):
  132.         for x, z in xz_iter:
  133.             pr.draw_cube(
  134.                 pr.Vector3(x, 1.5, z),
  135.                 1,
  136.                 1,
  137.                 1,
  138.                 pr.LIME,
  139.             )
  140.             pr.draw_cube(pr.Vector3(x, 0.5, z), 0.25, 1, 0.25, pr.BROWN)
  141.  
  142.         # Draw a cube at each player's position
  143.         pr.draw_cube(camera_player1.position, 1, 1, 1, pr.RED)
  144.         pr.draw_cube(camera_player2.position, 1, 1, 1, pr.BLUE)
  145.         pr.end_mode_3d()
  146.         pr.draw_rectangle(
  147.             0, 0, pr.get_screen_width() // 2, 40, pr.fade(pr.RAYWHITE, 0.8)
  148.         )
  149.         pr.draw_text("PLAYER2: UP/DOWN to move", 10, 10, 20, pr.DARKBLUE)
  150.         pr.end_texture_mode()
  151.  
  152.         # Draw both views render textures to the screen side by side
  153.         pr.begin_drawing()
  154.         pr.clear_background(pr.BLACK)
  155.  
  156.         pr.draw_texture_rec(
  157.             screen_player1.texture, split_screen_rect, pr.Vector2(0, 0), pr.WHITE
  158.         )
  159.         pr.draw_texture_rec(
  160.             screen_player2.texture,
  161.             split_screen_rect,
  162.             pr.Vector2(SCREEN_WIDTH / 2.0, 0),
  163.             pr.WHITE,
  164.         )
  165.  
  166.         pr.draw_rectangle(
  167.             pr.get_screen_width() // 2 - 2, 0, 4, pr.get_screen_height(), pr.LIGHTGRAY
  168.         )
  169.         pr.end_drawing()
  170.  
  171.     # De-Initialization
  172.     pr.unload_render_texture(screen_player1)  # Unload render texture
  173.     pr.unload_render_texture(screen_player2)  # Unload render texture
  174.     pr.close_window()  # Close window and OpenGL context
  175.  
  176.  
  177. if __name__ == "__main__":
  178.     main()
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement