Skip to content

Commit c2a9f7c

Browse files
author
Jorge A. Gomes
committed
new core examples
1 parent ba705f8 commit c2a9f7c

File tree

6 files changed

+494
-0
lines changed

6 files changed

+494
-0
lines changed

examples/audio_music_stream.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# -------------------------------------------------------------------------------------------------
2+
#
3+
# raylib [audio] example - Music playing (streaming)
4+
#
5+
# Example originally created with raylib 1.3, last time updated with raylib 4.0
6+
#
7+
# Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
8+
# BSD-like license that allows static linking with closed source software
9+
#
10+
# This example require some resources to be loaded. To run without loading errors, please,
11+
# make sure you have the original C raylib examples in your machine. Then, provide the directory
12+
# path where the original c example source is located:
13+
#
14+
# $ py audio_music_stream.py path/to/raylib/examples/audio
15+
#
16+
# Copyright (c) 2015-2023 Ramon Santamaria (@raysan5)
17+
#
18+
# -------------------------------------------------------------------------------------------------
19+
20+
import sys
21+
import os
22+
from ctypes import byref
23+
from raylibpy import *
24+
25+
def main():
26+
"""Program main entrypoint"""
27+
28+
# initialization
29+
# ---------------------------------------------------------------------------------------------
30+
screenWidth = 800
31+
screenHeight = 450
32+
33+
init_window(screenWidth, screenHeight, "raylib [audio] example - music playing (streaming)")
34+
init_audio_device()
35+
music = load_music_stream("resources/country.mp3")
36+
play_music_stream(music)
37+
timePlayed = 0.0
38+
pause = False
39+
40+
set_target_fps(30)
41+
42+
# Main Game Loop
43+
# ---------------------------------------------------------------------------------------------
44+
while not window_should_close():
45+
# Update
46+
# -----------------------------------------------------------------------------------------
47+
update_music_stream(music)
48+
49+
if is_key_pressed(KEY_SPACE):
50+
stop_music_stream(music)
51+
play_music_stream(music)
52+
53+
if is_key_pressed(KEY_P):
54+
pause = not pause
55+
56+
if pause:
57+
pause_music_stream(music)
58+
else:
59+
resume_music_stream(music)
60+
timePlayed = get_music_time_played(music) / get_music_time_length(music)
61+
62+
if timePlayed > 1.0:
63+
timePlayed = 1.0
64+
65+
# Draw
66+
# -----------------------------------------------------------------------------------------
67+
begin_drawing()
68+
69+
clear_background(RAYWHITE)
70+
draw_text("MUSIC SHOULD BE PLAYING!", 255, 150, 20, LIGHTGRAY)
71+
draw_rectangle(200, 200, 400, 12, LIGHTGRAY)
72+
draw_rectangle(200, 200, int(timePlayed * 400.0), 12, MAROON)
73+
draw_rectangle_lines(200, 200, 400, 12, GRAY)
74+
draw_text("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, LIGHTGRAY)
75+
draw_text("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, LIGHTGRAY)
76+
77+
end_drawing()
78+
79+
# De-Initialization
80+
# ---------------------------------------------------------------------------------------------
81+
unload_music_stream(music)
82+
close_audio_device()
83+
close_window()
84+
return 0
85+
86+
87+
if __name__ == "__main__":
88+
if len(sys.argv) >= 2 and isinstance(sys.argv[1], str):
89+
os.chdir(sys.argv[1])
90+
print("Working dir:", os.getcwd())
91+
sys.exit(main())

examples/core_2d_camera_mouse_zoom.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#*******************************************************************************************
2+
#
3+
# raylib [core] example - 2d camera mouse zoom
4+
#
5+
# Example originally created with raylib 4.2, last time updated with raylib 4.2
6+
#
7+
# Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
8+
# BSD-like license that allows static linking with closed source software
9+
#
10+
# Copyright (c) 2022-2023 Jeffery Myers (@JeffM2501)
11+
#
12+
#*******************************************************************************************/
13+
14+
import sys
15+
import os
16+
from ctypes import byref
17+
from raylibpy import *
18+
19+
20+
def main():
21+
"""Transpiled function."""
22+
23+
screenWidth = 800
24+
screenHeight = 450
25+
26+
init_window(screenWidth, screenHeight, "raylib [core] example - 2d camera mouse zoom")
27+
28+
camera = Camera2D()
29+
camera.zoom = 1.0
30+
31+
set_target_fps(60)
32+
33+
while not window_should_close():
34+
if is_mouse_button_down(MOUSE_BUTTON_RIGHT):
35+
delta = get_mouse_delta()
36+
delta = vector2scale(delta, -1.0 / camera.zoom)
37+
camera.target = vector2add(camera.target, delta)
38+
39+
wheel = get_mouse_wheel_move()
40+
41+
if wheel != 0:
42+
mouseWorldPos = get_screen_to_world2d(get_mouse_position(), camera)
43+
camera.offset = get_mouse_position()
44+
camera.target = mouseWorldPos
45+
zoomIncrement = 0.125
46+
camera.zoom += (wheel * zoomIncrement)
47+
48+
if camera.zoom < zoomIncrement:
49+
camera.zoom = zoomIncrement
50+
51+
begin_drawing()
52+
53+
clear_background(BLACK)
54+
55+
begin_mode2d(camera)
56+
57+
rl_push_matrix()
58+
rl_translatef(0, 25 * 50, 0)
59+
rl_rotatef(90, 1, 0, 0)
60+
draw_grid(100, 50)
61+
rl_pop_matrix()
62+
draw_circle(100, 100, 50, YELLOW)
63+
64+
end_mode2d()
65+
66+
draw_text("Mouse right button drag to move, mouse wheel to zoom", 10, 10, 20, WHITE)
67+
68+
end_drawing()
69+
70+
close_window()
71+
72+
return 0
73+
74+
75+
if __name__ == "__main__":
76+
if len(sys.argv) >= 2 and isinstance(sys.argv[1], str):
77+
os.chdir(sys.argv[1])
78+
print("Working dir:", os.getcwd())
79+
sys.exit(main())
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#*******************************************************************************************
2+
#
3+
# raylib [core] example - 2d camera split screen
4+
#
5+
# Addapted from the core_3d_camera_split_screen example:
6+
# https://github.com/raysan5/raylib/blob/master/examples/core/core_3d_camera_split_screen.c
7+
#
8+
# Example originally created with raylib 4.5, last time updated with raylib 4.5
9+
#
10+
# Example contributed by Gabriel dos Santos Sanches (@gabrielssanches) and reviewed by Ramon Santamaria (@raysan5)
11+
#
12+
# Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
13+
# BSD-like license that allows static linking with closed source software
14+
#
15+
# Copyright (c) 2023 Gabriel dos Santos Sanches (@gabrielssanches)
16+
#
17+
#*******************************************************************************************/
18+
19+
import sys
20+
import os
21+
from ctypes import byref
22+
from raylibpy import *
23+
24+
PLAYER_SIZE = 40
25+
26+
27+
def main():
28+
"""Transpiled function."""
29+
screenWidth = 800
30+
screenHeight = 440
31+
32+
init_window(screenWidth, screenHeight, "raylib [core] example - 2d camera split screen")
33+
34+
player1 = Rectangle(200, 200, PLAYER_SIZE, PLAYER_SIZE)
35+
player2 = Rectangle(250, 200, PLAYER_SIZE, PLAYER_SIZE)
36+
37+
camera1 = Camera2D(0)
38+
camera1.target = Vector2(player1.x, player1.y)
39+
camera1.offset = Vector2(200.0, 200.0)
40+
camera1.rotation = 0.0
41+
camera1.zoom = 1.0
42+
43+
camera2 = Camera2D(0)
44+
camera2.target = Vector2(player2.x, player2.y)
45+
camera2.offset = Vector2(200.0, 200.0)
46+
camera2.rotation = 0.0
47+
camera2.zoom = 1.0
48+
49+
screenCamera1 = load_render_texture(screenWidth / 2, screenHeight)
50+
screenCamera2 = load_render_texture(screenWidth / 2, screenHeight)
51+
52+
splitScreenRect = Rectangle(0.0, 0.0, screenCamera1.texture.width, screenCamera1.texture.height)
53+
54+
set_target_fps(60)
55+
56+
while not window_should_close():
57+
if is_key_down(KEY_S):
58+
player1.y += 3.0
59+
elif is_key_down(KEY_W):
60+
player1.y -= 3.0
61+
62+
if is_key_down(KEY_D):
63+
player1.x += 3.0
64+
elif is_key_down(KEY_A):
65+
player1.x -= 3.0
66+
67+
if is_key_down(KEY_UP):
68+
player2.y -= 3.0
69+
elif is_key_down(KEY_DOWN):
70+
player2.y += 3.0
71+
72+
if is_key_down(KEY_RIGHT):
73+
player2.x += 3.0
74+
elif is_key_down(KEY_LEFT):
75+
player2.x -= 3.0
76+
77+
camera1.target = Vector2(player1.x, player1.y)
78+
camera2.target = Vector2(player2.x, player2.y)
79+
80+
with texture_mode(screenCamera1):
81+
clear_background(RAYWHITE)
82+
83+
begin_mode2d(camera1)
84+
draw_rectangle_rec(player1, RED)
85+
draw_rectangle_rec(player2, BLUE)
86+
end_mode2d()
87+
88+
draw_rectangle(0, 0, get_screen_width() / 2, 30, fade(RAYWHITE, 0.6))
89+
draw_text("PLAYER1: W/S/A/D to move", 10, 10, 10, MAROON)
90+
91+
# end_texture_mode()
92+
93+
with texture_mode(screenCamera2):
94+
clear_background(RAYWHITE)
95+
96+
begin_mode2d(camera2)
97+
draw_rectangle_rec(player1, RED)
98+
draw_rectangle_rec(player2, BLUE)
99+
end_mode2d()
100+
101+
draw_rectangle(0, 0, get_screen_width() / 2, 30, fade(RAYWHITE, 0.6))
102+
draw_text("PLAYER2: UP/DOWN/LEFT/RIGHT to move", 10, 10, 10, DARKBLUE)
103+
104+
# end_texture_mode()
105+
106+
with drawing():
107+
108+
clear_background(BLACK)
109+
draw_texture_rec(screenCamera1.texture, splitScreenRect, Vector2(0, 0), WHITE)
110+
draw_texture_rec(screenCamera2.texture, splitScreenRect, Vector2(screenWidth / 2.0, 0), WHITE)
111+
draw_rectangle(get_screen_width() / 2 - 2, 0, 4, get_screen_height(), LIGHTGRAY)
112+
113+
# end_drawing()
114+
115+
unload_render_texture(screenCamera1)
116+
unload_render_texture(screenCamera2)
117+
118+
close_window()
119+
120+
return 0
121+
122+
123+
if __name__ == "__main__":
124+
if len(sys.argv) >= 2 and isinstance(sys.argv[1], str):
125+
os.chdir(sys.argv[1])
126+
print("Working dir:", os.getcwd())
127+
sys.exit(main())

examples/core_3d_camera_free.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#*******************************************************************************************
2+
#
3+
# raylib [core] example - Initialize 3d camera free
4+
#
5+
# Example originally created with raylib 1.3, last time updated with raylib 1.3
6+
#
7+
# Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
8+
# BSD-like license that allows static linking with closed source software
9+
#
10+
# Copyright (c) 2015-2023 Ramon Santamaria (@raysan5)
11+
#
12+
#*******************************************************************************************/
13+
14+
import sys
15+
import os
16+
from ctypes import byref
17+
from raylibpy import *
18+
19+
20+
def main():
21+
"""Transpiled function."""
22+
screenWidth = 800
23+
screenHeight = 450
24+
25+
init_window(screenWidth, screenHeight, "raylib [core] example - 3d camera free")
26+
27+
camera = Camera3D(0)
28+
camera.position = Vector3(10.0, 10.0, 10.0)
29+
camera.target = Vector3(0.0, 0.0, 0.0)
30+
camera.up = Vector3(0.0, 1.0, 0.0)
31+
camera.fovy = 45.0
32+
camera.projection = CAMERA_PERSPECTIVE
33+
34+
cubePosition = Vector3(0.0, 0.0, 0.0)
35+
36+
disable_cursor()
37+
38+
set_target_fps(60)
39+
40+
while not window_should_close():
41+
update_camera(byref(camera), CAMERA_FREE)
42+
43+
if is_key_pressed(KEY_Z):
44+
camera.target = Vector3(0.0, 0.0, 0.0)
45+
46+
with drawing():
47+
48+
clear_background(RAYWHITE)
49+
50+
with mode3d(camera):
51+
52+
draw_cube(cubePosition, 2.0, 2.0, 2.0, RED)
53+
draw_cube_wires(cubePosition, 2.0, 2.0, 2.0, MAROON)
54+
draw_grid(10, 1.0)
55+
56+
# end_mode3d()
57+
58+
draw_rectangle(10, 10, 320, 93, fade(SKYBLUE, 0.5))
59+
draw_rectangle_lines(10, 10, 320, 93, BLUE)
60+
draw_text("Free camera default controls:", 20, 20, 10, BLACK)
61+
draw_text("- Mouse Wheel to Zoom in-out", 40, 40, 10, DARKGRAY)
62+
draw_text("- Mouse Wheel Pressed to Pan", 40, 60, 10, DARKGRAY)
63+
draw_text("- Z to zoom to (0, 0, 0)", 40, 80, 10, DARKGRAY)
64+
65+
# end_drawing()
66+
67+
close_window()
68+
69+
return 0
70+
71+
72+
if __name__ == "__main__":
73+
if len(sys.argv) >= 2 and isinstance(sys.argv[1], str):
74+
os.chdir(sys.argv[1])
75+
print("Working dir:", os.getcwd())
76+
sys.exit(main())

0 commit comments

Comments
 (0)