Python 调试简单的乒乓球游戏?
我一直在使用Python的Pygame as run through Livewires编程一个简单的乒乓球游戏。我已经发布了一个问题和一个类似的示例游戏代码,它运行得很好,因为不是这里的每个人都精通livewires和/或pygame 下面是有bug的代码。发生的情况是窗口打开,“球”对象工作正常并落在屏幕上(它应该是不完整的),而“板”对象被粘在鼠标最初所在的位置。这是:Python 调试简单的乒乓球游戏?,python,debugging,pygame,livewires,Python,Debugging,Pygame,Livewires,我一直在使用Python的Pygame as run through Livewires编程一个简单的乒乓球游戏。我已经发布了一个问题和一个类似的示例游戏代码,它运行得很好,因为不是这里的每个人都精通livewires和/或pygame 下面是有bug的代码。发生的情况是窗口打开,“球”对象工作正常并落在屏幕上(它应该是不完整的),而“板”对象被粘在鼠标最初所在的位置。这是: from livewires import games, color games.init (screen_width
from livewires import games, color
games.init (screen_width = 640, screen_height = 480, fps = 50)
class Ball (games.Sprite):
def iMove (self):
self.dx = -self.dx
self.dy = -self.dy
class Slab (games.Sprite):
def mouse_moves (self):
self.x = games.mouse.x
self.y = games.mouse.y
self.iCollide()
def iCollide (self):
for Ball in overlapping_sprites:
Ball.iMove()
def main():
#Backgrounds
pingpongbackground = games.load_image ("pingpongbackground.jpg", transparent = False)
games.screen.background = pingpongbackground
#Ball: Initializing object and setting speed.
ballimage = games.load_image ("pingpongball.jpg")
theball = Ball (image = ballimage,
x = 320,
y = 240,
dx = 2,
dy = 2)
games.screen.add(theball)
#Paddle: Initializing ping pong object and setting initial poisition to the initial mouse position
slabimage = games.load_image ("pingpongpaddle.jpg")
theslab = Slab (image = slabimage,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(theslab)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main ()
下面是一段类似的功能代码:
# Slippery Pizza Program
# Demonstrates testing for sprite collisions
from livewires import games
import random
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 position. """
self.x = games.mouse.x
self.y = games.mouse.y
self.check_collide()
def check_collide(self):
""" Check for collision with pizza. """
for pizza in self.overlapping_sprites:
pizza.handle_collide()
class Pizza(games.Sprite):
"""" A slippery pizza. """
def handle_collide(self):
""" Move to a random screen location. """
self.x = random.randrange(games.screen.width)
self.y = random.randrange(games.screen.height)
def main():
wall_image = games.load_image("wall.jpg", transparent = False)
games.screen.background = wall_image
pizza_image = games.load_image("pizza.bmp")
pizza_x = random.randrange(games.screen.width)
pizza_y = random.randrange(games.screen.height)
the_pizza = Pizza(image = pizza_image, x = pizza_x, y = pizza_y)
games.screen.add(the_pizza)
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()
如果您有任何见解,我们将不胜感激 我不知道您的框架,但要防止板“卡住”,您需要在移动鼠标时更新其位置 在这里您可以初始化它:
theslab = Slab (image = slabimage,
x = games.mouse.x,
y = games.mouse.y)
然后在这里将其添加到游戏中:
games.screen.add(theslab)
游戏可能会在鼠标移动时调用此函数:
def mouse_moves (self):
self.x = games.mouse.x
self.y = games.mouse.y
self.iCollide()
但这不是没有发生,就是屏幕没有更新
因此,通过这样做,您应该发现:
def mouse_moves (self):
print "mouse_moves: ", str(games.mouse.x), str(games.mouse.y)
self.x = games.mouse.x
self.y = games.mouse.y
self.iCollide()
如果在鼠标移动时看到print语句的输出,则可能不是在更新屏幕,您需要检查框架文档。但我认为情况并非如此。当鼠标移动时,我认为你没有更新游戏。我可以想象框架中有某种onMouseMove类型的事件,您需要将其挂接到其中,以便在鼠标移动时更新游戏状态(即调用mouse_moves())。然后,下次更新屏幕时,您应该检查更改(使对象无效,将其标记为脏对象),如果对象脏了,则更新其屏幕部分,然后再次将其标记为干净。只需将此更新方法放在slab类中:
def update(self):
self.x=games.mouse.x
self.y=games.mouse.y
它将每五十分之一秒自动运行一次(您的更新速度)。当前,您只需在鼠标位置启动板,然后将其留在那里。为什么要使用不同的方法名称?你需要正确的名称,否则你的函数将不会被调用。我将重命名、检查并发回。是的,我在每个类中需要一个前导的更新(self)方法来抵消其他方法,还有其他一些小错误,但我修复了大部分。谢谢,我将再次查看文档。对不起,我第一次问的时候忘了选择这个作为最佳。就这么做了。再次感谢。@Louis93:谢谢。请随便问我任何问题。