PyGame 菜单格式
PyGame Menu Formatting
所以我最近一直致力于 Python 中的游戏编程以及名为 PyGame 的外部库。如果有人熟悉《炉石传说》游戏,那正是我想要的感觉。然而,这不是重点。我已经研究过如何显示图像及其位置,而且我一直在努力想办法在 PyGame UI 中创建一个带有按钮的菜单。我以前用 Visual Basic 编程过,我想知道 PyGame 接口是否允许一般系统事件检测,与 Visual Basic 不同(即鼠标点击、按键)。帮助将不胜感激。提前致谢!
需要创建一个 while
循环才能在 pygame 中进行事件检测。这是一个例子:
import time
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
#happens when 'X' in window is clicked
#put code to be executed here
elif event.type == pygame.MOUSEBUTTONDOWN:
onclick_function(pygame.mouse.get_pos)
#passes mouse position as tuple to function to deal with click control.
将图像以位方式传送到显示器时,需要知道坐标。只需保留一个 Rect
对象的列表,每个对象都有一个在单击时执行的函数,并使用一个函数遍历列表并查看鼠标的坐标是否在 Rect
内。
查看将图像 blitting 到显示器的链接:
链接:
-Rect
: http://www.pygame.org/docs/ref/rect.html#pygame.Rect
-Surface
: http://www.pygame.org/docs/ref/surface.html#pygame.Surface
I was wondering if the PyGame interface allowed for system event detection in general, not unlike Visual Basic
简答:没有。 Pygame 不是事件驱动的。您必须自己编写所有内容,因为 pygame 只是 SDL 的一个小包装,具有一些额外的功能。
所以我最近一直致力于 Python 中的游戏编程以及名为 PyGame 的外部库。如果有人熟悉《炉石传说》游戏,那正是我想要的感觉。然而,这不是重点。我已经研究过如何显示图像及其位置,而且我一直在努力想办法在 PyGame UI 中创建一个带有按钮的菜单。我以前用 Visual Basic 编程过,我想知道 PyGame 接口是否允许一般系统事件检测,与 Visual Basic 不同(即鼠标点击、按键)。帮助将不胜感激。提前致谢!
需要创建一个 while
循环才能在 pygame 中进行事件检测。这是一个例子:
import time
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
#happens when 'X' in window is clicked
#put code to be executed here
elif event.type == pygame.MOUSEBUTTONDOWN:
onclick_function(pygame.mouse.get_pos)
#passes mouse position as tuple to function to deal with click control.
将图像以位方式传送到显示器时,需要知道坐标。只需保留一个 Rect
对象的列表,每个对象都有一个在单击时执行的函数,并使用一个函数遍历列表并查看鼠标的坐标是否在 Rect
内。
查看将图像 blitting 到显示器的链接:
链接:
-Rect
: http://www.pygame.org/docs/ref/rect.html#pygame.Rect
-Surface
: http://www.pygame.org/docs/ref/surface.html#pygame.Surface
I was wondering if the PyGame interface allowed for system event detection in general, not unlike Visual Basic
简答:没有。 Pygame 不是事件驱动的。您必须自己编写所有内容,因为 pygame 只是 SDL 的一个小包装,具有一些额外的功能。