-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
232 lines (213 loc) · 9.63 KB
/
main.py
File metadata and controls
232 lines (213 loc) · 9.63 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
import pygame as pg
import random
from os import path
from settings import *
from sprites import *
class Game:
def __init__(self):
# 遊戲初始化
pg.init()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption(TITLE)
self.clock = pg.time.Clock()
self.running = True
self.font_name = pg.font.match_font(FONT_NAME)
self.load_data()
def load_data(self):
# 讀入資料
self.dir = path.dirname(__file__) # 如果在當前資料夾執行檔案,這行就沒有用。但如果用絕對路徑執行,這行就有用
self.img_dir = path.join(self.dir, 'img')
# 讀入stage
self.stage_img = pg.image.load(path.join(self.img_dir, 'stage.jpg')).convert_alpha()
self.stage_img = pg.transform.scale(self.stage_img, (1000, 561))
self.stage_img = pg.transform.flip(self.stage_img, True, False)
self.stage_img_rect = self.stage_img.get_rect()
self.stage_img_rect.midbottom = (WIDTH / 2, HEIGHT)
# 讀入intrologo
self.intrologo_img = pg.image.load(path.join(self.img_dir, 'intrologo.jpg')).convert()
self.intrologo_img_rect = self.intrologo_img.get_rect()
self.intrologo_img = pg.transform.scale(self.intrologo_img,
(self.intrologo_img_rect.width * 3 // 5 , self.intrologo_img_rect.height * 3 // 5))
self.intrologo_img_rect = self.intrologo_img.get_rect()
self.intrologo_img_rect.center = (WIDTH / 2, HEIGHT / 3)
# 讀入音樂
self.snd_dir = path.join(self.dir, 'snd')
self.punch_swoosh_sound = pg.mixer.Sound(path.join(self.snd_dir, 'Punch_Swoosh.ogg'))
self.hurt_sound = pg.mixer.Sound(path.join(self.snd_dir, 'Punch_Sound_Effect.ogg'))
self.dizzy_sound = pg.mixer.Sound(path.join(self.snd_dir, 'Dizzy_Sound_Effect.ogg'))
def new(self):
# 開始新遊戲
self.all_sprites = pg.sprite.Group()
self.red_team = pg.sprite.Group()
self.blue_team = pg.sprite.Group()
self.boxer_red = Boxer(self, 'red')
self.boxer_blue = Boxer(self, 'blue')
pg.mixer.music.load(path.join(self.snd_dir, 'playing.ogg'))
self.run()
def run(self):
# Game Loop
pg.mixer.music.play(loops=-1)
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
pg.mixer.music.fadeout(500)
def update(self):
# Game Loop - Update,遊戲的動作
self.all_sprites.update()
#碰撞判定
hits = pg.sprite.spritecollide(self.boxer_red, self.blue_team, False, pg.sprite.collide_mask)
if hits:
# 紅方攻擊判斷
if not self.boxer_blue.hurting and not self.boxer_blue.dizzying \
and not self.boxer_red.walking \
and self.boxer_red.pos.x + 55 < self.boxer_blue.pos.x:
# 衝刺攻擊判定
if self.boxer_red.sprinting:
self.hurt_sound.play()
self.boxer_blue.blood -= 20
self.boxer_blue.hurting = True
# 正拳擊中判定
if self.boxer_red.punching:
self.hurt_sound.play()
self.boxer_blue.blood -= 10
self.boxer_blue.hurting = True
# 上鉤拳擊中判定
if self.boxer_red.punching_up:
self.hurt_sound.play()
self.boxer_blue.blood -= 5
self.boxer_blue.hurting = True
self.boxer_blue.dizzy_num += random.randrange(1, 4)
if self.boxer_blue.dizzy_num >= 13:
self.dizzy_sound.play()
self.boxer_blue.dizzy_num = 0
self.boxer_blue.dizzying = True
# 藍方攻擊判斷
if not self.boxer_red.hurting and not self.boxer_red.dizzying \
and not self.boxer_blue.walking \
and self.boxer_blue.pos.x - 55 > self.boxer_red.pos.x:
# 衝刺攻擊判定
if self.boxer_blue.sprinting:
self.hurt_sound.play()
self.boxer_red.blood -= 20
self.boxer_red.hurting = True
# 正拳擊中判定
if self.boxer_blue.punching:
self.hurt_sound.play()
self.boxer_red.blood -= 10
self.boxer_red.hurting = True
# 上鉤拳擊中判定
if self.boxer_blue.punching_up:
self.hurt_sound.play()
self.boxer_red.blood -= 5
self.boxer_red.hurting = True
self.boxer_red.dizzy_num += random.randrange(1, 4)
if self.boxer_red.dizzy_num >= 13:
self.dizzy_sound.play()
self.boxer_red.dizzy_num = 0
self.boxer_red.dizzying = True
# 遊戲結束判斷
if self.boxer_red.blood <= 0:
self.boxer_red.KOing = True
elif self.boxer_blue.blood <= 0:
self.boxer_blue.KOing = True
def events(self):
# Game Loop - events,按鍵事件
for event in pg.event.get():
if event.type == pg.QUIT:
if self.playing:
self.playing = False
self.running = False
def draw(self):
# Game Loop - Draw,將update之後的東西繪製到遊戲主畫面上
self.screen.fill(WHITE)
self.screen.blit(self.stage_img, self.stage_img_rect)
self.all_sprites.draw(self.screen)
self.draw_blood()
self.draw_charge_point()
pg.display.flip()
def draw_blood(self):
# 繪製當前血量
# 藍色血量,+1為了防止完全歸零而畫不出來
self.blue_blood = pg.Surface((abs(self.boxer_blue.blood) * 1.5 + 1, 20))
self.blue_blood.fill(BLUE)
self.blue_blood_rect = self.blue_blood.get_rect()
self.blue_blood_rect.topleft = (750, 50)
self.screen.blit(self.blue_blood, self.blue_blood_rect)
# 紅色血量
self.red_blood = pg.Surface((abs(self.boxer_red.blood) * 1.5 + 1, 20))
self.red_blood.fill(RED)
self.red_blood_rect = self.red_blood.get_rect()
self.red_blood_rect.topleft = (100, 50)
self.screen.blit(self.red_blood, self.red_blood_rect)
def draw_charge_point(self):
# 繪製集氣條
# 藍方集氣條
if self.boxer_blue.charging and self.boxer_blue.charge_point > 0:
self.blue_charge_point = pg.Surface((2 * self.boxer_blue.charge_point, 20))
self.blue_charge_point.fill(GREEN)
self.blue_charge_point_rect = self.blue_charge_point.get_rect()
self.blue_charge_point_rect.bottomleft = (self.boxer_blue.pos.x - 60, self.boxer_blue.rect.top + 10)
self.screen.blit(self.blue_charge_point, self.blue_charge_point_rect)
# 紅方集氣條
if self.boxer_red.charging and self.boxer_red.charge_point > 0:
self.red_charge_point = pg.Surface((2 * self.boxer_red.charge_point, 20))
self.red_charge_point.fill(GREEN)
self.red_charge_point_rect = self.red_charge_point.get_rect()
self.red_charge_point_rect.bottomleft = (self.boxer_red.pos.x - 30, self.boxer_red.rect.top + 10)
self.screen.blit(self.red_charge_point, self.red_charge_point_rect)
def intro(self):
# Game Intro,遊戲開始畫面
pg.mixer.music.load(path.join(self.snd_dir, 'intro_music.mp3'))
pg.mixer.music.play(loops=-1)
self.screen.fill(WHITE)
self.screen.blit(self.intrologo_img, self.intrologo_img_rect)
self.draw_text('Py', 90, BLUE, 390, HEIGHT * 4 / 7 )
self.draw_text('Boxing!', 90, RED, 555, HEIGHT * 4 / 7)
self.draw_text('Press space to start', 44, GREEN, WIDTH / 2, HEIGHT * 7 / 9)
pg.display.flip()
self.wait_for_space()
pg.mixer.music.fadeout(500)
def gameover(self):
# Game Over,遊戲結束畫面
if not self.running:
return
pg.mixer.music.load(path.join(self.snd_dir, 'win_music.mp3'))
pg.mixer.music.play(loops=-1)
self.screen.fill(WHITE)
if self.boxer_blue.blood <= 0:
self.draw_text('RED WIN!', 100, RED, WIDTH / 2, HEIGHT / 3)
else:
self.draw_text('BLUE WIN!', 100, BLUE, WIDTH / 2, HEIGHT / 3)
self.draw_text('Press space to start', 44, GREEN, WIDTH / 2, HEIGHT * 2 / 3)
pg.display.flip()
self.wait_for_space()
pg.mixer.music.fadeout(500)
def wait_for_space(self):
# 按下任何按鍵後才會繼續執行
waiting = True
while waiting:
self.clock.tick(FPS)
for events in pg.event.get():
if events.type == pg.QUIT:
waiting = False
self.running = False
if events.type == pg.KEYUP:
if events.key == pg.K_SPACE:
waiting = False
def draw_text(self, text, size, color, x, y):
# 顯示文字
font = pg.font.Font(path.join(self.dir, 'font', 'BoxingWizards-Regular.otf'), size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
self.screen.blit(text_surface, text_rect)
game = Game()
game.intro()
while game.running:
game.new()
game.gameover()
pg.quit()
quit()