1

我正在制作 Pong 克隆以用于学习目的,并且需要在按下鼠标时让球从屏幕中间移动(当它经过桨时它被发送到那里)。我试过下面的代码,但它什么也没做,所以我可能不理解语法。请尽量保持简单,并解释一下,我宁愿没有 50 行代码(我想了解我在这里使用的所有内容)。我认为这是所有相关的代码,如果不是,对不起。谢谢。

def middle(self):
    """Restart the ball in the centre, waiting for mouse click. """
    # puts ball stationary in the middle of the screen
    self.x = games.screen.width/2
    self.y = games.screen.height/2
    self.dy = 0
    self.dx = 0

    # moves the ball if mouse is pressed
    if games.mouse.is_pressed(1):
        self.dx = -3
4

2 回答 2

0

根据该代码片段无法确切知道发生了什么,但看起来您使用了错误的函数来检测是否按下了鼠标按钮。

Screen.is_pressed来自游戏模块 wraps pygame.key.get_pressed,它只检测键盘键的状态,而不是鼠标按钮。您可能想要Screen.mouse_buttons包装的功能pygame.mouse.get_pressed。您可以在这样的循环中使用它(我假设您有一个games.Screen名为“screen”的实例):

left, middle, right = screen.mouse_buttons()
# value will be True if button is pressed
if left:
    self.dx = -3
于 2011-11-02T19:41:20.050 回答
0

我正在研究与初学者Python编码器相同的问题 - Games.py(修订版 1.7)包括is_pressed各种类中的几种方法,包括键盘和鼠标。

class Mouse(object):

#other stuff then 
def is_pressed(self, button_number):
    return pygame.mouse.get_pressed()[button_number] == 1

由于 pygame 是一个编译模块(我有 1.9.1)参考文档而不是源代码,我发现这里有一个 pygame.mouse.get_pressed(): 将获取鼠标按钮的状态

get_pressed() -> (button1, button2, button3)

所以我认为问题是在(你)我们的代码中使用这个而不是使用错误的函数.....

好的让这个工作 - 我的修复:

class myClass(games.Sprite):
    def update(self):
        if games.mouse.is_pressed(0)==1:
            self.x=games.mouse.x
            self.y=games.mouse.y

调用 in Main() 会导致选定的精灵移动到鼠标位置。高温高压

于 2015-10-10T11:35:42.723 回答