Прикреплённый файл «2013-12-13-greditor.py»
Загрузка 1 #!/usr/bin/env python
2 # coding: utf
3 '''
4 Примитивный графический редактор,
5 иллюстрирующий объектное планирование игрового пространства
6 '''
7
8 import pygame
9
10 pygame.init()
11 black = pygame.Color("black")
12 tan = pygame.Color("tan")
13 red = pygame.Color("tomato")
14 green = pygame.Color("limegreen")
15
16 def Editor(event):
17 '''Основной режим работы'''
18 global pos, Mainloop, figures, debug
19 if debug:
20 print event
21 if event.type == pygame.KEYDOWN:
22 if event.unicode == u'd':
23 figures.append(Dot())
24 Mainloop = FigureEditor
25 pygame.mouse.set_visible(False)
26 elif event.unicode == u'l':
27 figures.append(Line())
28 Mainloop = FigureEditor
29 pygame.mouse.set_visible(False)
30 elif event.key == pygame.K_F1:
31 debug = not debug
32 return None
33
34 def FigureEditor(event):
35 '''Режим ввода фигуры'''
36 global pos, Mainloop, figures
37 if event.type == pygame.MOUSEMOTION:
38 pos = event.pos
39 elif event.type == pygame.MOUSEBUTTONDOWN:
40 pos = event.pos
41 if figures:
42 figures[-1].append(pos)
43 if not figures or figures[-1].complete():
44 Mainloop = Editor
45 pygame.mouse.set_visible(True)
46 return pos
47
48 class Dot:
49 '''Точка'''
50 def __init__(self, size=3):
51 '''size — размер точки при рисовании'''
52 self.dots = []
53 self.ndots = 1
54 self.size = size
55
56 def append(self, dot):
57 '''Добавить точку'''
58 if not self.complete():
59 self.dots.append(dot)
60
61 def complete(self):
62 '''Редактирование фигуры закончено?'''
63 return len(self.dots) >= self.ndots
64
65 def draw(self, scr, pos=None):
66 '''Нарисовать точку по имеющимся координатам илив позиции pos'''
67 if self.complete():
68 pygame.draw.circle(scr, red, self.dots[0], self.size)
69 elif pos:
70 pygame.draw.circle(scr, red, pos, self.size)
71
72 class Line(Dot):
73 '''Отрезок'''
74 def __init__(self, size=2):
75 '''size — размер точки при рисовании'''
76 Dot.__init__(self, size)
77 self.ndots = 2
78
79 def shape(self, scr, pos1, pos2):
80 '''Нарисовать линию'''
81 pygame.draw.line(scr, green, pos1, pos2)
82
83 def draw(self, scr, pos=None):
84 '''Нарисовать в зависимости от количества введённых точек:
85 0 — точку в позиции pos
86 1 — фигуру по введённой опорной точке и pos
87 2 — фигуру по введённым опорной и второй точке'''
88 if self.complete():
89 self.shape(scr, self.dots[0], self.dots[1])
90 elif pos:
91 pygame.draw.circle(scr, green, pos, self.size)
92 if self.dots:
93 self.shape(scr, self.dots[0], pos)
94
95 def Redraw(scr, pos):
96 '''Нарисовать поле сфигурами, координаты курсора — pos'''
97 scr.fill(black)
98 for figure in figures:
99 figure.draw(scr, pos)
100
101 size = width, height = 1000,700
102
103 screen = pygame.display.set_mode(size)
104
105 figures,Mainloop=[],Editor
106 pos, again, debug = None, True, False
107 while again:
108 event = pygame.event.wait()
109 # Общие события
110 if event.type == pygame.QUIT:
111 again = False
112 Mainloop(event)
113 Redraw(screen, pos)
114 pygame.display.flip()
Прикреплённые файлы
Для ссылки на прикреплённый файл в тексте страницы напишите attachment:имяфайла, как показано ниже в списке файлов. Не используйте URL из ссылки «[получить]», так как он чисто внутренний и может измениться.- [получить | показать] (2013-12-14 19:07:22, 3.6 KB) [[attachment:2013-12-13-greditor.py]]
- [получить | показать] (2013-12-20 14:21:32, 6.5 KB) [[attachment:2013-12-13-greditor2.py]]
Вам нельзя прикреплять файлы к этой странице.