adding the core_window_should_close example
This commit is contained in:
parent
603d4888a8
commit
a278a38069
1 changed files with 74 additions and 0 deletions
74
examples/core/core_window_should_close.py
Normal file
74
examples/core/core_window_should_close.py
Normal file
|
@ -0,0 +1,74 @@
|
|||
"""
|
||||
|
||||
raylib [core] example - Window Should Close
|
||||
|
||||
"""
|
||||
from pyray import *
|
||||
from raylib.colors import (
|
||||
RAYWHITE,
|
||||
LIGHTGRAY,
|
||||
WHITE,
|
||||
BLACK
|
||||
)
|
||||
from raylib import (
|
||||
KEY_NULL,
|
||||
KEY_ESCAPE,
|
||||
KEY_Y,
|
||||
KEY_N
|
||||
)
|
||||
|
||||
# Initialization
|
||||
# --------------------------------------------------------------------------------------
|
||||
SCREEN_WIDTH = 800
|
||||
SCREEN_HEIGHT = 450
|
||||
|
||||
init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - window should close")
|
||||
|
||||
set_exit_key(KEY_NULL) # Disable KEY_ESCAPE to close window, X-button still works
|
||||
|
||||
exitWindowRequested = False # Flag to request window to exit
|
||||
exitWindow = False # Flag to set window to exit
|
||||
|
||||
set_target_fps(60) # Set our game to run at 60 frames-per-second
|
||||
# --------------------------------------------------------------------------------------
|
||||
|
||||
# Main game loop
|
||||
while not exitWindow:
|
||||
|
||||
# Update
|
||||
# ----------------------------------------------------------------------------------
|
||||
# Detect if X-button or KEY_ESCAPE have been pressed to close window
|
||||
if window_should_close() or is_key_pressed(KEY_ESCAPE):
|
||||
exitWindowRequested = True
|
||||
|
||||
if exitWindowRequested:
|
||||
|
||||
# A request for close window has been issued, we can save data before closing
|
||||
# or just show a message asking for confirmation
|
||||
|
||||
if is_key_pressed(KEY_Y):
|
||||
exitWindow = True
|
||||
elif is_key_pressed(KEY_N):
|
||||
exitWindowRequested = False
|
||||
|
||||
# ----------------------------------------------------------------------------------
|
||||
|
||||
# Draw
|
||||
# ----------------------------------------------------------------------------------
|
||||
begin_drawing()
|
||||
|
||||
clear_background(RAYWHITE)
|
||||
|
||||
if exitWindowRequested:
|
||||
|
||||
draw_rectangle(0, 100, SCREEN_WIDTH, 200, BLACK)
|
||||
draw_text("Are you sure you want to exit program? [Y/N]", 40, 180, 30, WHITE)
|
||||
|
||||
else:
|
||||
draw_text("Try to close the window to get confirmation message!", 120, 200, 20, LIGHTGRAY)
|
||||
|
||||
end_drawing()
|
||||
# ----------------------------------------------------------------------------------
|
||||
|
||||
# De-Initialization
|
||||
close_window() # Close window and OpenGL context
|
Reference in a new issue