Skip to content

Commit ccd91d3

Browse files
author
Jorge A. Gomes
committed
new core examples (second part)
1 parent c2a9f7c commit ccd91d3

7 files changed

+496
-0
lines changed

examples/core_input_keys.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#*******************************************************************************************
2+
#
3+
# raylib [core] example - Keyboard input
4+
#
5+
# Example originally created with raylib 1.0, last time updated with raylib 1.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+
# Copyright (c) 2014-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 - keyboard input")
26+
27+
ballPosition = Vector2(screenWidth / 2, screenHeight / 2)
28+
29+
set_target_fps(60)
30+
31+
while not window_should_close():
32+
if is_key_down(KEY_RIGHT):
33+
ballPosition.x += 2.0
34+
35+
if is_key_down(KEY_LEFT):
36+
ballPosition.x -= 2.0
37+
38+
if is_key_down(KEY_UP):
39+
ballPosition.y -= 2.0
40+
41+
if is_key_down(KEY_DOWN):
42+
ballPosition.y += 2.0
43+
44+
begin_drawing()
45+
46+
clear_background(RAYWHITE)
47+
draw_text("move the ball with arrow keys", 10, 10, 20, DARKGRAY)
48+
draw_circle_v(ballPosition, 50, MAROON)
49+
50+
end_drawing()
51+
52+
close_window()
53+
54+
return 0
55+
56+
57+
if __name__ == "__main__":
58+
if len(sys.argv) >= 2 and isinstance(sys.argv[1], str):
59+
os.chdir(sys.argv[1])
60+
print("Working dir:", os.getcwd())
61+
sys.exit(main())

