为了账号安全,请及时绑定邮箱和手机立即绑定

矩形未在屏幕上绘制

矩形未在屏幕上绘制

慕村9548890 2023-03-22 16:29:57
下面的代码是创建一个按钮,当光标悬停在它上面时它会改变颜色,同时结束程序,如果它被点击。结果不显示按钮,只显示背景。我在底部有执行后 pygame 窗口的图像。'''# initialisingimport pygame, syspygame.init()HEIGHT = 720WIDTH  = 720screen = pygame.display.set_mode((HEIGHT,WIDTH))# colour WHITE  = (255,255,255)BLACK  = (0,  0  ,0  )MAROON = (128,0  ,0  )RED    = (255,0  ,0  )x, y = pygame.mouse.get_pos()# rendering a text written in # this font # defining a font smallfont = pygame.font.SysFont('Corbel',35) text = smallfont.render('quit' , True , BLACK)         # superimposing the text onto our button screen.blit(text,(WIDTH/2 +50, HEIGHT/2)) # describing the processwhile True:    for ev in pygame.event.get():                if ev.type == pygame.QUIT:            pygame.quit()        #describing events        if ev.type == pygame.MOUSEBUTTONDOWN:                        if WIDTH/2 -70 <= x <= WIDTH/2 + 70 and HEIGHT/2 -70 <= x <= HEIGHT/2 + 70:                pygame.quit()                pygame.display.flip()            if  WIDTH/2 -70 <= x <= WIDTH/2 + 70 and HEIGHT/2 -70 <= x <= HEIGHT/2 + 70:        pygame.draw.rect(screen,RED,(100,100,500,500))     else:        pygame.draw.rect(screen,MAROON,(100,100,500,500))                      # filling screen with colour    screen.fill(WHITE)         # updates the frames of the game     pygame.display.update()         我试过 pygame.display.flip(),改变矩形的位置,但问题没有解决。我是 pygame 的初学者,因此才刚刚开始。
查看完整描述

1 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

您必须先绘制矩形screen.fill(WHITE)。注意pygame.Surface.fill用纯色填充整个表面。之前绘制的所有内容都被覆盖了。

# describing the process

while True:

    # [...]


    # filling screen with colour

    screen.fill(WHITE)


    if  WIDTH/2 -70 <= x <= WIDTH/2 + 70 and HEIGHT/2 -70 <= x <= HEIGHT/2 + 70:

        pygame.draw.rect(screen,RED,(100,100,500,500)) 

    else:

        pygame.draw.rect(screen,MAROON,(100,100,500,500))

    

    # updates the frames of the game 

    pygame.display.update() 


查看完整回答
反对 回复 2023-03-22
  • 1 回答
  • 0 关注
  • 131 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号