python - 为什么我不能在 Python 上单击并拖动圆圈?

标签 python python-3.x pygame

我想单击这两个圆球并将它们拖动到鼠标当前的鼠标位置,但它不起作用。

我只想用鼠标单击并拖动球。

未添加一些代码(导入pygame、绘制显示、颜色等)

   import pygame

window = pygame.display.set_mode((800,400))
clock = pygame.time.Clock()
##bg = pygame.image.load("bgpool.png")
##window.blit(bg,(0,0))


black = (0,0,0)
yellow = (255,255,0) 
class Circle:
    def __init__(self,x,y,color,radius):
        self.x = x
        self.y = y
        self.color = color
        self.radius = radius
        pygame.draw.circle(window, color,(x,y),radius)
def quitgame():
    pygame.quit()
    quit()

def loop():
    cikis = False
    while not cikis:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                cikis = True
                pygame.quit()
                quit()

        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        bas = pygame.MOUSEBUTTONDOWN
        window.fill((255,255,255))
        main_circle = Circle(75,175,black,10)
        aim_circle = Circle(375,175,yellow,10)

        if click[0] == 1:
            if mouse[0] >= main_circle.x and mouse[0] <= main_circle.x + main_circle.radius:
                if mouse[1] >= main_circle.y and mouse[1] <= main_circle.y + main_circle.radius:
                    if click[0] == 1:
                        main_circle.x == mouse[0]
                        main_circle.y == mouse[1]


    clock.tick(120)
    pygame.display.update()

loop()
pygame.quit()
quit()

最佳答案

虽然 click = pygame.mouse.get_pressed() 有效,但非常强烈建议使用 Pygame 的事件,就像您对 pygame.QUIT< 所做的那样

因为您没有提供太多代码,所以我添加了最少的代码以使代码可以运行,并且我对每个修改进行了注释:

import pygame

pygame.init()
window = pygame.display.set_mode((700, 500))

class Circle:
    def __init__(self, x, y, color, radius):
        # Changed pos to contain both coordinates
        self.pos = (x, y)
        # Where the radius ends
        self.x_boundary = (x - radius, x + radius)
        self.y_boundary = (y - radius, y + radius)
        self.color = color
        self.radius = radius

    def recalc_boundary(self):
        # Recalculate the boundaries of the circle,
        # this is needed whenever the circle is moved
        self.x_boundary = (
            self.pos[0] - self.radius, self.pos[0] + self.radius
        )
        self.y_boundary = (
            self.pos[1] - self.radius, self.pos[1] + self.radius
        )

# Single instantiation of our circle
# didn't add aim_circle because it is irrelevant
# in my answer
main_circle = Circle(75, 175, (255, 0, 0), 10)

# Small lambda function that'll take care of
# checking whether or not a point x is
# within two boundaries.
# This replaces your
# `if mouse[0] >= main_circle.x and mouse[0]
# <= main_circle.x + main_circle.radius:`
# lines
within = lambda x, low, high: low <= x <= high

# Boolean that attests to whether or not the
# user is clicking on our circle
selected = False

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

        # Test for MOUSEBUTTONDOWN events
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Left mouse button
            if event.button == 1:
                pos = pygame.mouse.get_pos()
                # Check if the mouse is within
                # our circle's limits, to see if the
                # user is trying to click on it
                if (
                    within(pos[0], *main_circle.x_boundary)
                    and within(pos[1], *main_circle.y_boundary)
                ):
                    selected = True

        elif event.type == pygame.MOUSEBUTTONUP:
            # User released mouse buttons
            selected = False

    if selected:
        # Move to mouse position when selected,
        # the circle 'follows' the mouse
        main_circle.pos = pygame.mouse.get_pos()
        main_circle.recalc_boundary()

    window.fill((0, 0, 0))
    # Moved this draw call to outside
    # your class definition
    pygame.draw.circle(
        window, main_circle.color,
        main_circle.pos,
        main_circle.radius
    )

    pygame.display.update()

注意:每次游戏循环迭代都实例化一个新的 Circle 对象是一个坏主意,这会白白消耗大量 CPU。我相信您已经这样做了,因为您将圆绘制调用放在了其中,但您实际上应该将其移到循环中。

关于python - 为什么我不能在 Python 上单击并拖动圆圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53132386/

相关文章:

python - 动态更改 wx.ComboBox() 中的选项

python - 展平由整数和列表组成的列表

python - 碰撞Pygame后穿过墙壁

python - Seaborn 热图 : Customize A Label

python - 系统等待时,如何在pygame中播放声音?

python - PyGame 和 Unicode - 一个永无休止的故事

python - 我应该如何使用 scikit learn 对以下列表列表进行矢量化?

python - for 循环在上一个循环完成之前开始

python - 在python中按特定年份分组数据

python - 将 Pandas 数据帧转为具有多层的长格式