Pygame如何检查鼠标坐标

8

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

我希望有一个功能,使当鼠标悬停在圆圈上并且鼠标按键按下并且事件按钮为1时,将宽度更改为0(填写)。

提前感谢!

P.S. 这不是我所有的代码。

3个回答

10

你可以直接使用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 =鼠标位置x和Mouse_Y =鼠标位置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()


        

            

4
你可以使用pygame.mouse.get_pos() (文档)。

pygame.mouse.get_pos()获取鼠标光标位置,返回值为(x, y)。

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


这很有道理,但我该如何检查特定的x和y位置呢? - jason97931
我该如何在if语句中检查位置? - jason97931

2
首先,您需要获取鼠标的坐标。可以像这样实现:
```javascript ```
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。 希望这可以帮助你!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接