From 54d8dd96bf3d35156b2fb3b362cb50f4f9c7a48c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=93=D7=95=D7=A8=20=D7=A9=D7=A4=D7=99=D7=A8=D7=90?= Date: Fri, 16 Sep 2022 13:23:30 +0300 Subject: [PATCH] adding shapes_following_eyes example --- examples/shapes/shapes_following_eyes.py | 96 ++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 examples/shapes/shapes_following_eyes.py diff --git a/examples/shapes/shapes_following_eyes.py b/examples/shapes/shapes_following_eyes.py new file mode 100644 index 0000000..5f99e0e --- /dev/null +++ b/examples/shapes/shapes_following_eyes.py @@ -0,0 +1,96 @@ +""" + +raylib [shapes] example - Following Eyes + +""" + +from pyray import * +from raylib.colors import ( + RAYWHITE, + BROWN, + BLACK, + LIGHTGRAY, + DARKGREEN, +) +from math import ( + atan2, + cos, + sin +) + +# Initialization +# ---------------------------------------------------------------------------------- +screenWidth = 800 +screenHeight = 450 + +init_window(screenWidth, screenHeight, "raylib [shapes] example - following eyes") + +scleraLeftPosition = Vector2(screenWidth / 2.0 - 100.0, screenHeight / 2.0) +scleraRightPosition = Vector2(screenWidth / 2.0 + 100.0, screenHeight / 2.0) +scleraRadius = 80.0 + +irisLeftPosition = Vector2(screenWidth / 2.0 - 100.0, screenHeight / 2.0) +irisRightPosition = Vector2(screenWidth / 2.0 - 100.0, screenHeight / 2.0) +irisRadius = 24.0 + +angle = 0.0 +dx, dy, dxx, dyy = 0.0, 0.0, 0.0, 0.0 + +set_target_fps(60) +# ---------------------------------------------------------------------------------- + +# Main game loop +while not window_should_close(): # Detect window close button or ESC key + # Update + # ---------------------------------------------------------------------------------- + irisLeftPosition = get_mouse_position() + irisRightPosition = get_mouse_position() + + # Check not inside the left eye sclera + if not check_collision_point_circle(irisLeftPosition, scleraLeftPosition, scleraRadius - 20): + dx = irisLeftPosition.x - scleraLeftPosition.x + dy = irisLeftPosition.y - scleraLeftPosition.y + + angle = atan2(dy, dx) + + dxx = (scleraRadius - irisRadius)*cos(angle) + dyy = (scleraRadius - irisRadius)*sin(angle) + + irisLeftPosition.x = scleraLeftPosition.x + dxx + irisLeftPosition.y = scleraLeftPosition.y + dyy + + # Check not inside the right eye sclera + if not check_collision_point_circle(irisRightPosition, scleraRightPosition, scleraRadius - 20): + dx = irisRightPosition.x - scleraRightPosition.x + dy = irisRightPosition.y - scleraRightPosition.y + + angle = atan2(dy, dx) + + dxx = (scleraRadius - irisRadius)*cos(angle) + dyy = (scleraRadius - irisRadius)*sin(angle) + + irisRightPosition.x = scleraRightPosition.x + dxx + irisRightPosition.y = scleraRightPosition.y + dyy + + # ---------------------------------------------------------------------------------- + + # draw + # ---------------------------------------------------------------------------------- + begin_drawing() + + clear_background(RAYWHITE) + + draw_circle_v(scleraLeftPosition, scleraRadius, LIGHTGRAY) + draw_circle_v(irisLeftPosition, irisRadius, BROWN) + draw_circle_v(irisLeftPosition, 10, BLACK) + + draw_circle_v(scleraRightPosition, scleraRadius, LIGHTGRAY) + draw_circle_v(irisRightPosition, irisRadius, DARKGREEN) + draw_circle_v(irisRightPosition, 10, BLACK) + + draw_fps(10, 10) + + end_drawing() + +# De-Initialization +close_window() # Close window and OpenGL context