调试简单的Ping-Pong游戏?

时间:2011-06-25 21:52:34

标签: python debugging pygame livewires

我一直在编写一个简单的乒乓球游戏,使用Python的Pygame作为Livewires运行。我已经发布了问题和一个类似的示例游戏代码,运行得非常好,因为并非每个人都会精通livewires和/或pygame。

这是有错误的代码。发生的事情是窗口打开,“Ball”对象运行良好并且屏幕掉落(因为它应该是,它不完整),并且“Slab”对象会卡在鼠标最初的任何位置。这是:

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()

任何见解都将非常感谢!

2 个答案:

答案 0 :(得分:1)

我不知道您的框架,但为了防止“卡住”,您需要在移动鼠标时更新其位置。

在这里初始化它:

 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()

如果您看到当鼠标移动时发生打印语句的输出,您可能不会更新屏幕,您需要检查框架文档。但我认为不是这样的。我认为当鼠标移动时你不会更新游戏。我认为框架有一些你需要挂钩的onMouseMove类型事件,允许你在鼠标移动时更新游戏状态(即调用mouse_moves())。然后,下次更新屏幕时,您应该检查更改(使对象无效,将它们标记为脏),如果它们是脏的,请更新它们的部分屏幕,然后再将它们标记为干净。

答案 1 :(得分:0)

只需将此更新方法放在slab类中:

var newList = xList.Except(yList);
var new2List = xList.Intersect(yList);

每五十分之一秒(您的更新速度)会自动运行一次。目前,您只需在鼠标位置启动平板,然后将其留在那里。