Skip to content

Commit 903747b

Browse files
Timfon“Timfon”
andauthored
added core_split_screen example (#106)
Co-authored-by: “Timfon” <“[email protected]”>
1 parent b23957d commit 903747b

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

examples/core/core_split_screen.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
"""
2+
3+
raylib [core] example - Split Screen
4+
5+
"""
6+
7+
from pyray import *
8+
9+
from raylib import (
10+
KEY_W,
11+
KEY_S,
12+
KEY_UP,
13+
KEY_DOWN,
14+
)
15+
16+
cameraPlayer1 = Camera3D([0])
17+
cameraPlayer2 = Camera3D([0])
18+
19+
def draw_scene():
20+
count = 5
21+
spacing = 4
22+
23+
# Grid of cube trees on a plane to make a "world"
24+
25+
draw_plane(Vector3(0, 0, 0), Vector2(50, 50), BEIGE)
26+
27+
for x in range(-count*spacing, count*spacing, spacing):
28+
for z in range(-count*spacing, count*spacing, spacing):
29+
draw_cube(Vector3(x, 1.5, z), 1, 1, 1, LIME)
30+
draw_cube(Vector3(x, 0.5, z), 0.25, 1, 0.25, BROWN)
31+
32+
# Draw a cube at each player's position
33+
draw_cube(cameraPlayer1.position, 1, 1, 1, RED)
34+
draw_cube(cameraPlayer2.position, 1, 1, 1, BLUE)
35+
36+
# Initialization
37+
38+
screenWidth = 800
39+
screenHeight = 450
40+
41+
init_window(screenWidth, screenHeight, "raylib [core] example - split screen")
42+
43+
# Setup player 1 camera and screen
44+
45+
cameraPlayer1.fovy = 45.0
46+
cameraPlayer1.up = Vector3(0,1.0,0)
47+
cameraPlayer1.target = Vector3(0,1.0,0)
48+
cameraPlayer1.position = Vector3(0,1,-3)
49+
50+
screenPlayer1 = load_render_texture(int(screenWidth/2), int(screenHeight))
51+
52+
# Setup player 2 camera and screen
53+
cameraPlayer2.fovy = 45.0
54+
cameraPlayer2.up = Vector3(0,1.0,0)
55+
cameraPlayer2.target = Vector3(0,3.0,0)
56+
cameraPlayer2.position = Vector3(-3,3,0)
57+
58+
screenPlayer2 = load_render_texture(int(screenWidth / 2), int(screenHeight))
59+
60+
# Build a flipped rectangle the size of the split view to use for drawing later
61+
splitScreenRect = Rectangle(0.0, 0.0, screenPlayer1.texture.width, -screenPlayer1.texture.height)
62+
63+
set_target_fps(60) # Set our game to run at 60 frames-per-second
64+
65+
# Main game loop
66+
while not window_should_close(): # Detect window close button or ESC key
67+
68+
''' Update
69+
If anyone moves this frame, how far will they move based on the time since the last frame
70+
this moves thigns at 10 world units per second, regardless of the actual FPS'''
71+
72+
offsetThisFrame = 10.0 * get_frame_time()
73+
74+
# Move Player1 forward and backwards (no turning)
75+
76+
if is_key_down(KEY_W):
77+
cameraPlayer1.position.z += offsetThisFrame
78+
cameraPlayer1.target.z += offsetThisFrame
79+
80+
elif is_key_down(KEY_S):
81+
82+
cameraPlayer1.position.z -= offsetThisFrame
83+
cameraPlayer1.target.z -= offsetThisFrame
84+
85+
# Move Player2 forward and backwards (no turning)
86+
if is_key_down(KEY_UP):
87+
cameraPlayer2.position.x += offsetThisFrame
88+
cameraPlayer2.target.x += offsetThisFrame
89+
90+
elif is_key_down(KEY_DOWN):
91+
cameraPlayer2.position
92+
cameraPlayer2.position.x -= offsetThisFrame
93+
cameraPlayer2.target.x -= offsetThisFrame
94+
95+
# Draw Player1 view to the render texture
96+
begin_texture_mode(screenPlayer1)
97+
clear_background(SKYBLUE)
98+
begin_mode_3d(cameraPlayer1)
99+
draw_scene()
100+
end_mode_3d()
101+
draw_text("PLAYER1 W/S to move", 10, 10, 20, RED)
102+
end_texture_mode()
103+
104+
# Draw Player2 view to the render texture
105+
106+
begin_texture_mode(screenPlayer2)
107+
clear_background(SKYBLUE)
108+
begin_mode_3d(cameraPlayer2)
109+
draw_scene()
110+
end_mode_3d()
111+
draw_text("PLAYER2 UP/DOWN to move", 10, 10, 20, BLUE)
112+
end_texture_mode()
113+
114+
# Draw both views render textures to the screen side by side
115+
begin_drawing()
116+
clear_background(BLACK)
117+
draw_texture_rec(screenPlayer1.texture, splitScreenRect, Vector2(0,0), WHITE)
118+
draw_texture_rec(screenPlayer2.texture, splitScreenRect, Vector2(screenWidth/2.0, 0 ), WHITE)
119+
end_drawing()
120+
121+
122+
# De-Initialization
123+
unload_render_texture(screenPlayer1) # Unload render texture
124+
unload_render_texture(screenPlayer2) # Unload render texture
125+
126+
close_window()
127+
128+
129+
130+

0 commit comments

Comments
 (0)