-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon2.py
171 lines (142 loc) · 5.22 KB
/
polygon2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
########################################################
### Polygon ###
########################################################
__author__ = "Jack B. Du (Jiadong Du)"
__copyright__ = "Copyright 2014, DS @NYUSH"
__email__ = "JackBDu@nyu.edu"
import pygame
from pygame.locals import *
from positional_list import PositionalList
import math
# absolute varibles
BLACK_COLOR = (0, 0, 0)
WHITE_COLOR = (255, 255, 255)
RED_COLOR = (255, 0, 0)
GREEN_COLOR = (0, 255, 0)
BLUE_COLOR = (0, 0, 255)
# manual initialization
pygame.init()
INIT_SCREEN_W = 1024
INIT_SCREEN_H = 768
FPS = 50
REDIUS = 10
bg_color = WHITE_COLOR
fullscreen = False
caption = "Jack's Polygon"
add = False
closed = False
move = False
deltaX = 0
deltaY = 0
mousePos = (0, 0)
pMousePos = mousePos
letsMove = False
inserted = False
# get device info
infoObject = pygame.display.Info()
FULLSCREEN_W = infoObject.current_w
FULLSCREEN_H = infoObject.current_h
# automatic initialization
if fullscreen:
current_screen_w = FULLSCREEN_W
current_screen_h = FULLSCREEN_H
else:
current_screen_w = INIT_SCREEN_W
current_screen_h = INIT_SCREEN_H
screen = pygame.display.set_mode((current_screen_w, current_screen_h), 0, 32)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(bg_color)
pygame.display.set_caption(caption)
def main():
global screen, polygonList, mainloop
polygonList = PositionalList()
mainloop = True
while mainloop:
pygame.time.Clock().tick(FPS)
screen.blit(background,(0,0))
update()
draw()
pygame.display.update()
pygame.quit()
def update():
global screen, add, mainloop, closed, move, deltaX, deltaY, mousePos, pMousePos, letsMove, currentCursor, inserted, fullscreen, current_screen_w, current_screen_h, background
for event in pygame.event.get():
if event.type == QUIT:
mainloop = False
if event.type == KEYDOWN:
# handling fullscreen
if event.key == K_f:
fullscreen = not fullscreen
if fullscreen:
current_screen_w = FULLSCREEN_W
current_screen_h = FULLSCREEN_H
screen = pygame.display.set_mode((current_screen_w, current_screen_h), FULLSCREEN, 32)
else:
current_screen_w = INIT_SCREEN_W
current_screen_h = INIT_SCREEN_H
screen = pygame.display.set_mode((current_screen_w, current_screen_h), 0, 32)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(bg_color)
if event.key == K_m:
letsMove = not letsMove
if event.type == MOUSEBUTTONDOWN:
mousePos = pygame.mouse.get_pos()
if not closed:
add = True
elif letsMove:
for pos in polygonList:
dist = math.sqrt((mousePos[0]-pos[0]-deltaX)**2 + (mousePos[1]-pos[1]-deltaY)**2)
if dist <= REDIUS:
move = True
elif not inserted:
cursor = polygonList.first()
pos = cursor.element()
dist = math.sqrt((mousePos[0]-pos[0]-deltaX)**2 + (mousePos[1]-pos[1]-deltaY)**2)
i = 1
length = len(polygonList)
while length > i and dist > REDIUS:
i += 1
cursor = polygonList.after(cursor)
pos = cursor.element()
dist = math.sqrt((mousePos[0]-pos[0]-deltaX)**2 + (mousePos[1]-pos[1]-deltaY)**2)
if dist <= REDIUS:
polygonList.add_after(cursor, (mousePos[0]-deltaX, mousePos[1]-deltaY))
currentCursor = polygonList.after(cursor)
inserted = True
elif event.type == MOUSEBUTTONUP:
if closed:
move = False
inserted = False
if event.type == MOUSEMOTION:
mousePos = pygame.mouse.get_pos()
if move:
deltaX += mousePos[0] - pMousePos[0]
deltaY += mousePos[1] - pMousePos[1]
if inserted:
polygonList.replace(currentCursor, (mousePos[0]-deltaX, mousePos[1]-deltaY))
pMousePos = mousePos
if add:
for pos in polygonList:
dist = math.sqrt((mousePos[0]-pos[0])**2 + (mousePos[1]-pos[1])**2)
if dist <= REDIUS:
mousePos = pos
polygonList.add_last(mousePos)
add = False
if len(polygonList) > 3:
firstPos = polygonList.first().element()
if mousePos == firstPos:
closed = True
def draw():
pPos = None
## circles and lines are separate so that lines are all underneath circles
for pos in polygonList:
pos = (pos[0] + deltaX, pos[1] + deltaY)
if pPos != None:
pygame.draw.line(screen, GREEN_COLOR, pPos, pos, 3)
pPos = pos
for pos in polygonList:
pos = (pos[0] + deltaX, pos[1] + deltaY)
pygame.draw.circle(screen, BLACK_COLOR, pos, REDIUS, 0)
main()