-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathchimp.py
163 lines (144 loc) · 5.04 KB
/
chimp.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
import os, sys
import pygame
from pygame.locals import *
if not pygame.font: print('Warning, fonts disabled')
if not pygame.mixer: print('Warning, sound disabled')
def load_image(name, colorkey=None):
fullname = os.path.join('chimp_data', name)
try:
image = pygame.image.load(fullname)
except pygame.error as message:
print('Cannot load image:', name)
raise SystemExit(message)
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
def load_sound(name):
class NoneSound:
def play(self): pass
if not pygame.mixer:
return NoneSound()
fullname = os.path.join('chimp_data', name)
print(fullname)
try:
sound = pygame.mixer.Sound(fullname)
except pygame.error as message:
print('Cannot load sound:', fullname)
raise SystemExit(message)
return sound
class Fist(pygame.sprite.Sprite):
"""moves a clenched fist on the screen, following the mouse"""
def __init__(self):
pygame.sprite.Sprite.__init__(self) # call Sprite initializer
self.image, self.rect = load_image('fist.bmp', -1)
self.punching = 0
def update(self):
"""move the fist based on the mouse position"""
pos = pygame.mouse.get_pos()
self.rect.midtop = pos
if self.punching:
self.rect.move_ip(5, 10)
def punch(self, target):
"""returns true if the fist collides with the target"""
if not self.punching:
self.punching = 1
hitbox = self.rect.inflate(-5, -5)
return hitbox.colliderect(target.rect)
def unpunch(self):
"""called to pull the fist back"""
self.punching = 0
class Chimp(pygame.sprite.Sprite):
"""moves a monkey critter across the screen. it can spin the
monkey when it is punched."""
def __init__(self):
pygame.sprite.Sprite.__init__(self) # call Sprite intializer
self.image, self.rect = load_image('chimp.bmp', -1)
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.rect.topleft = 60, 10
self.move = 5
self.dizzy = 0
def update(self):
"""walk or spin, depending on the monkeys state"""
if self.dizzy:
self._spin()
else:
self._walk()
pass
def _walk(self):
"""move the monkey across the screen, and turn at the ends"""
newpos = self.rect.move((self.move, 0))
if not self.area.contains(newpos):
self.move = -self.move
newpos = self.rect.move((self.move, 0))
self.image = pygame.transform.flip(self.image, 1, 0)
self.rect = newpos
def _spin(self):
"""spin the monkey image"""
center = self.rect.center
self.dizzy += 12
if self.dizzy >= 360:
self.dizzy = 0
self.image = self.original
else:
rotate = pygame.transform.rotate
self.image = rotate(self.original, self.dizzy)
self.rect = self.image.get_rect(center=center)
def punched(self):
"""this will cause the monkey to start spinning"""
if not self.dizzy:
self.dizzy = 1
self.original = self.image
def main():
pygame.init()
screen = pygame.display.set_mode((468, 90))
pygame.display.set_caption('Monkey Fever')
pygame.mouse.set_visible(0)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
if pygame.font:
font = pygame.font.SysFont('SimHei',24)
whiff_sound = load_sound('whiff.wav')
punch_sound = load_sound('punch.wav')
chimp = Chimp()
fist = Fist()
allsprites = pygame.sprite.Group((fist, chimp))
clock = pygame.time.Clock()
punchcount = 0
hitcount = 0
while 1:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
elif event.type == MOUSEBUTTONDOWN:
punchcount += 1
if fist.punch(chimp):
punch_sound.play() # punch
chimp.punched()
hitcount += 1
else:
whiff_sound.play() # miss
pass
elif event.type == MOUSEBUTTONUP:
fist.unpunch()
bg = background.copy()
if punchcount > 0:
msg = "打中次数: %d 击中率: %d%s" % (hitcount, round((hitcount/punchcount)*100), "%")
else:
msg = "挥舞拳头吧!"
text = font.render(msg, 1, (10, 10, 10))
textpos = text.get_rect(centerx=background.get_width()/2)
bg.blit(text, textpos)
allsprites.update()
screen.blit(bg, (0, 0))
allsprites.draw(screen)
pygame.display.flip()
if __name__ == "__main__":
main()