为什么python画不了图-python-为什么pygame不画一个圆?

我对pygame几乎一无所知,所以我无能为力了……但是我认为您在做的事总是使自己陷入困境.试试看:

pygame.init()

screen = pygame.display.set_mode((640, 480))

screen.fill((0,0,0))

while True:

for event in pygame.event.get():

if event.type == QUIT:

pygame.quit()

sys.exit()

if event.type == MOUSEBUTTONDOWN:

# draw background first (however)

screen.fill((0,0,0))

# draw your other layers (mouse image)

# draw the circle

color = (255,255,255)

posx,posy = pygame.mouse.get_pos()

pygame.draw.circle(screen, color, (posx,posy), 50)

pygame.display.update()

基本上,您的示例中发生的事情是,当您按下鼠标事件进行绘制时,您将再次在背景上进行绘制.我不确定mousec是什么,但是每次都会在背景上绘制出来.因此,您将永远不会看到鼠标点击绘制的圆

我的示例首先填充背景,然后当鼠标向下移动时,它将再次填充背景以绘制先前的状态,然后绘制圆.

使用您的确切示例,另一种方法是仅在检查事件时记录鼠标的位置,并将圆的绘制推迟到图层之后.您将“记住”鼠标的最后一个位置,以便在每个循环中不断重绘该圆圈:

last_mouse_pos = None

while True:

for event in pygame.event.get():

if event.type == QUIT:

pygame.quit()

sys.exit

if event.type == MOUSEBUTTONDOWN:

last_mouse_pos = pygame.mouse.get_pos()

elif event.type == KEYDOWN and event.unicode == 'c':

# clear the circle when pressing the 'c' key

last_mouse_pos = None

screen.blit(background,(0,0))

x,y = pygame.mouse.get_pos()

x -= mousec.get_width()/2

y -= mousec.get_height()/2

screen.blit(mousec, (x,y))

if last_mouse_pos:

color = (100,100,100)

posx,posy = last_mouse_pos

pygame.draw.circle(screen, color, (posx,posy), 50)

pygame.display.update()

这种方法的区别在于,您总是在每个循环中绘制所有内容,而不是仅响应事件的变化.

更新资料

为了回应您在评论中的问题,修改第二个示例以保留所有鼠标单击的一种方法是将它们保存在一个集中,并在每次绘制时都将其拉回.

mouse_clicks = set()

while True:

for event in pygame.event.get():

if event.type == QUIT:

pygame.quit()

sys.exit

if event.type == MOUSEBUTTONDOWN:

mouse_clicks.add(pygame.mouse.get_pos())

elif event.type == KEYDOWN and event.unicode == 'c':

# clear the circle when pressing the 'c' key

mouse_clicks.clear()

screen.blit(background,(0,0))

x,y = pygame.mouse.get_pos()

x -= mousec.get_width()/2

y -= mousec.get_height()/2

screen.blit(mousec, (x,y))

for pos in mouse_clicks:

color = (100,100,100)

posx,posy = pos

pygame.draw.circle(screen, color, (posx,posy), 50)

pygame.display.update()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值