Skip to content

Commit c437d62

Browse files
Merge pull request #75 from sDos280/master
adding more examples
2 parents a115b9b + ad21fe7 commit c437d62

File tree

3 files changed

+335
-0
lines changed

3 files changed

+335
-0
lines changed

examples/core/core_scissor_test.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
3+
raylib [core] example - Scissor Test
4+
5+
"""
6+
from pyray import *
7+
from raylib.colors import (
8+
RAYWHITE,
9+
LIGHTGRAY,
10+
RED,
11+
BLACK
12+
)
13+
from raylib import (
14+
KEY_S
15+
)
16+
17+
# Initialization
18+
# --------------------------------------------------------------------------------------
19+
SCREEN_WIDTH = 800
20+
SCREEN_HEIGHT = 450
21+
22+
init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - scissor test")
23+
24+
scissorArea = Rectangle(0, 0, 300, 300)
25+
scissorMode = True
26+
27+
set_target_fps(60) # Set our game to run at 60 frames-per-second
28+
# --------------------------------------------------------------------------------------
29+
30+
# Main game loop
31+
while not window_should_close(): # Detect window close button or ESC key
32+
# Update
33+
# ----------------------------------------------------------------------------------
34+
if is_key_pressed(KEY_S):
35+
scissorMode = not scissorMode
36+
37+
# Centre the scissor area around the mouse position
38+
scissorArea.x = get_mouse_x() - scissorArea.width/2
39+
scissorArea.y = get_mouse_y() - scissorArea.height/2
40+
# ----------------------------------------------------------------------------------
41+
42+
43+
# Draw
44+
# ----------------------------------------------------------------------------------
45+
begin_drawing()
46+
47+
clear_background(RAYWHITE)
48+
if scissorMode:
49+
begin_scissor_mode(int(scissorArea.x), int(scissorArea.y), int(scissorArea.width), int(scissorArea.height))
50+
51+
# Draw full screen rectangle and some text
52+
# NOTE: Only part defined by scissor area will be rendered
53+
draw_rectangle(0, 0, get_screen_width(), get_screen_height(), RED)
54+
draw_text("Move the mouse around to reveal this text!", 190, 200, 20, LIGHTGRAY)
55+
56+
if scissorMode:
57+
end_scissor_mode()
58+
59+
draw_rectangle_lines_ex(scissorArea, 1, BLACK)
60+
draw_text("Press S to toggle scissor test", 10, 10, 20, BLACK)
61+
62+
end_drawing()
63+
64+
# De-Initialization
65+
close_window() # Close window and OpenGL context
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
3+
raylib [core] example - Window Should Close
4+
5+
"""
6+
from pyray import *
7+
from raylib.colors import (
8+
RAYWHITE,
9+
LIGHTGRAY,
10+
WHITE,
11+
BLACK
12+
)
13+
from raylib import (
14+
KEY_NULL,
15+
KEY_ESCAPE,
16+
KEY_Y,
17+
KEY_N
18+
)
19+
20+
# Initialization
21+
# --------------------------------------------------------------------------------------
22+
SCREEN_WIDTH = 800
23+
SCREEN_HEIGHT = 450
24+
25+
init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - window should close")
26+
27+
set_exit_key(KEY_NULL) # Disable KEY_ESCAPE to close window, X-button still works
28+
29+
exitWindowRequested = False # Flag to request window to exit
30+
exitWindow = False # Flag to set window to exit
31+
32+
set_target_fps(60) # Set our game to run at 60 frames-per-second
33+
# --------------------------------------------------------------------------------------
34+
35+
# Main game loop
36+
while not exitWindow:
37+
38+
# Update
39+
# ----------------------------------------------------------------------------------
40+
# Detect if X-button or KEY_ESCAPE have been pressed to close window
41+
if window_should_close() or is_key_pressed(KEY_ESCAPE):
42+
exitWindowRequested = True
43+
44+
if exitWindowRequested:
45+
46+
# A request for close window has been issued, we can save data before closing
47+
# or just show a message asking for confirmation
48+
49+
if is_key_pressed(KEY_Y):
50+
exitWindow = True
51+
elif is_key_pressed(KEY_N):
52+
exitWindowRequested = False
53+
54+
# ----------------------------------------------------------------------------------
55+
56+
# Draw
57+
# ----------------------------------------------------------------------------------
58+
begin_drawing()
59+
60+
clear_background(RAYWHITE)
61+
62+
if exitWindowRequested:
63+
64+
draw_rectangle(0, 100, SCREEN_WIDTH, 200, BLACK)
65+
draw_text("Are you sure you want to exit program? [Y/N]", 40, 180, 30, WHITE)
66+
67+
else:
68+
draw_text("Try to close the window to get confirmation message!", 120, 200, 20, LIGHTGRAY)
69+
70+
end_drawing()
71+
# ----------------------------------------------------------------------------------
72+
73+
# De-Initialization
74+
close_window() # Close window and OpenGL context
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
"""
2+
3+
raylib [texture] example - Mouse Painting
4+
5+
"""
6+
from pyray import *
7+
from raylib.colors import *
8+
from raylib import (
9+
KEY_RIGHT,
10+
KEY_LEFT,
11+
MOUSE_BUTTON_LEFT,
12+
KEY_C,
13+
GESTURE_DRAG,
14+
MOUSE_BUTTON_RIGHT,
15+
KEY_S
16+
)
17+
18+
MAX_COLORS_COUNT = 23 # Number of colors available
19+
20+
# Initialization
21+
screenWidth = 800
22+
screenHeight = 450
23+
24+
init_window(screenWidth, screenHeight, "raylib [textures] example - mouse painting")
25+
26+
# Colours to choose from
27+
colors = [RAYWHITE, YELLOW, GOLD, ORANGE, PINK, RED, MAROON, GREEN, LIME, DARKGREEN,
28+
SKYBLUE, BLUE, DARKBLUE, PURPLE, VIOLET, DARKPURPLE, BEIGE, BROWN, DARKBROWN,
29+
LIGHTGRAY, GRAY, DARKGRAY, BLACK]
30+
31+
colorsRecs = []
32+
33+
# Define colorsRecs data (for every rectangle)
34+
for i in range(MAX_COLORS_COUNT):
35+
colorsRecs.append(Rectangle(10 + 30.0 * i + 2 * i, 10, 30, 30))
36+
37+
colorSelected = 0
38+
colorSelectedPrev = colorSelected
39+
colorMouseHover = 0
40+
brushSize = 20.0
41+
mouseWasPressed = False
42+
43+
btnSaveRec = Rectangle(750, 10, 40, 30)
44+
btnSaveMouseHover = False
45+
showSaveMessage = False
46+
saveMessageCounter = 0
47+
48+
# Create a RenderTexture2D to use as a canvas
49+
target = load_render_texture(screenWidth, screenHeight)
50+
51+
# Clear render texture before entering the game loop
52+
begin_texture_mode(target)
53+
clear_background(colors[0])
54+
end_texture_mode()
55+
56+
set_target_fps(120) # Set our game to run at 120 frames-per-second
57+
58+
# Main game loop
59+
while not window_should_close(): # Detect window close button or ESC key
60+
# Update
61+
# ----------------------------------------------------------------------------------
62+
mousePos = get_mouse_position()
63+
64+
# Move between colors with keys
65+
if is_key_pressed(KEY_RIGHT):
66+
colorSelected += 1
67+
elif is_key_pressed(KEY_LEFT):
68+
colorSelected -= 1
69+
70+
if colorSelected >= MAX_COLORS_COUNT:
71+
colorSelected = MAX_COLORS_COUNT - 1
72+
elif colorSelected < 0:
73+
colorSelected = 0
74+
75+
# Choose color with mouse
76+
for i in range(MAX_COLORS_COUNT):
77+
if check_collision_point_rec(mousePos, colorsRecs[i]):
78+
colorMouseHover = i
79+
break
80+
else:
81+
colorMouseHover = -1
82+
83+
if colorMouseHover >= 0 and is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
84+
colorSelected = colorMouseHover
85+
colorSelectedPrev = colorSelected
86+
87+
# Change brush size
88+
brushSize += get_mouse_wheel_move() * 5
89+
if brushSize < 2: brushSize = 2
90+
if brushSize > 50: brushSize = 50
91+
92+
if is_key_pressed(KEY_C):
93+
# Clear render texture to clear color
94+
begin_texture_mode(target)
95+
clear_background(colors[0])
96+
end_texture_mode()
97+
98+
if is_mouse_button_pressed(MOUSE_BUTTON_LEFT) or get_gesture_detected() == GESTURE_DRAG:
99+
100+
# Paint circle into render texture
101+
# NOTE: To avoid discontinuous circles, we could store
102+
# previous-next mouse points and just draw a line using brush size
103+
begin_texture_mode(target)
104+
if mousePos.y > 50:
105+
draw_circle(int(mousePos.x), int(mousePos.y), brushSize, colors[colorSelected])
106+
end_texture_mode()
107+
if is_mouse_button_down(MOUSE_BUTTON_RIGHT):
108+
109+
if not mouseWasPressed:
110+
colorSelectedPrev = colorSelected
111+
colorSelected = 0
112+
113+
mouseWasPressed = True
114+
115+
# Erase circle from render texture
116+
begin_texture_mode(target)
117+
if mousePos.y > 50: draw_circle(int(mousePos.x), int(mousePos.y), brushSize, colors[0])
118+
end_texture_mode()
119+
120+
elif is_mouse_button_released(MOUSE_BUTTON_RIGHT) and mouseWasPressed:
121+
122+
colorSelected = colorSelectedPrev
123+
mouseWasPressed = False
124+
125+
# Check mouse hover save button
126+
if check_collision_point_rec(mousePos, btnSaveRec):
127+
btnSaveMouseHover = True
128+
else:
129+
btnSaveMouseHover = False
130+
131+
# Image saving logic
132+
# NOTE: Saving painted texture to a default named image
133+
if (btnSaveMouseHover and is_mouse_button_released(MOUSE_BUTTON_LEFT)) or is_key_pressed(KEY_S):
134+
image = load_image_from_texture(target.texture)
135+
image_flip_vertical(image)
136+
export_image(image, "my_amazing_texture_painting.png")
137+
unload_image(image)
138+
showSaveMessage = True
139+
140+
if showSaveMessage:
141+
# On saving, show a full screen message for 2 seconds
142+
saveMessageCounter += 1
143+
if saveMessageCounter > 240:
144+
showSaveMessage = False
145+
saveMessageCounter = 0
146+
147+
# ----------------------------------------------------------------------------------
148+
149+
# Draw
150+
# ----------------------------------------------------------------------------------
151+
begin_drawing()
152+
clear_background(RAYWHITE)
153+
154+
# NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
155+
draw_texture_rec(target.texture, Rectangle(0, 0, float(target.texture.width), float(-target.texture.height)),
156+
Vector2(0, 0), WHITE)
157+
158+
# Draw drawing circle for reference
159+
if mousePos.y > 50:
160+
if is_mouse_button_down(MOUSE_BUTTON_RIGHT):
161+
draw_circle_lines(int(mousePos.x), int(mousePos.y), brushSize, GRAY)
162+
else:
163+
draw_circle(get_mouse_x(), get_mouse_y(), brushSize, colors[colorSelected])
164+
# Draw top panel
165+
draw_rectangle(0, 0, get_screen_width(), 50, RAYWHITE)
166+
draw_line(0, 50, get_screen_width(), 50, LIGHTGRAY)
167+
168+
# Draw color selection rectangles
169+
for i in range(MAX_COLORS_COUNT):
170+
draw_rectangle_rec(colorsRecs[i], colors[i])
171+
draw_rectangle_lines(10, 10, 30, 30, LIGHTGRAY)
172+
173+
if colorMouseHover >= 0: draw_rectangle_rec(colorsRecs[colorMouseHover], fade(WHITE, 0.6))
174+
175+
draw_rectangle_lines_ex(
176+
Rectangle(colorsRecs[colorSelected].x - 2, colorsRecs[colorSelected].y - 2, colorsRecs[colorSelected].width + 4,
177+
colorsRecs[colorSelected].height + 4), 2, BLACK)
178+
179+
# Draw save image button
180+
draw_rectangle_lines_ex(btnSaveRec, 2, RED if btnSaveMouseHover else BLACK)
181+
draw_text("SAVE!", 755, 20, 10, RED if btnSaveMouseHover else BLACK)
182+
183+
if showSaveMessage:
184+
draw_rectangle(0, 0, get_screen_width(), get_screen_height(), fade(RAYWHITE, 0.8))
185+
draw_rectangle(0, 150, get_screen_width(), 80, BLACK)
186+
draw_text("IMAGE SAVED: my_amazing_texture_painting.png", 150, 180, 20, RAYWHITE)
187+
188+
end_drawing()
189+
190+
# ----------------------------------------------------------------------------------
191+
192+
# De-Initialization
193+
# ----------------------------------------------------------------------------------
194+
unload_render_texture(target) # Unload render texture
195+
196+
close_window() # Close window and OpenGL context

0 commit comments

Comments
 (0)