|
| 1 | +""" |
| 2 | +
|
| 3 | +raylib [shapes] example - Following Eyes |
| 4 | +
|
| 5 | +""" |
| 6 | + |
| 7 | +from pyray import * |
| 8 | +from raylib.colors import ( |
| 9 | + RAYWHITE, |
| 10 | + BROWN, |
| 11 | + BLACK, |
| 12 | + LIGHTGRAY, |
| 13 | + DARKGREEN, |
| 14 | +) |
| 15 | +from math import ( |
| 16 | + atan2, |
| 17 | + cos, |
| 18 | + sin |
| 19 | +) |
| 20 | + |
| 21 | +# Initialization |
| 22 | +# ---------------------------------------------------------------------------------- |
| 23 | +screenWidth = 800 |
| 24 | +screenHeight = 450 |
| 25 | + |
| 26 | +init_window(screenWidth, screenHeight, "raylib [shapes] example - following eyes") |
| 27 | + |
| 28 | +scleraLeftPosition = Vector2(screenWidth / 2.0 - 100.0, screenHeight / 2.0) |
| 29 | +scleraRightPosition = Vector2(screenWidth / 2.0 + 100.0, screenHeight / 2.0) |
| 30 | +scleraRadius = 80.0 |
| 31 | + |
| 32 | +irisLeftPosition = Vector2(screenWidth / 2.0 - 100.0, screenHeight / 2.0) |
| 33 | +irisRightPosition = Vector2(screenWidth / 2.0 - 100.0, screenHeight / 2.0) |
| 34 | +irisRadius = 24.0 |
| 35 | + |
| 36 | +angle = 0.0 |
| 37 | +dx, dy, dxx, dyy = 0.0, 0.0, 0.0, 0.0 |
| 38 | + |
| 39 | +set_target_fps(60) |
| 40 | +# ---------------------------------------------------------------------------------- |
| 41 | + |
| 42 | +# Main game loop |
| 43 | +while not window_should_close(): # Detect window close button or ESC key |
| 44 | + # Update |
| 45 | + # ---------------------------------------------------------------------------------- |
| 46 | + irisLeftPosition = get_mouse_position() |
| 47 | + irisRightPosition = get_mouse_position() |
| 48 | + |
| 49 | + # Check not inside the left eye sclera |
| 50 | + if not check_collision_point_circle(irisLeftPosition, scleraLeftPosition, scleraRadius - 20): |
| 51 | + dx = irisLeftPosition.x - scleraLeftPosition.x |
| 52 | + dy = irisLeftPosition.y - scleraLeftPosition.y |
| 53 | + |
| 54 | + angle = atan2(dy, dx) |
| 55 | + |
| 56 | + dxx = (scleraRadius - irisRadius)*cos(angle) |
| 57 | + dyy = (scleraRadius - irisRadius)*sin(angle) |
| 58 | + |
| 59 | + irisLeftPosition.x = scleraLeftPosition.x + dxx |
| 60 | + irisLeftPosition.y = scleraLeftPosition.y + dyy |
| 61 | + |
| 62 | + # Check not inside the right eye sclera |
| 63 | + if not check_collision_point_circle(irisRightPosition, scleraRightPosition, scleraRadius - 20): |
| 64 | + dx = irisRightPosition.x - scleraRightPosition.x |
| 65 | + dy = irisRightPosition.y - scleraRightPosition.y |
| 66 | + |
| 67 | + angle = atan2(dy, dx) |
| 68 | + |
| 69 | + dxx = (scleraRadius - irisRadius)*cos(angle) |
| 70 | + dyy = (scleraRadius - irisRadius)*sin(angle) |
| 71 | + |
| 72 | + irisRightPosition.x = scleraRightPosition.x + dxx |
| 73 | + irisRightPosition.y = scleraRightPosition.y + dyy |
| 74 | + |
| 75 | + # ---------------------------------------------------------------------------------- |
| 76 | + |
| 77 | + # draw |
| 78 | + # ---------------------------------------------------------------------------------- |
| 79 | + begin_drawing() |
| 80 | + |
| 81 | + clear_background(RAYWHITE) |
| 82 | + |
| 83 | + draw_circle_v(scleraLeftPosition, scleraRadius, LIGHTGRAY) |
| 84 | + draw_circle_v(irisLeftPosition, irisRadius, BROWN) |
| 85 | + draw_circle_v(irisLeftPosition, 10, BLACK) |
| 86 | + |
| 87 | + draw_circle_v(scleraRightPosition, scleraRadius, LIGHTGRAY) |
| 88 | + draw_circle_v(irisRightPosition, irisRadius, DARKGREEN) |
| 89 | + draw_circle_v(irisRightPosition, 10, BLACK) |
| 90 | + |
| 91 | + draw_fps(10, 10) |
| 92 | + |
| 93 | + end_drawing() |
| 94 | + |
| 95 | +# De-Initialization |
| 96 | +close_window() # Close window and OpenGL context |
0 commit comments