Pygame以交互方式绘制

时间:2014-12-25 08:13:20

标签: python pygame mouse draw interactive

嗨,我在实现自己的想法时遇到了一些麻烦。我首先想用两次鼠标点击绘制矩形,但它没有正常工作,所以我把它缩小到这个:用一个鼠标点击绘制固定大小的矩形。 然而它仍然没有工作/...

import pygame
windowSize = (500,500)
white = (255,255,255)
black = (0,0,0)

pygame.init()
screen = pygame.display.set_mode(windowSize)
running = 1

while running:
    screen.fill(white)
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            running = 0

问题在哪里

        elif event.type == pygame.MOUSEBUTTONDOWN:
            rect = pygame.Rect(event.dict["pos"],(30,50))
            pygame.draw.rect(screen,black,rect,1)


    pygame.display.flip()

我知道我的代码可能存在很多概念错误......请帮忙!

1 个答案:

答案 0 :(得分:1)

每个刻度都在整个屏幕上填充白色。因此,在您实际绘制屏幕后,在下一个刻度上再次变为空白。只需将screen.fill(white)移出主循环:

import pygame
windowSize = (500,500)
white = (255,255,255)
black = (0,0,0)

pygame.init()
screen = pygame.display.set_mode(windowSize)
running = 1

screen.fill(white)
while running:
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            rect = pygame.Rect(event.dict["pos"],(30,50))
            pygame.draw.rect(screen,black,rect,1)
    pygame.display.flip()