调试简单的乒乓游戏?

0 投票
2 回答
1376 浏览
提问于 2025-04-16 20:17

我正在用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

只需在slab类中放入这个更新方法:

def update(self):
    self.x=games.mouse.x
    self.y=games.mouse.y

这个方法会每隔五十分之一秒自动运行一次(这就是你的更新速度)。现在,你只需把slab放在鼠标的位置,然后就让它待在那里。

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())。然后,下次屏幕更新时,你应该检查是否有变化(让对象失效,标记为脏),如果它们是脏的,就更新它们在屏幕上的部分,然后再标记为干净。

撰写回答