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

时间:2018-11-03 14:43:29

标签: 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()

1 个答案:

答案 0 :(得分:1)

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,而没有任何工作。我相信您这样做是因为您将圆形绘图调用放置在其中,但您实际上应该将其移入循环中。