Skip to content

Commit b23957d

Browse files
Converting shapes_basic_shapes.c to python and adding it to examples (#104)
* Converting shapes_basic_shapes.c to python and adding it to examples * Add shapes_bouncing_ball.py to examples in shapes --------- Co-authored-by: pranavganesh <[email protected]>
1 parent f735bd5 commit b23957d

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import pyray
2+
from raylib.colors import (
3+
RAYWHITE,
4+
DARKGRAY,
5+
DARKBLUE,
6+
SKYBLUE,
7+
MAROON,
8+
ORANGE,
9+
RED,
10+
VIOLET,
11+
BEIGE,
12+
BROWN,
13+
BLACK,
14+
GREEN,
15+
GOLD
16+
)
17+
18+
19+
# Initialization
20+
screenWidth = 800
21+
screenHeight = 450
22+
pyray.init_window(screenWidth, screenHeight, "raylib [shapes] example - basic shapes drawing")
23+
24+
rotation = 0.0
25+
26+
pyray.set_target_fps(60)
27+
28+
# Main game loop
29+
while not pyray.window_should_close():
30+
# Update
31+
rotation += 0.2
32+
33+
# Draw
34+
pyray.begin_drawing()
35+
36+
pyray.clear_background(RAYWHITE)
37+
38+
pyray.draw_text("some basic shapes available on raylib", 20, 20, 20, DARKGRAY)
39+
40+
# Circle shapes and lines
41+
pyray.draw_circle(screenWidth // 5, 120, 35, DARKBLUE)
42+
pyray.draw_circle_gradient(screenWidth // 5, 220, 60, GREEN, SKYBLUE)
43+
pyray.draw_circle_lines(screenWidth // 5, 340, 80, DARKBLUE)
44+
45+
# Rectangle shapes and lines
46+
pyray.draw_rectangle(screenWidth // 4 * 2 - 60, 100, 120, 60, RED)
47+
pyray.draw_rectangle_gradient_h(screenWidth // 4 * 2 - 90, 170, 180, 130, MAROON, GOLD)
48+
pyray.draw_rectangle_lines(screenWidth // 4 * 2 - 40, 320, 80, 60, ORANGE)
49+
50+
# Triangle shapes and lines
51+
pyray.draw_triangle(pyray.Vector2(screenWidth / 4.0 * 3.0, 80.0),
52+
pyray.Vector2(screenWidth / 4.0 * 3.0 - 60.0, 150.0),
53+
pyray.Vector2(screenWidth / 4.0 * 3.0 + 60.0, 150.0), VIOLET)
54+
55+
pyray.draw_triangle_lines(pyray.Vector2(screenWidth / 4.0 * 3.0, 160.0),
56+
pyray.Vector2(screenWidth / 4.0 * 3.0 - 20.0, 230.0),
57+
pyray.Vector2(screenWidth / 4.0 * 3.0 + 20.0, 230.0), DARKBLUE)
58+
59+
# Polygon shapes and lines
60+
pyray.draw_poly(pyray.Vector2(screenWidth / 4.0 * 3, 330), 6, 80, rotation, BROWN)
61+
pyray.draw_poly_lines(pyray.Vector2(screenWidth / 4.0 * 3, 330), 6, 90, rotation, BROWN)
62+
pyray.draw_poly_lines_ex(pyray.Vector2(screenWidth / 4.0 * 3, 330), 6, 85, rotation, 6, BEIGE)
63+
64+
# NOTE: We draw all LINES based shapes together to optimize internal drawing,
65+
# this way, all LINES are rendered in a single draw pass
66+
pyray.draw_line(18, 42, screenWidth - 18, 42, BLACK)
67+
68+
pyray.end_drawing()
69+
70+
# De-Initialization
71+
pyray.close_window()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pyray
2+
3+
# Initialization
4+
screenWidth = 800
5+
screenHeight = 450
6+
pyray.init_window(screenWidth, screenHeight, "pyray [shapes] example - bouncing ball")
7+
8+
ballPosition = pyray.Vector2(pyray.get_screen_width() / 2.0, pyray.get_screen_height() / 2.0)
9+
ballSpeed = pyray.Vector2(5.0, 4.0)
10+
ballRadius = 20
11+
12+
pause = False
13+
framesCounter = 0
14+
15+
pyray.set_target_fps(60)
16+
17+
# Main game loop
18+
while not pyray.window_should_close():
19+
# Update
20+
if pyray.is_key_pressed(pyray.KEY_SPACE):
21+
pause = not pause
22+
23+
if not pause:
24+
ballPosition.x += ballSpeed.x
25+
ballPosition.y += ballSpeed.y
26+
27+
# Check walls collision for bouncing
28+
if (ballPosition.x >= (pyray.get_screen_width() - ballRadius)) or (ballPosition.x <= ballRadius):
29+
ballSpeed.x *= -1.0
30+
if (ballPosition.y >= (pyray.get_screen_height() - ballRadius)) or (ballPosition.y <= ballRadius):
31+
ballSpeed.y *= -1.0
32+
else:
33+
framesCounter += 1
34+
35+
# Draw
36+
pyray.begin_drawing()
37+
38+
pyray.clear_background(pyray.RAYWHITE)
39+
40+
pyray.draw_circle_v(ballPosition, ballRadius, pyray.MAROON)
41+
pyray.draw_text("PRESS SPACE to PAUSE BALL MOVEMENT", 10, pyray.get_screen_height() - 25, 20, pyray.LIGHTGRAY)
42+
43+
# On pause, we draw a blinking message
44+
if pause and (framesCounter // 30) % 2:
45+
pyray.draw_text("PAUSED", 350, 200, 30, pyray.GRAY)
46+
47+
pyray.draw_fps(10, 10)
48+
49+
pyray.end_drawing()
50+
51+
# De-Initialization
52+
pyray.close_window()

0 commit comments

Comments
 (0)