Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame,如何使用鼠标按钮使圆圈消失和出现?_Python_Pygame - Fatal编程技术网

Python Pygame,如何使用鼠标按钮使圆圈消失和出现?

Python Pygame,如何使用鼠标按钮使圆圈消失和出现?,python,pygame,Python,Pygame,我刚开始使用Pygame,对MOUSEBUTTONDOWN和Pygame.draw.circle()有一个问题 单击屏幕内部的62.5x62.5正方形时,我希望它显示一个绿色圆圈,并且 再次单击它,我希望它消失。我不知道该怎么做,在我的代码中,我必须点击正方形,让圆圈在瞬间出现,然后它又消失了。我也尝试过使用while循环,但无法使其工作 import pygame pygame.init() screen = pygame.display.set_mode((800, 600)) runn

我刚开始使用Pygame,对MOUSEBUTTONDOWN和Pygame.draw.circle()有一个问题 单击屏幕内部的62.5x62.5正方形时,我希望它显示一个绿色圆圈,并且 再次单击它,我希望它消失。我不知道该怎么做,在我的代码中,我必须点击正方形,让圆圈在瞬间出现,然后它又消失了。我也尝试过使用while循环,但无法使其工作

import pygame
pygame.init()

screen = pygame.display.set_mode((800, 600))

running = True
while running:
    screen.fill((0, 0, 0))
    mouse_pos = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False          
        if event.type == pygame.MOUSEBUTTONDOWN and  mouse_pos[0] >= 150 and  mouse_pos[0] <= 212.5 \
            and  mouse_pos[1] >= 425 and  mouse_pos[1] <= 487.5: 
            pygame.draw.circle(screen, (152, 251, 152), (181.25, 393.75), 20)
            

    pygame.display.update()
导入pygame
pygame.init()
screen=pygame.display.set_模式((800600))
运行=真
运行时:
屏幕填充((0,0,0))
mouse\u pos=pygame.mouse.get\u pos()
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误

如果event.type==pygame.MOUSEBUTTONDOWN,mouse_pos[0]>=150,mouse_pos[0]=425,mouse_pos[1]则需要在事件循环中而不是在事件循环中绘制圆。添加一个布尔变量
draw_circle
,并在单击鼠标时切换变量的状态。
不能以浮点精度绘制。像素和鼠标位置具有整数单位。
使用对象并单击测试。鼠标单击的位置存储在对象的
pos
属性中:

导入pygame
pygame.init()
screen=pygame.display.set_模式((800600))
画圆=假
test_rect=pygame.rect(0,0,40,40)
测试中心=(181393)
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果event.type==pygame.MOUSEBUTTONDOWN:
如果测试直接碰撞点(事件位置):
画圆=不画圆
屏幕填充((0,0,0))
pygame.draw.rect(screen,(127127127),test_rect,1)#用于调试
如果画圆:
pygame.draw.circle(屏幕,(152251152),(181393),20)
pygame.display.update()