Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0bd2256

Browse files
committed
Python Pygame
1 parent d0f113d commit 0bd2256

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

‎taiyangxue/pygame/chimp.py‎

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import os, sys
2+
import pygame
3+
from pygame.locals import *
4+
5+
if not pygame.font: print('Warning, fonts disabled')
6+
if not pygame.mixer: print('Warning, sound disabled')
7+
8+
def load_image(name, colorkey=None):
9+
fullname = os.path.join('chimp_data', name)
10+
try:
11+
image = pygame.image.load(fullname)
12+
except pygame.error as message:
13+
print('Cannot load image:', name)
14+
raise SystemExit(message)
15+
image = image.convert()
16+
if colorkey is not None:
17+
if colorkey is -1:
18+
colorkey = image.get_at((0, 0))
19+
image.set_colorkey(colorkey, RLEACCEL)
20+
return image, image.get_rect()
21+
22+
def load_sound(name):
23+
class NoneSound:
24+
def play(self): pass
25+
if not pygame.mixer:
26+
return NoneSound()
27+
fullname = os.path.join('chimp_data', name)
28+
print(fullname)
29+
try:
30+
sound = pygame.mixer.Sound(fullname)
31+
except pygame.error as message:
32+
print('Cannot load sound:', fullname)
33+
raise SystemExit(message)
34+
return sound
35+
36+
class Fist(pygame.sprite.Sprite):
37+
"""moves a clenched fist on the screen, following the mouse"""
38+
def __init__(self):
39+
pygame.sprite.Sprite.__init__(self) # call Sprite initializer
40+
self.image, self.rect = load_image('fist.bmp', -1)
41+
self.punching = 0
42+
43+
def update(self):
44+
"""move the fist based on the mouse position"""
45+
pos = pygame.mouse.get_pos()
46+
self.rect.midtop = pos
47+
if self.punching:
48+
self.rect.move_ip(5, 10)
49+
50+
def punch(self, target):
51+
"""returns true if the fist collides with the target"""
52+
if not self.punching:
53+
self.punching = 1
54+
hitbox = self.rect.inflate(-5, -5)
55+
return hitbox.colliderect(target.rect)
56+
57+
def unpunch(self):
58+
"""called to pull the fist back"""
59+
self.punching = 0
60+
61+
class Chimp(pygame.sprite.Sprite):
62+
"""moves a monkey critter across the screen. it can spin the
63+
monkey when it is punched."""
64+
def __init__(self):
65+
pygame.sprite.Sprite.__init__(self) # call Sprite intializer
66+
self.image, self.rect = load_image('chimp.bmp', -1)
67+
screen = pygame.display.get_surface()
68+
self.area = screen.get_rect()
69+
self.rect.topleft = 60, 10
70+
self.move = 5
71+
self.dizzy = 0
72+
73+
def update(self):
74+
"""walk or spin, depending on the monkeys state"""
75+
if self.dizzy:
76+
self._spin()
77+
else:
78+
self._walk()
79+
pass
80+
81+
def _walk(self):
82+
"""move the monkey across the screen, and turn at the ends"""
83+
newpos = self.rect.move((self.move, 0))
84+
if not self.area.contains(newpos):
85+
self.move = -self.move
86+
newpos = self.rect.move((self.move, 0))
87+
self.image = pygame.transform.flip(self.image, 1, 0)
88+
self.rect = newpos
89+
90+
def _spin(self):
91+
"""spin the monkey image"""
92+
center = self.rect.center
93+
self.dizzy += 12
94+
if self.dizzy >= 360:
95+
self.dizzy = 0
96+
self.image = self.original
97+
else:
98+
rotate = pygame.transform.rotate
99+
self.image = rotate(self.original, self.dizzy)
100+
self.rect = self.image.get_rect(center=center)
101+
102+
def punched(self):
103+
"""this will cause the monkey to start spinning"""
104+
if not self.dizzy:
105+
self.dizzy = 1
106+
self.original = self.image
107+
108+
def main():
109+
pygame.init()
110+
screen = pygame.display.set_mode((468, 90))
111+
pygame.display.set_caption('Monkey Fever')
112+
pygame.mouse.set_visible(0)
113+
114+
background = pygame.Surface(screen.get_size())
115+
background = background.convert()
116+
background.fill((250, 250, 250))
117+
118+
if pygame.font:
119+
font = pygame.font.SysFont('SimHei',24)
120+
121+
whiff_sound = load_sound('whiff.wav')
122+
punch_sound = load_sound('punch.wav')
123+
chimp = Chimp()
124+
fist = Fist()
125+
allsprites = pygame.sprite.Group((fist, chimp))
126+
clock = pygame.time.Clock()
127+
punchcount = 0
128+
hitcount = 0
129+
130+
while 1:
131+
clock.tick(60)
132+
for event in pygame.event.get():
133+
if event.type == QUIT:
134+
return
135+
elif event.type == KEYDOWN and event.key == K_ESCAPE:
136+
return
137+
elif event.type == MOUSEBUTTONDOWN:
138+
punchcount += 1
139+
if fist.punch(chimp):
140+
punch_sound.play() # punch
141+
chimp.punched()
142+
hitcount += 1
143+
else:
144+
whiff_sound.play() # miss
145+
pass
146+
elif event.type == MOUSEBUTTONUP:
147+
fist.unpunch()
148+
bg = background.copy()
149+
if punchcount > 0:
150+
msg = "打中次数: %d 击中率: %d%s" % (hitcount, round((hitcount/punchcount)*100), "%")
151+
else:
152+
msg = "挥舞拳头吧!"
153+
text = font.render(msg, 1, (10, 10, 10))
154+
textpos = text.get_rect(centerx=background.get_width()/2)
155+
bg.blit(text, textpos)
156+
157+
allsprites.update()
158+
screen.blit(bg, (0, 0))
159+
allsprites.draw(screen)
160+
pygame.display.flip()
161+
162+
if __name__ == "__main__":
163+
main()
5.37 KB
Binary file not shown.
4.28 KB
Binary file not shown.
4.08 KB
Binary file not shown.
5.71 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /