|
| 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 |
0 commit comments