在pygame Python上删除图形而不覆盖以前的图形

2024-10-04 01:33:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个绘画应用程序,我从之前打开的帖子中获得了帮助:

The previous question I posted关于如何实现ctrl Z

但是,现在我遇到了另一个问题,当我按ctrl-Z时,它确实会删除我绘制的内容(最后一个,因为它像堆栈一样工作),但是如果你一个接一个地绘制两个形状,然后按ctrl-Z,它会删除上一个形状的某些部分,例如:

然后按ctrl-Z键,我会看到:

enter image description here

如何修复它,使其不会在那里留下空白

我的代码:

from collections import deque
from DrawEntity import DrawEntity

import pygame as pg


WHITE = (255, 255, 255)
BLACK =  (0, 0, 0)
SCREEN_SIZE = (1100, 800)
DrawEnt = deque()
brush = 3


def draw(window):
    if len(DrawEnt) > 0:
        d_Ent = DrawEnt[-1]

        mouseX, mouseY = pg.mouse.get_pos()
        click = pg.mouse.get_pressed()
        if click[0]:
            circle = pg.draw.circle(window, BLACK, (mouseX, mouseY), brush)
            d_Ent.add(circle)





def main():
    global brush
    running = True
    window = pg.display.set_mode(SCREEN_SIZE)
    window.fill(WHITE)


    while running:
        clock.tick(1800)

        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False

            if event.type == pg.MOUSEBUTTONDOWN:
                if event.button == 1:
                    DrawEnt.append(DrawEntity())
                elif event.button == 4:
                    brush += 1 if brush < 30 else  0
                elif event.button == 5:
                    brush -= 1 if brush > 0 else 0



            if event.type == pg.KEYDOWN:

                if event.key == pg.K_z and pg.key.get_mods() & pg.KMOD_LCTRL:


                    if len(DrawEnt) > 0:
                        ent = DrawEnt.pop()
                        for entity in ent.entity:
                                pg.draw.circle(window, WHITE, entity.center , entity.width)




            draw(window)

        pg.display.flip()
    #end main loop
    pg.quit()

if __name__ == '__main__':

    pg.init()
    clock = pg.time.Clock()
    main()

paurentity.py:

class DrawEntity:
def __init__(self):
    self.entity = []

def add(self, toAdd):
    self.entity.append(toAdd)

def remove(self):
    self.entity = []

def __str__(self):
    return ' '.join(map(str, self.entity))

Tags: selfeventgetifmaindefwindowentity
1条回答
网友
1楼 · 发布于 2024-10-04 01:33:05

不能撤消图形。您实际要做的是在当前图形的顶部绘制白色圆圈

此外,^{}的返回值不是某种圆对象,而是一个^{}对象,它包围了圆

绘制对象时,必须在draw中存储颜色、位置和笔刷(d_Ent.add((BLACK, (mouseX, mouseY), brush))):

def draw(window):
    if len(DrawEnt) > 0:
        d_Ent = DrawEnt[-1]

        mouseX, mouseY = pg.mouse.get_pos()
        click = pg.mouse.get_pressed()
        if click[0]:
            circle = pg.draw.circle(window, BLACK, (mouseX, mouseY), brush)
            d_Ent.add((BLACK, (mouseX, mouseY), brush))

如果要“撤消”图形的某些部分,则必须清除整个显示并重新绘制所有剩余对象:

def main():
    # [...]

    while running:
        # [...]

        for event in pg.event.get():
            # [...]

            if event.type == pg.KEYDOWN:

                if event.key == pg.K_z and pg.key.get_mods() & pg.KMOD_LCTRL:

                    if len(DrawEnt) > 0:
                        ent = DrawEnt.pop()

                        window.fill((255, 255, 255))         
                        for drawent in DrawEnt:
                            for entity in drawent.entity:
                                pg.draw.circle(window, *entity)

相关问题 更多 >