-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbashup.py
More file actions
executable file
·300 lines (249 loc) · 7.39 KB
/
bashup.py
File metadata and controls
executable file
·300 lines (249 loc) · 7.39 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
# Simple 'game' with no real purpose
# by Siraj 'Sid' Rakhada sid-git@mindless.co.uk
# up/down to change speed
# left/right to change size
# space to change colour
# f to display FPS
# m to start/stop music - randomly
# p to pause
# IMPORTANT! ESC and l together to quit
import sys, pygame
import random
from pygame.locals import *
import math
import keyboard
# R G B
WHITE = (255, 255, 255)
GRAY = (185, 185, 185)
BLACK = ( 0, 0, 0)
RED = (155, 0, 0)
LIGHTRED = (175, 20, 20)
GREEN = ( 0, 155, 0)
LIGHTGREEN = ( 20, 175, 20)
BLUE = ( 0, 0, 155)
LIGHTBLUE = ( 20, 20, 175)
YELLOW = (155, 155, 0)
LIGHTYELLOW = (175, 175, 20)
BGCOLOR = BLACK
TEXTCOLOR = WHITE
TEXTSHADOWCOLOR = GRAY
TUNES = ['tetrisb.mid', 'tetrisc.mid']
BEEPLIST = ['beep1.ogg', 'beep2.ogg', 'beep3.ogg', 'beep4.ogg']
global direction, dirty_rects
direction=0 # store the direction of the rectangle travel, 0 = right, 1 = left
def main():
global screen, WIDTH, HEIGHT, bgsurf, BIGFONT, BASICFONT, BEEPS
global dirty_rects, oldText, dx, dy, speed, oldsize, clock
oldText = None
dirty_rects = []
BEEPS = []
displayFPS = 1
fpscleared = 0
playtime = 0
speed = 100 # initial speed in pixels per second
dx = dy = None
oldsize = size = 200
colour = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
FPS=30
# Block win/meta key
keyboard.block_key('windows')
pygame.init()
#screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN|pygame.HWSURFACE|pygame.DOUBLEBUF)
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
clock = pygame.time.Clock() #create pygame clock object
#pygame.display.set_caption('Hello World!')
WIDTH=screen.get_width()
HEIGHT=screen.get_height()
BASICFONT = pygame.font.Font(None, 18)
BIGFONT = pygame.font.Font(None, 100)
#bgsurf = pygame.Surface(screen.get_size())
#bgsurf = bgsurf.convert()
#bgsurf.fill(BGCOLOR)
screen.fill(BGCOLOR)
pygame.key.set_repeat(500, 30)
# load the sound files
for i in range(len(BEEPLIST)):
BEEPS.append(pygame.mixer.Sound(BEEPLIST[i]))
curX = screen.get_size()[0]/2
curY = screen.get_size()[1]/2
playing = None
screen.fill(BGCOLOR)
pygame.display.flip()
for i in range(len(BEEPS)):
BEEPS[i].play()
while True:
milliseconds = clock.tick(FPS)
seconds = milliseconds / 1000.0
playtime += seconds
keys = pygame.key.get_pressed()
if keys[K_ESCAPE] and keys[K_l]:
terminate()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
#if event.key == K_ESCAPE:
# terminate()
if event.key == K_UP:
if dx >= 0:
speed += 10
dx += 10
else:
speed -= 10
dx -= 10
if dy >= 0:
dy += 10
else:
dy -= 10
elif event.key == K_DOWN:
if dx >= 0:
dx -= 10
else:
dx += 10
if dy >= 0:
dy -= 10
else:
dy += 10
elif event.key == K_RIGHT:
size += 10
elif event.key == K_LEFT:
size -= 10
elif event.key == K_SPACE:
colour = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
elif event.key == pygame.K_f:
# clear the screen of the old fps text
if displayFPS == 1 and oldText:
oldText = None
screen.fill(BGCOLOR)
pygame.display.update()
# toggle fps display
displayFPS = not displayFPS
elif event.key == pygame.K_m:
if playing == None:
pygame.mixer.music.load(TUNES[random.randint(0,len(TUNES)-1)])
pygame.mixer.music.play(-1, 0.0)
playing = True
else:
pygame.mixer.music.stop()
playing = None
elif event.key == pygame.K_p:
# pause
showTextScreen("PAUSED")
else:
rad = random.triangular(0,2*math.pi,1)
dx = math.cos(rad)*abs(speed)
dy = math.sin(rad)*abs(speed)
sound = BEEPS[0]
random.choice(BEEPS).play()
if displayFPS and seconds % 1:
# run FPS display code
currentfps = "{:.2f}".format(clock.get_fps())
displayFPStext(currentfps)
(curX, curY)=moveRectangle(curX, curY, size, colour, seconds, speed)
pygame.display.update(dirty_rects)
#pygame.display.flip()
dirty_rects = []
def terminate():
pygame.quit()
#print "dirty_rects at exit: ", len(dirty_rects)
sys.exit()
def displayFPStext(fps):
global oldText
fpsfont = pygame.font.Font(None, 36)
text = fpsfont.render(fps, 1, (0,0,255), BGCOLOR)
newtextloc = text.get_rect(centerx=WIDTH/2)
# no previous oldText, make one up
if oldText == None:
oldText = newtextloc
# this draw over the old text
pygame.draw.rect(screen, BGCOLOR, oldText)
# this draws the new text
screen.blit(text, newtextloc)
# update the screen position where we just drew text and/or removed text
dirty_rects.append(oldText.union(newtextloc))
oldText = newtextloc
return
def moveRectangle(x, y, size, colour, seconds, speed):
global dx, dy, oldsize, BEEPS
sound = None
distance = HEIGHT
if dx == None:
dx = dy = speed # set initial speed - pixels per second
if oldsize != size:
# then we've had a size change
rect = pygame.Rect(x, y, oldsize, oldsize)
oldsize=size
sound = BEEPS[0]
sound.play()
else:
rect = pygame.Rect(x, y, size, size)
pygame.draw.rect(screen, BGCOLOR, rect)
dirty_rects.append(rect)
x += dx * seconds # set distance of X movement per frame
y += dy * seconds
if x < 0:
x = 0
dx *= -1
dx += random.randint(-15,15)
sound = BEEPS[random.randint(0,len(BEEPS)-1)]
elif x + size >= WIDTH:
x = WIDTH - size
dx *= -1
dx += random.randint(-15,15)
sound = BEEPS[random.randint(0,len(BEEPS)-1)]
if y < 0:
y = 0
dy *= -1
dy += random.randint(-15,15)
sound = BEEPS[random.randint(0,len(BEEPS)-1)]
elif y + size >= HEIGHT:
y = HEIGHT - size
dy *= -1
dy += random.randint(-15,15)
sound = BEEPS[random.randint(0,len(BEEPS)-1)]
if sound != None:
sound.play()
rect = pygame.Rect(x, y, size, size)
pygame.draw.rect(screen, colour, rect)
dirty_rects.append(rect)
#print "dx:", dx, "dy:", dy, "x:", x, "y:", y
return (x, y)
# taken from tetromino demo
def showTextScreen(text):
# This function displays large text in the
# center of the screen until a key is pressed.
# Draw the text drop shadow
titleSurf, titleRect = makeTextObjs(text, BIGFONT, TEXTSHADOWCOLOR)
titleRect.center = (int(WIDTH / 2), int(HEIGHT / 2))
screen.blit(titleSurf, titleRect)
# Draw the text
titleSurf, titleRect = makeTextObjs(text, BIGFONT, TEXTCOLOR)
titleRect.center = (int(WIDTH / 2) - 3, int(HEIGHT / 2) - 3)
screen.blit(titleSurf, titleRect)
# Draw the additional "Press a key to play." text.
pressKeySurf, pressKeyRect = makeTextObjs('Press a key to play.', BASICFONT, TEXTCOLOR)
pressKeyRect.center = (int(WIDTH / 2), int(HEIGHT / 2) + 100)
screen.blit(pressKeySurf, pressKeyRect)
while checkForKeyPress() == None:
pygame.display.update()
clock.tick()
screen.fill(BGCOLOR)
pygame.display.update()
def makeTextObjs(text, font, color):
surf = font.render(text, True, color)
return surf, surf.get_rect()
def checkForKeyPress():
for event in pygame.event.get():
if event.type == QUIT: #event is quit
terminate()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE: #event is escape key
terminate()
else:
return event.key #key found return with it
# no quit or key events in queue so return None
return None
if __name__ == '__main__':
main()