-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchimp.py
167 lines (148 loc) · 5.06 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
164
165
166
167
import pygame as pg
import os
main_dir = os.path.split(os.path.abspath(__file__))[0]
data_dir = os.path.join(main_dir, 'data')
# functions to load resources
def load_image(name, colorkey=None, scale=1):
fullname = os.path.join(data_dir, name)
image = pg.image.load(fullname)
size = image.get_size()
size = (size[0] * scale, size[1] * scale)
image = pg.transform.scale(image, size)
image = image.convert()
if colorkey is not None:
if colorkey == -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, pg.RLEACCEL)
return image, image.get_rect()
def load_sound(name):
class NoneSound:
def play(self):
pass
if not pg.mixer or not pg.mixer.get_init():
return NoneSound()
fullname = os.path.join(data_dir, name)
sound = pg.mixer.Sound(fullname)
return sound
# classes for our game objects
class Fist(pg.sprite.Sprite):
"""moves a clenched fist on the screen, following the mouse"""
def __init__(self) -> None:
pg.sprite.Sprite.__init__(self)
self.image, self.rect = load_image("fist.png", -1)
self.fist_offset = (-235, -80)
self.punching = False
def update(self):
"""move the fist based on the mouse position"""
pos = pg.mouse.get_pos()
self.rect.topleft = pos
self.rect.move_ip(self.fist_offset)
if self.punching:
self.rect.move_ip(15, 25)
def punch(self, target):
"""returns true if the fist collides with the target"""
if not self.punching:
self.punching = True
hitbox = self.rect.inflate(-5, -5)
return hitbox.colliderect(target.rect)
def unpunch(self):
"""called to pull the fist back"""
self.punching = False
class Chimp(pg.sprite.Sprite):
"""moves a monkey critter across the screen. it can spin the
monkey when it is punched."""
def __init__(self) -> None:
pg.sprite.Sprite.__init__(self) # call Sprite initializer
self.image, self.rect = load_image("chimp.png", -1, 4)
screen = pg.display.get_surface()
self.area = screen.get_rect()
self.rect.topleft = 10, 90
self.move = 18
self.dizzy = False
def update(self):
if self.dizzy:
self._spin()
else:
self._walk()
def _walk(self):
"""move the monkey across the screen, and turn at the ends"""
newpos = self.rect.move((self.move, 0))
# Change moving direction and flip the image if object outside the screen.
if not self.area.contains(newpos):
if self.rect.left < self.area.left \
or self.rect.right > self.area.right:
self.move = -self.move
newpos = self.rect.move((self.move, 0))
self.image = pg.transform.flip(
self.image, True, False)
self.rect = newpos
def _spin(self):
"""spin the monkey image"""
center = self.rect.center
self.dizzy = self.dizzy + 12
if self.dizzy >= 360:
self.dizzy = False
self.image = self.original
else:
rotate = pg.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 = True
self.original = self.image
# Initialize Everything
pg.init()
screen = pg.display.set_mode((1280, 480), pg.SCALED)
pg.display.set_caption("Punching the Chimpanzee")
pg.mouse.set_visible(False)
# Create The Background
background = pg.Surface(screen.get_size())
background = background.convert()
background.fill((170, 238, 187))
# Put Text On The Background, Centered
if pg.font:
font = pg.font.Font(None, 64)
text = font.render("Pummel the Chimp!",
True, (10, 10, 10))
textpos = text.get_rect(
centerx=background.get_width()/2, y=10)
background.blit(text, textpos)
# Display The Background
screen.blit(background, (0, 0))
pg.display.update()
# Initialize Game Objects
whiff_sound = load_sound("whiff.wav")
punch_sound = load_sound("punch.wav")
chimp = Chimp()
fist = Fist()
allsprites = pg.sprite.RenderPlain((chimp, fist))
clock = pg.time.Clock()
# Main Loop
going = True
while going:
# Set maximum frame per second
clock.tick(60)
# Handle Input Events
for event in pg.event.get():
if event.type == pg.QUIT: # quit
going = False
elif event.type == pg.KEYDOWN \
and event.key == pg.K_ESCAPE: # escape
going = False
elif event.type == pg.MOUSEBUTTONDOWN:
if fist.punch(chimp):
punch_sound.play() # punch
chimp.punched()
else:
whiff_sound.play() # miss
elif event.type == pg.MOUSEBUTTONUP:
fist.unpunch()
allsprites.update()
# Draw Everything
screen.blit(background, (0, 0))
allsprites.draw(screen)
pg.display.update()
pg.quit()
# Game Over