【问题标题】:pygame how to check mouse coordinates [duplicate]pygame如何检查鼠标坐标[重复]
【发布时间】:2014-05-24 03:48:32
【问题描述】:

我正在使用 python 和 pygame 制作一个多项选择测验游戏。我有圆圈,当你点击圆圈时,它会改变宽度并使圆圈变满。但我不知道可以做到这一点的python函数

到目前为止,这是我的代码:

    # Controls the width of the circles
    width_1 = 2
    width_2 = 2
    width_3 = 2
    width_4 = 2

    # Circles
    pygame.draw.circle(screen, BLACK, [250, 230], 7, width_1)
    pygame.draw.circle(screen, BLACK, [250, 260], 7, width_2)
    pygame.draw.circle(screen, BLACK, [250, 290], 7, width_3)
    pygame.draw.circle(screen, BLACK, [250, 320], 7, width_4)

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:

我想要一些东西,如果鼠标悬停在圆圈上并且 mousebuttondown AND event.button == 1 然后将宽度更改为 0(填写)

提前致谢!

附:这不是我的全部代码

【问题讨论】:

    标签: python position pygame mouse


    【解决方案1】:

    你可以这样做 x, y = pygame.mouse.get_pos() 然后 print(x, y)

    假设我们想制作一些从一个位置到另一个位置的飞行积木。 我们可以这样做

    import pygame
    from pygame import *
    import sys, random, math, fractions
    
    pygame.init()
    Screen_Width = 800
    Screen_Height = 600
    
    Total_Display = pygame.display.set_mode((Screen_Width, Screen_Height))
    
    clock = pygame.time.Clock()
    
    class Blocks(pygame.sprite.Sprite):
        def __init__(self, width, height):
            super().__init__()
            all_sprite_list.add(self)
            block_list.add(self)
            self.image = pygame.Surface((32,32))
            self.rect = self.image.get_rect()
            self.change_y = 0
            self.change_x = 0
    
        def blockMove (self, cursor_pos_x, cursor_pos_y, player_pos_x, player_pos_y):
    
            block_vec_x = cursor_pos_x - player_pos_x
            block_vec_y = cursor_pos_y - player_pos_y
            vec_length = math.sqrt(block_vec_x ** 2 + block_vec_y ** 2)
            block_vec_y = (block_vec_y / vec_length) * 5
            block_vec_x = (block_vec_x / vec_length) * 5
            self.change_y += block_vec_y
            self.change_x += block_vec_x
    
        def update(self):
    
            self.rect.y += self.change_y
            self.rect.x += self.change_x
    

    现在制作一个简单的播放器类,只是为了将块发射到鼠标位置,然后将它放在窗口的中间!

    class Player(pygame.sprite.Sprite):
        def __init__(self):
            player_list.add(self)
            self.rect = Rect(400,300,16,16)
            self.image = pygame.surface((16,16))
    
    all_sprite_list = pygame.sprite.Group()
    player_list = pygame.sprite.Group()
    block_list = pygame.sprite.Group()
    
    block = Blocks(16,16)
    player = Player()
    
    running = True
    while running:
        clock.tick(60)
    
        for e in pygame.event.get()
            if e == pygame.Quit:
                running = False
            if e.type == pygame.KEYDOWN and e.type == pygame.K_ESCAPE:
                running = False
    

    这里是我们设置鼠标位置的地方 Mouse_X 和 Mouse_Y = mouse.get_pos() 将输出 (x, y) 或 Mouse_X = mouse pos x 和 Mouse_Y = mouse pos y。您也可以通过添加 print(Mouse_x & " " & Mouse_y) 来输出鼠标的位置

        Mouse_x, Mouse_y = pygame.mouse.get_pos()
        key = pygame.key.get_pressed()
        if key[pygame.K_SPACE]:
            block = Blocks(16,16)
            block.blockMove(Mouse_x, Mouse_y, player.rect.x, player.rect.y)
            block.rect.x = player.rect.x
            block.rect.y = player.rect.y
        block.update()
        Total_Display.fill((255,0,0))
        for sprite in all_sprite_list:
            pygame.draw.rect(Total_Display,(0,0,0), sprite)
        for blocks in block_list:
            pygame.draw.rect(Total_Display, (0,0,0), block)
        
        pygame.display.flip()
    

    我可能有点过火了,但希望这对那里的人有所帮助! 这是整个事情,只是为了确保我没有犯错

    import pygame
    from pygame import *
    import sys, random, math, fractions
    
    pygame.init()
    Screen_Width = 800
    Screen_Height = 600
    
    Total_Display = pygame.display.set_mode((Screen_Width, Screen_Height))
    
    clock = pygame.time.Clock()
    
    class Blocks(pygame.sprite.Sprite):
        def __init__(self, width, height):
            super().__init__()
            all_sprite_list.add(self)
            block_list.add(self)
            self.image = pygame.Surface((32,32))
            self.rect = self.image.get_rect()
            self.change_y = 0
            self.change_x = 0
    
        def blockMove (self, cursor_pos_x, cursor_pos_y, player_pos_x, player_pos_y):
    
            block_vec_x = cursor_pos_x - player_pos_x
            block_vec_y = cursor_pos_y - player_pos_y
            vec_length = math.sqrt(block_vec_x ** 2 + block_vec_y ** 2)
            block_vec_y = (block_vec_y / vec_length) * 5
            block_vec_x = (block_vec_x / vec_length) * 5
            self.change_y += block_vec_y
            self.change_x += block_vec_x
    
        def update(self):
    
            self.rect.y += self.change_y
            self.rect.x += self.change_x
    
    class Player(pygame.sprite.Sprite):
        def __init__(self):
            super().__init__()
            player_list.add(self)
            all_sprite_list.add(self)
            self.rect = Rect(400,300,16,16)
            self.image = pygame.Surface((16,16))
    
    
    all_sprite_list = pygame.sprite.Group()
    player_list = pygame.sprite.GroupSingle()
    block_list = pygame.sprite.Group()
    
    block = Blocks(16,16)
    player = Player()
    
    running = True
    while running:
        clock.tick(60)
    
    
        for e in pygame.event.get():
            if e == pygame.QUIT:
                Running = False
            if e.type == pygame.KEYDOWN and e.type == pygame.K_ESCAPE:
                running = False
    
        Mouse_x, Mouse_y = pygame.mouse.get_pos()
        key = pygame.key.get_pressed()
        if key[pygame.K_SPACE]:
            block = Blocks(16,16)
            block.update()
            block.blockMove(Mouse_x, Mouse_y, player.rect.x, player.rect.y)
            block.rect.x = player.rect.x
            block.rect.y = player.rect.y
        block.update()
        Total_Display.fill((255,0,0))
        for sprite in all_sprite_list:
            pygame.draw.rect(Total_Display,(0,0,0), sprite)
        for blocks in block_list:
            pygame.draw.rect(Total_Display, (0,0,0), block)
        
        pygame.display.flip()
    
    
            
    
                
    

    【讨论】:

      【解决方案2】:

      您可以使用pygame.mouse.get_pos() (docs)

      pygame.mouse.get_pos() 获取鼠标光标位置 get_pos() -> (x, y)

      返回鼠标光标的 X 和 Y 位置。位置是 相对于显示屏的左上角。光标位置 可以位于显示窗口之外,但总是 限制在屏幕上。

      【讨论】:

      • 说得有道理,但是我如何检查具体的 x 和 y 位置。
      • 如何检查 if 语句中的位置?
      【解决方案3】:

      首先,您需要鼠标坐标。你可以这样做:

      mousex, mousey = pygame.mouse.get_pos()
      

      然后你需要一个函数来检查鼠标是否在圆圈内。为此,您需要一个用于圆圈的碰撞箱。圆形碰撞箱很难制作,因此您可以通过以下操作使用方形碰撞箱:

      def check_pressed ():
          mousex, mousey = pygame.mouse.get_pos()
          click = pygame.mouse.get_pressed()
      
          if click != (0, 0, 0):
              if mousex >= buttonx and mousex <= buttonx + button_width and 
              mousey >= buttony and mousey <= buttony + button_height:
                  #run function to make button bigger and run a clicked event
      

      将此 check_pressed 函数添加到您的主循环中。 希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-06
        相关资源
        最近更新 更多