-
Notifications
You must be signed in to change notification settings - Fork 0
/
galaga.py
199 lines (154 loc) · 5.32 KB
/
galaga.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
import pygame, sys, random, time
from pygame.locals import *
points = 0
mx = 0
my = 0
gaming = True #게임중인지 여부
# 적군 class
class Enermy:
def __init__(self):
self.x = random.randint(0, 570)
self.y = -100
self.dy = 0
self.dy = random.randint(2, 6)
self.dx = random.choice((-(self.dy), (self.dy)))
def move(self):
self.dy += 0.1
self.y += self.dy
self.x += self.dx
def draw(self):
screen.blit(enermy_image, (self.x, self.y))
def hit(self,missile):
return pygame.Rect((self.x,self.y), (101, 100)).collidepoint(missile.x,missile.y)
def off_screen(self):
return self.y > 640
def bounce(self):
if self.x>570 or self.x<0:
self.dx = -(self.dx)
# 아군 class
class Forces:
def __init__(self):
self.x = 320
self.y = 574
def move(self):
if pressed_keys[K_LEFT] and self.x>0:
self.x -= 3
if pressed_keys[K_RIGHT] and self.x<540:
self.x += 3
def draw(self):
screen.blit(forces_image, (self.x, self.y))
def fire(self):
missiles.append(Missile(self.x+50))
def boom(self,enermy):
return (enermy.x+100>forces.x) and (enermy.y+100 > forces.y) and (enermy.x < forces.x+100)
# 미사일 class
class Missile:
def __init__(self,x):
self.x = x
self.y = 574
def move(self):
self.y -= 5
def off_screen(self):
return self.y < -8
def draw(self):
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
pygame.draw.line(screen, (r,g,b),(self.x, self.y), (self.x,self.y+8), 6)
class Button:
def __init__(self):
self.x = 250
self.y = 510
def click(self):
pos = pygame.mouse.get_pos()
mx = pos[0]
my = pos[1]
return pygame.Rect((self.x,self.y), (150, 100)).collidepoint(mx,my)
# 300이 mx 보다 작고, 610이 my보다 크고,
# class 복제
enermys = []
forces = Forces()
missiles = []
btn = Button()
# 온갖 설정
pygame.init()
pygame.display.set_caption("Galaga")
screen = pygame.display.set_mode((640, 650))
myfont = pygame.font.Font(None, 50)
enermy_image = pygame.image.load("enemy.png").convert()
enermy_image.set_colorkey((0,0,0))
forces_image = pygame.image.load("forces.png").convert()
forces_image.set_colorkey((0,0,0))
game_over = pygame.image.load("game-over.png").convert()
restart_btn = pygame.image.load("restart.png").convert()
last_enermy_spawn_time = 0
clock = pygame.time.Clock()
while 1:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN and event.key == K_SPACE:
forces.fire()
if event.type == pygame.MOUSEBUTTONDOWN:
if btn.click(): # 다시시작버튼
print('ggg')
gaming = True
pressed_keys = pygame.key.get_pressed()
pos = pygame.mouse.get_pos()
mx = pos[0]
my = pos[1]
if time.time() - last_enermy_spawn_time > 1:
enermys.append(Enermy())
last_enermy_spawn_time = time.time()
screen.fill((0, 0, 0))
forces.move()
forces.draw()
# 적군
i = 0
while i < len(enermys):
enermys[i].move()
enermys[i].bounce()
enermys[i].draw()
if enermys[i].off_screen():
del enermys[i]
i-= 1
i += 1
# 미사일
i = 0
while i < len(missiles):
missiles[i].move()
missiles[i].draw()
if missiles[i].off_screen():
del missiles[i]
i-= 1
i += 1
# 충돌시
i = 0
while i < len(enermys) :
j = 0
while j < len(missiles):
if enermys[i].hit(missiles[j]):
del enermys[i]
del missiles[j]
i -= 1
points += 1
break
j += 1
i+= 1
score_img = myfont.render("Score : " + str(points), True, (255,255,255))
screen.blit(score_img , (10, 10))
# 끝!!
for enermy in enermys:
if forces.boom(enermy):
screen.blit(game_over, (170, 200))
screen.blit(restart_btn, (250, 510))
print('점수는', points)
pygame.display.update()
gaming = False
break
if gaming:
pygame.display.update()
else:
enermys = []
points = 0