问题描述
我试图记住以前的图形,以实现Ctrl-Z(撤消)和(重做).为此,我使用了集合中的堆栈(双端队列),我还做的是创建一个名为DrawEntity的对象,该对象具有矩形列表的属性(pygame类),因为我不想保存我绘制的像素,但是只有一个笔触",而不是使他成色的组件(小笔弯会产生笔触),但是,我不知道如何将其插入到主DrawEntity列表中,从而将这些实体保存在每个笔划而不是每个矩形的队列.这是我到目前为止所拥有的:
I am trying to remember previous drawings to implement Ctrl-Z (Undo) and (Redo). For this I am using a stack (deque) from collections and what I also did was to create an Object called DrawEntity, which has an attribute of a list of rectangles (pygame class) because I don't want to save every pixel that I draw, but only one 'brushstroke' and not the components that make him (little ractangles make a brushstroke) however, I don't know how to insert to the main DrawEntity list which will save those entities in the queue for each brushstroke and not for each rectangle. This is what I have so far:
main.py:
import pygame as pg
from collections import deque
from DrawEntity import DrawEntity
DrawEnt = deque()
def draw(window):
d_Ent = DrawEntity()
mouseX, mouseY = pg.mouse.get_pos()
click = pg.mouse.get_pressed()
if click[0]:
rect = pg.draw.rect(window, (0, 0, 0), pg.Rect(mouseX, mouseY, 3, 3), 3)
d_Ent.add(rect)
if d_Ent.entity:
print("new Entity")
print(d_Ent)
DrawEnt.append(d_Ent)
def main():
running = True
window = pg.display.set_mode((640, 480))
window.fill((255, 255, 255))
while running:
clock.tick(3200)
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_z and pg.key.get_mods() & pg.KMOD_LCTRL:
print(len(DrawEnt))
draw(window)
pg.display.flip()
#end main loop
pg.quit()
if __name__ == '__main__':
pg.init()
clock = pg.time.Clock()
main()
和DrawEntity.py:
And DrawEntity.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))
基本上,我想进入一个while循环并停留在那里,我一直单击,以便将所有矩形收集到列表中,然后将其附加到主要实体列表中,但是这不会进入while循环,每个每次绘制时,它会将每个新实体附加一个矩形而不是一系列矩形.(如果我尝试使用while循环游戏会崩溃)
因此:
Basically, I want to enter a while loop and stay there WHILE I keep clicking, in order to gather all the rectangles into the list and then append it into the main entity list, however this does not enter a while loop and each time I draw it appends each new entity with a single rectangle and not a series of rectangles. (If I try to use while loop the game crashes)
So:
-1单击时,收集到目前为止我绘制的所有矩形
-1 While clicking gather all the rectangles I've drawn so far
1.1如果我停止单击,则将带有矩形列表的新d_Ent附加到主要实体列表(main.py中第5行的DrawEnt)
1.1 If I stopped clicking then append the new d_Ent with a list of rectangles to the main entity list (DrawEnt on line 5 in main.py)
- 继续执行程序
- 如果我再次单击,请转到1
推荐答案
每次单击鼠标按钮时,都必须创建一个新的 DrawEntity
对象:
Every time whe the mouse button is clicked, then you have to create a new DrawEntity
object:
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
DrawEnt.append(DrawEntity())
在 draw
中,您必须将Rect附加到 deque
( DrawEnt
)中的最后一个 DrawEntity
上:
In draw
you have to append a Rect to the last DrawEntity
in the deque
(DrawEnt
):
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]:
rect = pg.draw.rect(window, (0, 0, 0), pg.Rect(mouseX, mouseY, 3, 3), 3)
d_Ent.add(rect)
在-上,从 DrawEnt
( pop
)中删除最后一个元素,清除显示并绘制再次所有剩余的元素:
On - remove the last element from DrawEnt
(pop
), clear the display and draw all remaining elements again:
if event.type == pg.KEYDOWN:
if event.key == pg.K_z and pg.key.get_mods() & pg.KMOD_LCTRL:
if len(DrawEnt) > 0:
DrawEnt.pop()
window.fill((255, 255, 255))
for entity in DrawEnt:
for r in entity.entity:
pg.draw.rect(window, (0, 0, 0), r, 3)
请参见示例:
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))
DrawEnt = deque()
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]:
rect = pg.draw.rect(window, (0, 0, 0), pg.Rect(mouseX, mouseY, 3, 3), 3)
d_Ent.add(rect)
def main():
running = True
window = pg.display.set_mode((640, 480))
window.fill((255, 255, 255))
while running:
clock.tick(3200)
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
d_Ent = DrawEntity()
DrawEnt.append(DrawEntity())
if event.type == pg.KEYDOWN:
if event.key == pg.K_z and pg.key.get_mods() & pg.KMOD_LCTRL:
if len(DrawEnt) > 0:
DrawEnt.pop()
window.fill((255, 255, 255))
for entity in DrawEnt:
for r in entity.entity:
pg.draw.rect(window, (0, 0, 0), r, 3)
draw(window)
pg.display.flip()
#end main loop
pg.quit()
if __name__ == '__main__':
pg.init()
clock = pg.time.Clock()
main()
这篇关于在Pygame中保存绘图的历史记录以实现Ctrl Z的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!