一个简单的问题:为什么第一个代码工作,但看起来相同的第二个代码在pygame窗口出现时会冻结?
# Moving Pan
# Demonstrates mouse input
from livewires import games
games.init(screen_width = 640, screen_height = 480, fps = 50)
class Pan(games.Sprite):
""" A pan controlled by the mouse. """
def update(self):
""" Move to mouse coordinates. """
self.x = games.mouse.x
self.y = games.mouse.y
def main():
wall_image = games.load_image("wall.jpg", transparent = False)
games.screen.background = wall_image
pan_image = games.load_image("pan.bmp")
the_pan = Pan(image = pan_image,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(the_pan)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
# kick it off!
main()
故障的第二个:
from livewires import games,color
games.init (screen_width = 640, screen_height = 480, fps = 50)
#Creating a moving object tied to the cursor. This includes one method with two
#lines of code.
class Pan (games.Sprite):
def moved (self):
#Receives mouse position
self.x = games.mouse.x
#Changes mouse position to new x,y values.
self.y = games.mouse.y
#The Main
myscr = games.screen
myscr.set_background (games.load_image ("wall.jpg", transparent = False))
pan_image = games.load_image ("pan.bmp")
le_pan = Pan (image = pan_image,
x = games.mouse.x,
y = games.mouse.y)
games.mouse.is_visible = False
myscr.add (le_pan)
myscr.event_grab = True
myscr.mainloop()
答案 0 :(得分:2)
我从未使用过livewires,但在游戏中你通常需要 - 或多或少 - 无尽的游戏循环。
游戏循环背后的意义是,你总是想知道鼠标在哪里或按下了什么键,而不仅仅是一次!所以你必须一遍又一遍地问Where is the mouse?
。为实现这一目标,您可以使用循环来检查每次执行时所需的所有内容。
在第一个例子中,游戏循环是函数main
。应用程序的流程如下:
导入所需的库
from livewires import games
初始化游戏画面
games.init(screen_width = 640, screen_height = 480, fps = 50)
声明可以在屏幕上显示的精灵
class Pan(games.Sprite):
""" A pan controlled by the mouse. """
def update(self):
""" Move to mouse coordinates. """
self.x = games.mouse.x
self.y = games.mouse.y
声明主要方法并设置游戏屏幕背景
def main():
wall_image = games.load_image("wall.jpg", transparent = False)
games.screen.background = wall_image
将上面定义的精灵添加到屏幕并将其移动到鼠标光标的位置
pan_image = games.load_image("pan.bmp")
the_pan = Pan(image = pan_image,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(the_pan)
使鼠标光标不可见并激活事件
games.mouse.is_visible = False
games.screen.event_grab = True
运行mainloop 。此方法的调用显示:Run me
( function
main
)
over and over!
games.screen.mainloop()
第一次致电
main()
在第二个例子中,没有游戏循环。应用程序的流程(更紧凑)是这样的:
导入库,初始化游戏画面,声明精灵
from livewires import games,color
games.init (screen_width = 640, screen_height = 480, fps = 50)
class Pan (games.Sprite):
def moved (self):
self.x = games.mouse.x
self.y = games.mouse.y
设置游戏画面背景并添加精灵
myscr = games.screen
myscr.set_background (games.load_image ("wall.jpg", transparent = False))
pan_image = games.load_image ("pan.bmp")
le_pan = Pan (image = pan_image,
x = games.mouse.x,
y = games.mouse.y)
myscr.add(le_pan)
停用鼠标光标,启用事件
games.mouse.is_visible = False
myscr.event_grab = True
运行mainloop 。此方法的调用显示:Run me
( function
undefined
)
over and over!
myscr.mainloop()
这是关键点! 您无法调用Python文件根目录中的代码! mainloop
函数不知道返回的位置或从哪里开始。电话会丢失,你的程序会冻结。游戏画面无法更新,因为没有任何东西告诉它应该如何更新。
结论:你必须拥有游戏循环的功能!