pygame中的python单击事件范围问题

pygame中的python单击事件范围问题,python,scope,mouseevent,pygame,Python,Scope,Mouseevent,Pygame,我使用python pygame库创建了一个GameObject类,它在屏幕上绘制了一个矩形。 我想集成一个事件处理程序,它允许我画一个矩形,但是游戏对象的点击事件没有注册。 下面是该类的几段代码: def on_事件(self,event): def on_-draw(自,表面): 我是否需要将rect变成一个图像,以便注册鼠标事件,或者是否有其他方法可以拖动此框 我是否需要将rect变成一个图像,以便注册鼠标事件,或者是否有其他方法可以拖动此框 不需要图像。解决方案: 注: 您已经使用事件获

我使用python pygame库创建了一个GameObject类,它在屏幕上绘制了一个矩形。 我想集成一个事件处理程序,它允许我画一个矩形,但是游戏对象的点击事件没有注册。 下面是该类的几段代码:

def on_事件(self,event):

def on_-draw(自,表面):

我是否需要将rect变成一个图像,以便注册鼠标事件,或者是否有其他方法可以拖动此框

我是否需要将rect变成一个图像,以便注册鼠标事件,或者是否有其他方法可以拖动此框

不需要图像。解决方案:

注:

  • 您已经使用事件获取MOUSEMOTION,因此请使用事件的数据:.pos和.button

  • 使用类存储为单个变量:

    self.color\u bg=颜色(“蓝色”) self.color_fg=颜色(20,20,20) 打印self.color\u bg.r、self.color\u bg.g、self.color\u bg.b

  • 注意:为了便于阅读,我将一些代码保留得稍微详细一些,例如:您可以:

    # it uses
    x,y = event.pos
    if b.rect.collidepoint( (x,y) ):
    # you could do (the same thing)
    if b.rect.collidepoint( event.pos* ):
    
    解决方案:

    import pygame
    from pygame.locals import *
    from random import randint
    
    class Box(object):
        """simple box, draws rect and Color"""
        def __init__(self, rect, color):
            self.rect = rect
            self.color = color
        def draw(self, surface):
            pygame.draw.rect(surface, self.color, self.rect)
    
        class Game(object):
        def __init__(self):
            # random loc and size boxes
            # target = box to move
            self.target = None
    
            for i in range(5):
                x,y = randint(0, self.width), randint(0, self.height)
                size = randint(20, 200)
    
                r = Rect(0,0,0,0)
                # used 0 because: will be using propery, but have to create it first
                r.size = (size, size)
                r.center = (x, y)
                self.boxes.append( Box(r, Color("red") ))
    
        def draw(self):
            for b in self.boxes: b.draw()
    
        def on_event(self, event):
            # see: http://www.pygame.org/docs/ref/event.html
    
            if event.type == MOUSEBUTTONDOWN and event.button = 1:
                # LMB down = target if collide
                x,y = event.pos
                for b in self.boxes:
                    if b.rect.collidepoint( (x,y) ): self.target = b
    
            elif event.type == MOUSEBUTTONUP and event.button = 1:
                # LMB up : untarget
                self.target = None
    
            elif event.type == MOUSEMOTION:
                x,y = event.pos
                # is a valid Box selected?
                if self.target is not None:
                    self.target.rect.center = (x,y)
    
    我是否需要将rect变成一个图像,以便注册鼠标事件,或者是否有其他方法可以拖动此框

    不需要图像。解决方案:

    注:

  • 您已经使用事件获取MOUSEMOTION,因此请使用事件的数据:.pos和.button

  • 使用类存储为单个变量:

    self.color\u bg=颜色(“蓝色”) self.color_fg=颜色(20,20,20) 打印self.color\u bg.r、self.color\u bg.g、self.color\u bg.b

  • 注意:为了便于阅读,我将一些代码保留得稍微详细一些,例如:您可以:

    # it uses
    x,y = event.pos
    if b.rect.collidepoint( (x,y) ):
    # you could do (the same thing)
    if b.rect.collidepoint( event.pos* ):
    
    解决方案:

    import pygame
    from pygame.locals import *
    from random import randint
    
    class Box(object):
        """simple box, draws rect and Color"""
        def __init__(self, rect, color):
            self.rect = rect
            self.color = color
        def draw(self, surface):
            pygame.draw.rect(surface, self.color, self.rect)
    
        class Game(object):
        def __init__(self):
            # random loc and size boxes
            # target = box to move
            self.target = None
    
            for i in range(5):
                x,y = randint(0, self.width), randint(0, self.height)
                size = randint(20, 200)
    
                r = Rect(0,0,0,0)
                # used 0 because: will be using propery, but have to create it first
                r.size = (size, size)
                r.center = (x, y)
                self.boxes.append( Box(r, Color("red") ))
    
        def draw(self):
            for b in self.boxes: b.draw()
    
        def on_event(self, event):
            # see: http://www.pygame.org/docs/ref/event.html
    
            if event.type == MOUSEBUTTONDOWN and event.button = 1:
                # LMB down = target if collide
                x,y = event.pos
                for b in self.boxes:
                    if b.rect.collidepoint( (x,y) ): self.target = b
    
            elif event.type == MOUSEBUTTONUP and event.button = 1:
                # LMB up : untarget
                self.target = None
    
            elif event.type == MOUSEMOTION:
                x,y = event.pos
                # is a valid Box selected?
                if self.target is not None:
                    self.target.rect.center = (x,y)
    

    “单击事件未注册”-如中所示,您的on_事件方法从未调用过?“单击事件未注册”-如中所示,您的on_事件方法从未调用过?