From 76e945e1c9974963913f2389c8c23efaa8089cb1 Mon Sep 17 00:00:00 2001 From: Matt Lebrun Date: Thu, 30 Jul 2020 20:43:39 +0800 Subject: [PATCH] Core examples (#11) * Added core_input_keys example * Added core_basic_window example * Added core_input_mouse example * Added core_input_mouse_wheel example --- examples/core/core_basic_window.py | 41 +++++++++++++++++ examples/core/core_input_keys.py | 47 ++++++++++++++++++++ examples/core/core_input_mouse.py | 58 +++++++++++++++++++++++++ examples/core/core_input_mouse_wheel.py | 53 ++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 examples/core/core_basic_window.py create mode 100644 examples/core/core_input_keys.py create mode 100644 examples/core/core_input_mouse.py create mode 100644 examples/core/core_input_mouse_wheel.py diff --git a/examples/core/core_basic_window.py b/examples/core/core_basic_window.py new file mode 100644 index 0000000..7a8cb0f --- /dev/null +++ b/examples/core/core_basic_window.py @@ -0,0 +1,41 @@ +""" + +raylib [core] example - Basic window + +""" +from raylib.pyray import PyRay +from raylib.colors import ( + RAYWHITE, + LIGHTGRAY, +) + + +pyray = PyRay() + + +# Initialization +SCREEN_WIDTH = 800 +SCREEN_HEIGHT = 450 + +pyray.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, + 'raylib [core] example - basic window') +pyray.set_target_fps(60) # Set our game to run at 60 frames-per-second + + +# Main game loop +while not pyray.window_should_close(): # Detect window close button or ESC key + # Update + # TODO: Update your variables here + + # Draw + pyray.begin_drawing() + + pyray.clear_background(RAYWHITE) + pyray.draw_text( + 'Congrats! You created your first window!', 190, 200, 20, LIGHTGRAY) + + pyray.end_drawing() + + +# De-Initialization +pyray.close_window() # Close window and OpenGL context diff --git a/examples/core/core_input_keys.py b/examples/core/core_input_keys.py new file mode 100644 index 0000000..3680d6e --- /dev/null +++ b/examples/core/core_input_keys.py @@ -0,0 +1,47 @@ +""" + +raylib [core] example - Keyboard input + +""" +from raylib.pyray import PyRay +from raylib.colors import ( + RAYWHITE, + DARKGRAY, + MAROON, +) + + +pyray = PyRay() + + +# Initialization +SCREEN_WIDTH = 800 +SCREEN_HEIGHT = 450 + +pyray.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, + 'raylib [core] example - keyboard input') +ball_position = pyray.Vector2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) + +pyray.set_target_fps(60) # Set our game to run at 60 frames-per-second + + +# Main game loop +while not pyray.window_should_close(): # Detect window close button or ESC key + # Update + if pyray.is_key_down(pyray.KEY_RIGHT): ball_position.x += 2 + if pyray.is_key_down(pyray.KEY_LEFT): ball_position.x -= 2 + if pyray.is_key_down(pyray.KEY_UP): ball_position.y -= 2 + if pyray.is_key_down(pyray.KEY_DOWN): ball_position.y += 2 + + # Draw + pyray.begin_drawing() + + pyray.clear_background(RAYWHITE) + pyray.draw_text('move the ball with arrow keys', 10, 10, 20, DARKGRAY) + pyray.draw_circle_v(ball_position, 50, MAROON) + + pyray.end_drawing() + + +# De-Initialization +pyray.close_window() # Close window and OpenGL context diff --git a/examples/core/core_input_mouse.py b/examples/core/core_input_mouse.py new file mode 100644 index 0000000..bd6c82d --- /dev/null +++ b/examples/core/core_input_mouse.py @@ -0,0 +1,58 @@ +""" + +raylib [core] example - Mouse input + +""" +from raylib.pyray import PyRay +from raylib.colors import ( + RAYWHITE, + DARKGRAY, + MAROON, + DARKBLUE, + LIME, +) + + +pyray = PyRay() + + +# Initialization +SCREEN_WIDTH = 800 +SCREEN_HEIGHT = 450 + +pyray.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, + 'raylib [core] example - mouse input') + +ball_position = pyray.Vector2(-100, -100) +ball_color = DARKBLUE + +pyray.set_target_fps(60) # Set our game to run at 60 frames-per-second + + +# Main game loop +while not pyray.window_should_close(): # Detect window close button or ESC key + # Update + ball_position = pyray.get_mouse_position() + + if pyray.is_mouse_button_pressed(pyray.MOUSE_LEFT_BUTTON): + ball_color = MAROON + elif pyray.is_mouse_button_pressed(pyray.MOUSE_MIDDLE_BUTTON): + ball_color = LIME + elif pyray.is_mouse_button_pressed(pyray.MOUSE_RIGHT_BUTTON): + ball_color = DARKBLUE + + # Draw + pyray.begin_drawing() + + pyray.clear_background(RAYWHITE) + pyray.draw_circle_v(ball_position, 40, ball_color) + pyray.draw_text( + 'move ball with mouse and click mouse button to change color', + 10, 10, 20, DARKGRAY + ) + + pyray.end_drawing() + + +# De-Initialization +pyray.close_window() # Close window and OpenGL context diff --git a/examples/core/core_input_mouse_wheel.py b/examples/core/core_input_mouse_wheel.py new file mode 100644 index 0000000..3aee861 --- /dev/null +++ b/examples/core/core_input_mouse_wheel.py @@ -0,0 +1,53 @@ +""" + +raylib [core] example - Mouse wheel input + +""" +from raylib.pyray import PyRay +from raylib.colors import ( + RAYWHITE, + GRAY, + LIGHTGRAY, + MAROON, +) + + +pyray = PyRay() + + +# Initialization +SCREEN_WIDTH = 800 +SCREEN_HEIGHT = 450 + +pyray.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, + 'raylib [core] example - input mouse wheel') + +box_position_y: int = SCREEN_HEIGHT // 2 - 40 +scroll_speed = 4 # Scrolling speed in pixels + +pyray.set_target_fps(60) # Set our game to run at 60 frames-per-second + + +# Main game loop +while not pyray.window_should_close(): # Detect window close button or ESC key + # Update + box_position_y -= (pyray.get_mouse_wheel_move() * scroll_speed) + + # Draw + pyray.begin_drawing() + + pyray.clear_background(RAYWHITE) + + pyray.draw_rectangle( + SCREEN_WIDTH // 2 - 40, box_position_y, 80, 80, MAROON) + + pyray.draw_text('User mouse wheel to move the cube up and down!', + 10, 10, 20, GRAY) + pyray.draw_text('Box position Y: {:03d}'.format(box_position_y), + 10, 40, 20, LIGHTGRAY) + + pyray.end_drawing() + + +# De-Initialization +pyray.close_window() # Close window and OpenGL context