0

我一直在使用 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()

任何见解都将不胜感激!

4

2 回答 2

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

如果您在鼠标移动时看到 print 语句的输出,那么您可能没有更新屏幕,您需要检查框架文档。但我认为情况并非如此。我认为当鼠标移动时你没有更新游戏。我会想象框架有某种 onMouseMove 类型的事件,您需要挂钩,以允许您在鼠标移动发生时更新游戏状态(即调用 mouse_moves())。然后,下次屏幕更新时,您应该检查更改(使对象无效,将它们标记为脏),如果它们是脏的,则更新它们的屏幕部分,然后再次将它们标记为干净。

于 2011-06-25T22:12:01.520 回答
0

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

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

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

于 2018-03-05T14:44:00.907 回答