-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
142 lines (117 loc) · 4.29 KB
/
main.py
File metadata and controls
142 lines (117 loc) · 4.29 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
import pygame, sys
from pygame.locals import *
import random
from Config import *
from Slot import *
from Point import *
from Mallet import *
from Explosion import *
def sortLayer(sprites):
for zombie in sprites:
posY = zombie.rect.top
sprites.change_layer(zombie, posY)
def switchBackGroundId():
i = 0
if bgId == 0:
i = random.randint(1, NUM_BG - 1)
elif bgId == NUM_BG - 1:
i = random.randint(0, NUM_BG - 2)
elif random.randint(0, 1) == 1:
i = random.randint(0, bgId - 1)
else:
i = random.randint(bgId + 1, NUM_BG - 1)
return i
pygame.init()
loadDeadResource()
pygame.mixer.music.load('res/sound/bgMusic.mp3')
pygame.mixer.music.play(-1)
hitSound = pygame.mixer.Sound('res/sound/hit1.wav')
deadSounds = []
for x in range(NUM_DEAD_SOUND):
ds = pygame.mixer.Sound('res/sound/dead' + str(x+1) + ".wav")
deadSounds.append(ds)
gOverSound = pygame.mixer.Sound('res/sound/gameOver.wav')
replaySound = pygame.mixer.Sound('res/sound/horrorLaugh.mp3')
replaySound.play()
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("BEAT THE ZOMBIE")
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
bgs = []
for x in range(NUM_BG):
bg = pygame.image.load("res/Object/background_" + str(x+1) + ".png")
bgs.append(bg)
bgId = random.randint(0, NUM_BG - 1)
background = bgs[bgId]
# mainGroup = pygame.sprite.Group()
# mainGroup.add(background)
# slot_group = pygame.sprite.LayeredUpdates()
zombie_group = pygame.sprite.LayeredUpdates()
for x in range(Z_MAX_NUMBER):
# slot = Slot(x)
# slot_group.add(slot)
zombie = Zombie(x)
zombie_group.add(zombie)
mallet_group = pygame.sprite.LayeredUpdates()
mallet = Mallet()
mallet_group.add(mallet)
eps_group = pygame.sprite.LayeredUpdates()
eps = Explosion()
eps_group.add(eps)
point = Point()
loseNotice = pygame.font.SysFont('consolas', 30).render('Game Over!', True, WHITE)
loseNoticeRect = loseNotice.get_rect()
loseNoticeRect.center = (WIDTH//2, HEIGHT//2)
loseBackground = pygame.Surface((WIDTH, HEIGHT), SRCALPHA)
isEndGame = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONUP:
if isEndGame:
loseBackground.fill((0, 0, 0, 0))
isEndGame = False
point.reset()
replaySound.play()
pygame.mixer.music.play(-1)
bgId = switchBackGroundId()
background = bgs[bgId]
else:
pos = pygame.mouse.get_pos()
mallet_group.sprites()[0].attack()
hit = False
for spr in zombie_group.sprites():
if spr.checkHit(pos):
print("hit")
hit = True
point.updatePoint(1)
eps_group.sprites()[0].show()
spr.die()
hitSound.play()
deadSounds[random.randint(0, NUM_DEAD_SOUND - 1)].play()
if hit == False:
point.updatePoint(0)
if point.totalBeat - point.point >= DELTA_P_LOSE:
SCREEN.blit(backgroundMain, (0, 0))
point.draw(SCREEN)
loseBackground.fill((0, 0, 0, 50))
loseBackground.blit(loseNotice, loseNoticeRect)
SCREEN.blit(loseBackground, (0, 0))
isEndGame = True
gOverSound.play()
pygame.mixer.music.pause()
if isEndGame == False:
backgroundMain = pygame.transform.scale(background, (background.get_rect().width // 4, background.get_rect().height // 4))
zombie_group.update()
sortLayer(zombie_group)
mallet_group.update()
eps_group.update()
zombie_group.draw(backgroundMain)
eps_group.draw(backgroundMain)
mallet_group.draw(backgroundMain)
SCREEN.blit(backgroundMain, (0, 0))
point.draw(SCREEN)
pygame.display.update()
clock.tick(FPS)