Skip to content

Commit

Permalink
* fix: get_mouse_msg() / get_char() / get_key() not quit correctly w…
Browse files Browse the repository at this point in the history
…hen the graphics window is closed
  • Loading branch information
royqh1979 committed Jan 5, 2020
1 parent 9320c6d commit 98699f6
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 21 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ History
* fix: dialogs dosen't work in normal pyqt program
* change: contains_control() -> contains_ctrl()
* add: load_image() throw value error if the image file is not exist
* fix: get_mouse_msg() / get_char() / get_key() not quit correctly when the graphics window is closed

1.0.16
-----------
Expand Down
13 changes: 3 additions & 10 deletions easygraphics/easygraphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2214,19 +2214,12 @@ def get_click() -> MouseMessage:
"""
Get the mouse click message.
If there is not any mouse button is clicked in last 100 ms, the program will stop and wait for
the next clicking.
If there isn't any clicked event, the program will stop and wait for it.
:return: x of the cursor, y of the cursor , mouse buttons down
( QtCore.Qt.LeftButton or QtCore.Qt.RightButton or QtCore.Qt.MidButton or QtCore.Qt.NoButton)
:return: mouse message
"""
_check_not_headless_and_in_shell()
while is_run():
msg = _win.get_mouse_msg()
if msg.type == MouseMessageType.RELEASE_MESSAGE and contains_left_button(msg.button):
return msg

return 0, 0, QtCore.Qt.NoButton
return _win.get_click()


def clear_key_msg():
Expand Down
15 changes: 15 additions & 0 deletions easygraphics/graphwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,21 @@ def get_mouse_msg(self) -> "MouseMessage":
return MouseMessage(None,None)
pass

def get_click(self) -> "MouseMessage":
"""
Get the mouse click message.
If there isn't any clicked event, the program will stop and wait for it.
:return: mouse message
"""
while self._is_run:
msg = self.get_mouse_msg()
if msg == MouseMessageType.NO_MESSAGE or\
msg.type == MouseMessageType.RELEASE_MESSAGE and (msg.button & QtCore.Qt.LeftButton) > 0:
return msg
return MouseMessage(None,None)

def has_kb_hit(self) -> bool:
"""
See if any ascii char key hit message in the message queue.
Expand Down
65 changes: 65 additions & 0 deletions examples/games/input_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
A Print game
"""
from easygraphics import *
import random


def show_welcome():
clear_device()
set_color("yellow")
set_font_size(64)
draw_text(160, 110, "Print Game");
set_color("white");
c = 0
set_font_size(20)
while is_run():
if has_kb_hit():
break
set_color(color_rgb(c, c, c))
draw_text(180, 400, "Press any key to continue")
c = (c + 8) % 255;
delay_fps(30)
ch = get_char()
clear_device()


def show_goodbye():
clear_device();
set_color("yellow");
set_font_size(48);
draw_text(104, 180, "Bye!!!");
pause()


def main():
init_graph(640, 480)
set_render_mode(RenderMode.RENDER_MANUAL)
set_background_color("black")

show_welcome()
random.seed()
set_font_size(20)
set_fill_color("black")

while is_run():
target = chr(65 + random.randint(0, 25))
x = random.randint(0, 620)
for y in range(16, 460):
set_color("white")
draw_text(x, y, target)
if has_kb_hit():
key = get_char()
if key.upper() == target:
fill_rect(x - 2, y - 22, x + 22, y + 2) # clear the char and generate next char
break
if key == " ":
show_goodbye()
close_graph()
exit()
delay_fps(60)
fill_rect(x - 2, y - 22, x + 22, y + 2) # clear the char

close_graph()

easy_run(main)
23 changes: 12 additions & 11 deletions examples/games/reversi.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,18 @@ def play():
now=B
print_board()
print_current()
while is_run():
msg = get_mouse_msg()
if msg.type == MouseMessageType.PRESS_MESSAGE:
x=msg.x//60
y=msg.y//60
if judge(x,y,now):
update(x,y,now)
now = B if now == W else W
print_current()
if finised() or losed(W) or losed(B):
break
while True:
msg = get_click()
if not is_run():
break
x=msg.x//60
y=msg.y//60
if judge(x,y,now):
update(x,y,now)
now = B if now == W else W
print_current()
if finised() or losed(W) or losed(B):
break

def main():
init_graph(640,480)
Expand Down

0 comments on commit 98699f6

Please sign in to comment.