/
savebouncingball.py
191 lines (166 loc) · 5.12 KB
/
savebouncingball.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
import os
import sys
import pygame
import stddrawpygame as stddraw
import string
import thread
from pygame.locals import *
global _surface
global _deferDraw
def get_key():
while 1:
event = pygame.event.poll()
if event.type == KEYDOWN:
return event.key
else:
pass
#----stddraw functions we need for now
def _draw():
"""
Update the surface on the window if deferDraw is False.
"""
if not _deferDraw:
pygame.display.flip()
checkForEvents()
def show(t=None):
"""
Update the surface on the window. If t is given, defer subsequent updates
for t milliseconds.
"""
global _deferDraw
_deferDraw = False
_draw()
if t != None:
time.sleep(float(t) / 1000.0)
_deferDraw = True
def filledSquare(x, y, r):
global _surface
"""
Draw on the surface a filled square whose sides are of length 2r,
centered on (x, y).
"""
ws = stddraw._factorX(2*r)
hs = stddraw._factorY(2*r)
if (ws <= 1) and (hs <= 1):
stddraw._pixel(x, y)
else:
xs = stddraw._scaleX(x)
ys = stddraw._scaleY(y)
filledRectangle(x, y, r, r)
def filledRectangle(x, y, w, h):
"""
Draw on the surface a filled rectangle of width w and height h,
centered on (x, y).
"""
global _surface
ws = stddraw._factorX(2*w)
hs = stddraw._factorY(2*h)
if (ws <= 1) and (hs <= 1):
stddraw._pixel(x, y)
else:
xs = stddraw._scaleX(x)
ys = stddraw._scaleY(y)
pygame.draw.rect(_surface,
pygameColor(stddraw.pygameColor(stddraw.GRAY)),
pygame.Rect(xs-ws/2, ys-hs/2, ws, hs),
0)
_draw()
def filledCircle(x, y, r):
"""
Draw on the surface a filled circle of radius r centered on (x, y).
"""
global _surface
ws = stddraw._factorX(2*r)
hs = stddraw._factorY(2*r)
if (ws <= 1) and (hs <= 1):
stddraw._pixel(x, y)
else:
xs = stddraw._scaleX(x)
ys = stddraw._scaleY(y)
pygame.draw.ellipse(_surface,
stddrawpygameColor(stddraw.BLACK),
pygame.Rect(xs-ws/2, ys-hs/2, ws, hs),
0)
_draw()
#-------
def display_buttonBackground(_background, message):
pygame.font.init()
fontobject = pygame.font.Font(None, 18)
buttonBackground = pygame.Surface((512, 50))
buttonBackground.fill(stddraw.pygameColor(stddraw.RED))
button = pygame.Surface((80, 30))
button.fill(stddraw.pygameColor(stddraw.GRAY))
button.blit(fontobject.render("Save", 1, (0,0,0)), (button.get_width() / 2 - 20, button.get_height() / 2 - 5))
pygame.draw.line(buttonBackground, stddraw.pygameColor(stddraw.BLACK), (0, 49), (511, 49))
pygame.draw.rect(buttonBackground, stddraw.pygameColor(stddraw.BLACK),
(10, 10,
260, 30), 0)
pygame.draw.rect(buttonBackground, stddraw.pygameColor(stddraw.WHITE),
(12, 12,
255, 25), 0)
if len(message) != 0:
print "should be printing words"
buttonBackground.blit(fontobject.render(message, 1, stddraw.pygameColor(stddraw.BLACK)),
(15, 20))
buttonBackground.blit(button, (290, 10))
_background.blit(buttonBackground, (0, 0))
pygame.display.flip()
return buttonBackground
def get_name(_background):
global _surface
pygame.font.init()
current_string = []
while 1:
inkey = get_key()
if inkey == K_BACKSPACE:
current_string = current_string[0:-1]
elif inkey == K_RETURN:
pygame.image.save(_surface, ''.join(current_string)+ ".JPEG")
break
elif inkey == K_MINUS:
current_string.append("_")
elif inkey <= 127:
current_string.append(chr(inkey))
display_buttonBackground(_background, string.join(current_string, ""))
return string.join(current_string, "")
def window( threadName, delay):
## Main loop.
global _surface
_background = pygame.display.set_mode([512, 562])
_surface = pygame.Surface((512, 512))
_surface.fill(stddraw.pygameColor(stddraw.WHITE))
_background.blit(_surface, (0, 50))
display_buttonBackground(_background, "")
name = get_name(_background)
print name + " was entered"
def bouncingBall( threadName, delay):
global _surface
stddraw.setXscale(-1.0, 1.0)
stddraw.setYscale(-1.0, 1.0)
rx = .480
ry = .860
vx = .015
vy = .023
radius = .05
dt = 20
while True:
# Update ball position and draw it there.
if abs(rx + vx) + radius > 1.0:
vx = -vx
if abs(ry + vy) + radius > 1.0:
vy = -vy
rx = rx + vx
ry = ry + vy
filledSquare(0, 0, 1.0)
filledCircle(rx, ry, radius)
show(dt)
# Update ball position and draw it there.
def main():
try:
thread.start_new_thread( window, ("Thread-1", 2, ) )
thread.start_new_thread( bouncingBall, ("Thread-2", 4, ) )
except:
print "Error: unable to start thread"
while 1:
pass
if __name__ == '__main__': main()