/
copypy.py
235 lines (208 loc) · 7.52 KB
/
copypy.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'''Rendering Graphics.'''
from cube import *
from headers import *
import math
from math import pi,tan,atan,sin,cos
from pygame import *
import pygame
class GUI(object):
'''The class for the rendering purpose.'''
def __init__(self,cube):
self.cube=cube
self.readsExit=False
self.clock=pygame.time.Clock()
self.screen=None
self.xrotation=0
self.yrotation=0
self.gestureMode=False
self.gestureStartPos=(0,0)
self.gestures=[]
def eventHandler(self):
'''Handles events,such as mouse clicks.'''
Changed=False
firstTime=True
while not Changed:
self.clock.tick(50);
i=pygame.event.wait()
if i.type==QUIT:
self.readsExit=True
return
else:
if i.type==MOUSEBUTTONDOWN and i.button==1:
self.gestureMode=True
self.gestureStartPos=i.pos
Changed=True
elif i.type==MOUSEBUTTONUP and i.button==1:
self.gestureMode=False
gesture=(self.gestureStartPos,i.pos)
self.gestures.append(gesture)
Changed=True
if i.type==MOUSEMOTION and self.gestureMode:
if abs(i.rel[0])<=1: return
if abs(i.rel[1])<=1: return
self.xrotation += i.rel[0]
self.yrotation += i.rel[1]
Changed=True
def getFrontRect(self):
'''Returns surface for front side.'''
front=Surface((221,221))
front.fill(Color("gray"))
fronties=self.cube.getSide(FRONT_SIDE)
for item in fronties:
surface=Surface((70,70))
surface.fill(item.xz.getColor())
y=(2-item.pos[2])*75
x=item.pos[0] * 75
rect=pygame.Rect(x,y,70,70)
front.blit(surface,rect)
return front
def getRightRect(self):
'''Returns surface for right side.'''
side=Surface((221,221))
side.fill(Color("gray"))
sidies=self.cube.getSide(RIGHT_SIDE)
for item in sidies:
surface=Surface((70,70))
surface.fill(item.yz.getColor())
y=(2-item.pos[2])*75
x=item.pos[1] * 75
rect=pygame.Rect(x,y,70,70)
side.blit(surface,rect)
theta=30*pi/180
width=110
side=pygame.transform.scale(side,(width,side.get_rect().height))
oldRect=side.get_rect()
width=oldRect.width
newSurface=Surface((width,oldRect.height+oldRect.width*tan(theta)))
newSurface.fill((0,0,0,0))
newSurface.set_colorkey((0,0,0,0))
for x in range(0,oldRect.width,2):
a=oldRect.width-x
h=a*tan(theta)
for y in range(0,oldRect.height):
color=side.get_at((x,y))
pos=(x,int(y+h))
pos1=(x+1,int(y+h))
pos2=(x+3,int(y+h))
newSurface.set_at(pos,color)
newSurface.set_at(pos1,color)
newSurface.set_at(pos2,color)
return newSurface
def getTopRect(self):
'''Returns surface for top side.'''
side=Surface((221,221))
side.fill(Color("gray"))
sidies=self.cube.getSide(TOP_SIDE)
for item in sidies:
surface=Surface((70,70))
surface.fill(item.xy.getColor())
y=(2-item.pos[1])*75
x=item.pos[0] * 75
rect=pygame.Rect(x,y,70,70)
side.blit(surface,rect)
theta=60*pi/180
height=62
side=pygame.transform.scale(side,(side.get_rect().width,height))
oldRect=side.get_rect()
height=oldRect.height
newSurface=Surface((oldRect.width+oldRect.height*tan(theta),height))
newSurface.fill((0,0,0,0))
newSurface.set_colorkey((0,0,0,0))
for y in range(0,oldRect.height,2):
a=oldRect.height-y
w=a*tan(theta)
for x in range(0,oldRect.width):
color=side.get_at((x,y))
pos=(int(x+w),y)
pos1=(int(x+w),y+1)
pos2=(int(x+w),y+2)
newSurface.set_at(pos,color)
newSurface.set_at(pos1,color)
newSurface.set_at(pos2,color)
return newSurface
#return side
def getLeftRect(self):
'''Returns surface for left side.'''
side=Surface((221,221))
side.fill(Color("gray"))
sidies=self.cube.getSide(LEFT_SIDE)
for item in sidies:
surface=Surface((70,70))
surface.fill(item.yz.getColor())
y=(2-item.pos[2])*75
x=(2-item.pos[1]) * 75
rect=pygame.Rect(x,y,70,70)
side.blit(surface,rect)
return side
def getBottomRect(self):
'''Returns surface for bottom side.'''
side=Surface((221,221))
side.fill(Color("gray"))
sidies=self.cube.getSide(BOTTOM_SIDE)
for item in sidies:
surface=Surface((70,70))
surface.fill(item.xy.getColor())
y=(item.pos[1])*75
x=(item.pos[0]) * 75
rect=pygame.Rect(x,y,70,70)
side.blit(surface,rect)
return side
def getBackRect(self):
'''Returns surface for back side.'''
side=Surface((221,221))
side.fill(Color("gray"))
sidies=self.cube.getSide(BACK_SIDE)
for item in sidies:
surface=Surface((70,70))
surface.fill(item.xz.getColor())
y=(2-item.pos[2])*75
x=(item.pos[0]) * 75
rect=pygame.Rect(x,y,70,70)
side.blit(surface,rect)
return side
def updateDisplay(self):
'''reRenders the screen.'''
#print self.xrotation,self.yrotation
#return
front=self.getFrontRect()
frontRect=front.get_rect()
frontRect.centerx=self.screen.get_rect().centerx
frontRect.centery=self.screen.get_rect().centery-30
rightSide=self.getRightRect()
rightRect=rightSide.get_rect()
rightRect.x += 461
rightRect.centery=self.screen.get_rect().centery-62
topSide=self.getTopRect()
topRect=topSide.get_rect()
topRect.y=173
topRect.centerx=frontRect.centerx+52
leftSide=self.getLeftRect()
leftRect=leftSide.get_rect()
leftRect.y=rightRect.y
bottomSide=self.getBottomRect()
bottomRect=bottomSide.get_rect()
bottomRect.y=480
bottomRect.x=topRect.x
backSide=self.getBackRect()
backSide=pygame.transform.scale(backSide,(150,150))
backRect=backSide.get_rect()
backRect.x=550
backRect.y=560
self.screen.blit(front,frontRect)
self.screen.blit(rightSide,rightRect);
self.screen.blit(topSide,topRect)
self.screen.blit(leftSide,leftRect)
self.screen.blit(bottomSide,bottomRect)
self.screen.blit(backSide,backRect)
pygame.display.update()
def begin(self):
'''Gameloop.'''
self.screen=pygame.display.set_mode((700,750))
while not self.readsExit:
self.eventHandler()
self.updateDisplay()
#self.clock.tick(1)
pygame.quit()
if __name__=="__main__":
#import main
print "done"