-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto.py
executable file
·219 lines (175 loc) · 6.14 KB
/
proto.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
#!/usr/bin/env python2.7
import argparse
from sys import exit
import pygame
from pygame.locals import *
from pygame.sprite import Sprite
def render_text(s, fontsize):
font = pygame.font.Font(None, fontsize)
text = font.render(s, True, pygame.Color("white"))
return text
def exit_game():
exit()
class Ball(Sprite):
def __init__(self, color, position, racket):
Sprite.__init__(self)
self.image = pygame.Surface((6,6))
pygame.draw.circle(self.image, pygame.Color(color), (3,3), 3)
self.rect = self.image.get_rect()
self.rect.center = position
self.velocity = [0,0]
self.racket = racket
def start(self):
self.velocity = [10, -10]
def update(self):
self.rect.move_ip(*self.velocity)
if self.rect.colliderect(self.racket.rect):
print "collide"
self.velocity[1] *= -1.2
self.rect.x += self.velocity[0]
if self.rect.top < 50:
self.velocity[1] *= -1
self.rect.top = 51
if self.rect.left < 190:
self.velocity[0] *= -1
self.rect.left = 191
if self.rect.right > 610:
self.velocity[0] *= -1
self.rect.right = 609
if self.rect.bottom > 600:
if self.velocity[1] > 0:
self.velocity[0] = 0
self.velocity[1] = 0
print "game over"
class Racket(Sprite):
def __init__(self, color, position):
Sprite.__init__(self)
self.image = pygame.Surface([40, 10])
self.rect = pygame.Rect(0,0,40,10)
pygame.draw.rect(self.image, pygame.Color(color), self.rect)
self.rect.center = position
self.velocity = 0
def left(self):
self.velocity -= 15
def right(self):
self.velocity += 15
def update(self):
if self.rect.left < 100 and self.velocity < 0:
pass
elif self.rect.right > 700 and self.velocity > 0:
pass
else:
self.rect.move_ip(self.velocity, 0)
class Options():
def __init__(self, res, opts):
self._width = res[0]
self._fontsize = 50
self.selected = 0
self.surf = pygame.Surface(res)
self.text = []
self.opts = opts
for opt in opts:
self.text.append(render_text(opt[0], 50))
def run(self):
self.running = True
while self.running:
for text in self.text:
x = self.surf.get_width()/2 - text.get_width()/2
y += text.get_height()*1.5
self.surf.blit(text, (x,y))
def update(self):
self.surf.fill(pygame.Color("black"))
pygame.draw.rect(self.surf, pygame.Color("purple"), pygame.Rect(0, (self.selected+1)*(self._fontsize), self._width, self._fontsize))
y = 0
for text in self.text:
x = self.surf.get_width()/2 - text.get_width()/2
y += text.get_height()*1.5
self.surf.blit(text, (x,y))
def draw(self, screen):
# screen.blit(self.select, (0, self.selected*1.5))
screen.blit(self.surf, (0,0))
def up(self):
self.selected -= 1
def down(self):
self.selected += 1
def select(self):
return self.opts[self.selected][1]
def nop():
pass
def menu(screen, clock):
mainmenu = [
["START GAME", 'play'],
["SELECT LEVEL", nop],
["OPTIONS", nop],
["QUIT", exit_game]
]
o = Options((600,400), mainmenu)
ismenu = True
while ismenu:
o.update()
o.draw(screen)
pygame.display.flip()
events = pygame.event.get()
for event in events:
if (event.type == QUIT):
exit_game()
if (event.type == KEYUP) and (event.key == K_ESCAPE):
exit_game()
if (event.type == KEYDOWN) and (event.key == K_UP):
o.up()
if (event.type == KEYDOWN) and (event.key == K_DOWN):
o.down()
if (event.type == KEYDOWN) and (event.key == K_RETURN):
selected = o.select()
if selected == 'play':
breakout(screen, clock)
else:
selected()
clock.tick(30)
def breakout(screen, clock):
game = pygame.Surface((600,600))
racket = Racket('white', (380, 560))
ball = Ball("yellow", (380, 550), racket)
keymap = {
pygame.K_SPACE: [ball.start, nop],
pygame.K_LEFT: [racket.left, racket.right],
pygame.K_RIGHT: [racket.right, racket.left]
}
isrunning = True
while isrunning:
bg = pygame.Surface((800,600))
bg.fill(pygame.Color("black"))
pygame.draw.Rect(bg, pygame.Color("white"), (190,50), (190,600), 5)
pygame.draw.Rect(bg, pygame.Color("white"), (610,50), (610,600), 5)
pygame.draw.Rect(bg, pygame.Color("white"), (190,50), (610,50), 5)
screen.blit(bg, (0,0))
sprites = pygame.sprite.RenderClear([racket, ball])
sprites.update()
sprites.draw(screen)
pygame.display.flip()
sprites.clear(screen, bg)
events = pygame.event.get()
for event in events:
if (event.type == QUIT) or ((event.type == KEYUP) and (event.key == K_ESCAPE)):
isrunning = False
if (event.type == pygame.KEYDOWN) and (event.key in keymap):
keymap[event.key][0]()
if (event.type == pygame.KEYUP) and (event.key in keymap):
keymap[event.key][1]()
clock.tick(30)
def main():
parser = argparse.ArgumentParser(description=
"A pygame implementation of Breakout")
parser.add_argument('-r', '--resolution',
help="Resolution", default='800x600')
parser.add_argument('-f', '--fullscreen',
help="Fullscreen", action='store_true')
args = vars(parser.parse_args())
res = tuple(int(x) for x in args['resolution'].split('x'))
fs = args['fullscreen']
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode(res)
menu(screen, clock)
if __name__ == '__main__':
main()