examples/core_input_mouse.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#*******************************************************************************************
2+
#
3+
# raylib [core] example - Mouse input
4+
#
5+
# Example originally created with raylib 1.0, 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+
# Copyright (c) 2014-2023 Ramon Santamaria (@raysan5)
11+
#
12+
#*******************************************************************************************/
13+
14+
15+
import sys
16+
import os
17+
from ctypes import byref
18+
from raylibpy import *
19+
20+
21+
def main():
22+
"""Transpiled function."""
23+
screenWidth = 800
24+
screenHeight = 450
25+
26+
init_window(screenWidth, screenHeight, "raylib [core] example - mouse input")
27+
28+
ballPosition = Vector2(-100.0, -100.0)
29+
ballColor = DARKBLUE
30+
31+
set_target_fps(60)
32+
33+
while not window_should_close():
34+
ballPosition = get_mouse_position()
35+
36+
if is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
37+
ballColor = MAROON
38+
elif is_mouse_button_pressed(MOUSE_BUTTON_MIDDLE):
39+
ballColor = LIME
40+
elif is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
41+
ballColor = DARKBLUE
42+
elif is_mouse_button_pressed(MOUSE_BUTTON_SIDE):
43+
ballColor = PURPLE
44+
elif is_mouse_button_pressed(MOUSE_BUTTON_EXTRA):
45+
ballColor = YELLOW
46+
elif is_mouse_button_pressed(MOUSE_BUTTON_FORWARD):
47+
ballColor = ORANGE
48+
elif is_mouse_button_pressed(MOUSE_BUTTON_BACK):
49+
ballColor = BEIGE
50+
51+
begin_drawing()
52+
53+
clear_background(RAYWHITE)
54+
draw_circle_v(ballPosition, 40, ballColor)
55+
draw_text("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY)
56+
57+
end_drawing()
58+
59+
close_window()
60+
61+
return 0
62+
63+
64+
if __name__ == "__main__":
65+
if len(sys.argv) >= 2 and isinstance(sys.argv[1], str):
66+
os.chdir(sys.argv[1])
67+
print("Working dir:", os.getcwd())
68+
sys.exit(main())

examples/core_input_mouse_wheel.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#*******************************************************************************************
2+
#
3+
# raylib [core] examples - Mouse wheel input
4+
#
5+
# Example originally created with raylib 1.1, 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) 2014-2023 Ramon Santamaria (@raysan5)
11+
#
12+
#*******************************************************************************************/
13+
14+
15+
import sys
16+
import os
17+
from ctypes import byref
18+
from raylibpy import *
19+
20+
21+
def main():
22+
"""Transpiled function."""
23+
screenWidth = 800
24+
screenHeight = 450
25+
26+
init_window(screenWidth, screenHeight, "raylib [core] example - input mouse wheel")
27+
28+
boxPositionY = screenHeight / 2 - 40
29+
scrollSpeed = 4
30+
31+
set_target_fps(60)
32+
33+
while not window_should_close():
34+
boxPositionY -= (get_mouse_wheel_move() * scrollSpeed)
35+
36+
begin_drawing()
37+
38+
clear_background(RAYWHITE)
39+
draw_rectangle(screenWidth / 2 - 40, boxPositionY, 80, 80, MAROON)
40+
draw_text("Use mouse wheel to move the cube up and down!", 10, 10, 20, GRAY)
41+
draw_text(f"Box position Y: {boxPositionY}", 10, 40, 20, LIGHTGRAY)
42+
43+
end_drawing()
44+
45+
close_window()
46+
47+
return 0
48+
49+
50+
if __name__ == "__main__":
51+
if len(sys.argv) >= 2 and isinstance(sys.argv[1], str):
52+
os.chdir(sys.argv[1])
53+
print("Working dir:", os.getcwd())
54+
sys.exit(main())

examples/core_scissor_test.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#*******************************************************************************************
2+
#
3+
# raylib [core] example - Scissor test
4+
#
5+
# Example originally created with raylib 2.5, last time updated with raylib 3.0
6+
#
7+
# Example contributed by Chris Dill (@MysteriousSpace) and reviewed by Ramon Santamaria (@raysan5)
8+
#
9+
# Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
10+
# BSD-like license that allows static linking with closed source software
11+
#
12+
# Copyright (c) 2019-2023 Chris Dill (@MysteriousSpace)
13+
#
14+
#*******************************************************************************************/
15+
16+
17+
import sys
18+
import os
19+
from ctypes import byref
20+
from raylibpy import *
21+
22+
23+
def main():
24+
"""Transpiled function."""
25+
screenWidth = 800
26+
screenHeight = 450
27+
28+
init_window(screenWidth, screenHeight, "raylib [core] example - scissor test")
29+
30+
scissorArea = Rectangle(0, 0, 300, 300)
31+
scissorMode = True
32+
33+
set_target_fps(60)
34+
35+
while not window_should_close():
36+
if is_key_pressed(KEY_S):
37+
scissorMode = not scissorMode
38+
39+
scissorArea.x = get_mouse_x() - scissorArea.width / 2
40+
scissorArea.y = get_mouse_y() - scissorArea.height / 2
41+
42+
with drawing():
43+
clear_background(RAYWHITE)
44+
45+
if scissorMode:
46+
begin_scissor_mode(scissorArea.x, scissorArea.y, scissorArea.width, scissorArea.height)
47+
48+
draw_rectangle(0, 0, get_screen_width(), get_screen_height(), RED)
49+
draw_text("Move the mouse around to reveal this text!", 190, 200, 20, LIGHTGRAY)
50+
51+
if scissorMode:
52+
end_scissor_mode()
53+
54+
draw_rectangle_lines_ex(scissorArea, 1, BLACK)
55+
draw_text("Press S to toggle scissor test", 10, 10, 20, BLACK)
56+
57+
# end_drawing()
58+
59+
close_window()
60+
return 0
61+
62+
63+
if __name__ == "__main__":
64+
if len(sys.argv) >= 2 and isinstance(sys.argv[1], str):
65+
os.chdir(sys.argv[1])
66+
print("Working dir:", os.getcwd())
67+
sys.exit(main())

examples/core_smooth_pixelperfect.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#*******************************************************************************************
2+
#
3+
# raylib [core] example - Smooth Pixel-perfect camera
4+
#
5+
# Example originally created with raylib 3.7, last time updated with raylib 4.0
6+
#
7+
# Example contributed by Giancamillo Alessandroni (@NotManyIdeasDev) and
8+
# reviewed by Ramon Santamaria (@raysan5)
9+
#
10+
# Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
11+
# BSD-like license that allows static linking with closed source software
12+
#
13+
# Copyright (c) 2021-2023 Giancamillo Alessandroni (@NotManyIdeasDev) and Ramon Santamaria (@raysan5)
14+
#
15+
#*******************************************************************************************/
16+
17+
18+
import sys
19+
import os
20+
from ctypes import byref
21+
from raylibpy import *
22+
from math import sin as sinf, cos as cosf, atan, atan2
23+
24+
25+
def main():
26+
"""Transpiled function."""
27+
screenWidth = 800
28+
screenHeight = 450
29+
30+
virtualScreenWidth = 160
31+
virtualScreenHeight = 90
32+
virtualRatio = screenWidth / virtualScreenWidth
33+
34+
init_window(screenWidth, screenHeight, "raylib [core] example - smooth pixel-perfect camera")
35+
36+
worldSpaceCamera = Camera2D()
37+
worldSpaceCamera.zoom = 1.0
38+
39+
screenSpaceCamera = Camera2D()
40+
screenSpaceCamera.zoom = 1.0
41+
42+
target = load_render_texture(virtualScreenWidth, virtualScreenHeight)
43+
44+
rec01 = Rectangle(70.0, 35.0, 20.0, 20.0)
45+
rec02 = Rectangle(90.0, 55.0, 30.0, 10.0)
46+
rec03 = Rectangle(80.0, 65.0, 15.0, 25.0)
47+
48+
sourceRec = Rectangle(0.0, 0.0, target.texture.width, -target.texture.height)
49+
destRec = Rectangle(-virtualRatio, -virtualRatio, screenWidth + (virtualRatio * 2), screenHeight + (virtualRatio * 2))
50+
51+
origin = Vector2(0.0, 0.0)
52+
rotation = 0.0
53+
cameraX = 0.0
54+
cameraY = 0.0
55+
56+
set_target_fps(60)
57+
58+
while not window_should_close():
59+
rotation += 60.0 * get_frame_time()
60+
61+
cameraX = (sinf(get_time()) * 50.0) - 10.0
62+
cameraY = cosf(get_time()) * 30.0
63+
64+
screenSpaceCamera.target = Vector2(cameraX, cameraY)
65+
66+
worldSpaceCamera.target.x = screenSpaceCamera.target.x
67+
screenSpaceCamera.target.x -= worldSpaceCamera.target.x
68+
screenSpaceCamera.target.x *= virtualRatio
69+
70+
worldSpaceCamera.target.y = screenSpaceCamera.target.y
71+
screenSpaceCamera.target.y -= worldSpaceCamera.target.y
72+
screenSpaceCamera.target.y *= virtualRatio
73+
74+
with texture_mode(target):
75+
clear_background(RAYWHITE)
76+
77+
with mode2d(worldSpaceCamera):
78+
draw_rectangle_pro(rec01, origin, rotation, BLACK)
79+
draw_rectangle_pro(rec02, origin, -rotation, RED)
80+
draw_rectangle_pro(rec03, origin, rotation + 45.0, BLUE)
81+
82+
# end_mode2d()
83+
# end_texture_mode()
84+
85+
with drawing():
86+
clear_background(RED)
87+
88+
with mode2d(screenSpaceCamera):
89+
draw_texture_pro(target.texture, sourceRec, destRec, origin, 0.0, WHITE)
90+
91+
# end_mode2d()
92+
93+
draw_text(f"Screen resolution: {screenWidth}, {screenHeight}", 10, 10, 20, DARKBLUE)
94+
draw_text(f"World resolution: {virtualScreenWidth}, {virtualScreenHeight}", 10, 40, 20, DARKGREEN)
95+
draw_fps(get_screen_width() - 95, 10)
96+
97+
# end_drawing()
98+
99+
unload_render_texture(target)
100+
101+
close_window()
102+
103+
return 0
104+
105+
106+
if __name__ == "__main__":
107+
if len(sys.argv) >= 2 and isinstance(sys.argv[1], str):
108+
os.chdir(sys.argv[1])
109+
print("Working dir:", os.getcwd())
110+
sys.exit(main())

0 commit comments

Comments
 (0)