一个简单的问题:为什么第一个代码可以工作,但看似相同的第二个代码在 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()