forked from sjbrown/writing_games_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
book_chapter1.example01.py
83 lines (69 loc) · 2.51 KB
/
book_chapter1.example01.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
import time
import pygame
import pygame.constants as c
score = 0
screenDimensions = pygame.Rect((0,0,400,100))
black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
red = (255,0,0)
class Monkey(pygame.sprite.Sprite):
def __init__(self):
self.stunTimeout = None
self.velocity = 2
super(Monkey, self).__init__()
self.image = pygame.Surface((60,60))
self.rect = self.image.get_rect()
self.render(blue)
def render(self, color):
'''draw onto self.image the face of a monkey in the specified color'''
self.image.fill(color)
pygame.draw.circle(self.image, white, (10,10), 10, 2)
pygame.draw.circle(self.image, white, (50,10), 10, 2)
pygame.draw.circle(self.image, white, (30,60), 20, 2)
def attempt_punch(self, pos):
'''If the given position (pos) is inside the monkey's rect, the monkey
has been "punched". A successful punch will stun the monkey and increment
the global score. The monkey cannot be punched if he is already stunned
'''
if self.stunTimeout:
return # already stunned
if self.rect.collidepoint(pos):
# Argh! The punch intersected with my face!
self.stunTimeout = time.time() + 2 # 2 seconds from now
global score
score += 1
self.render(red)
def update(self):
if self.stunTimeout:
# If stunned, the monkey doesn't move
if time.time() > self.stunTimeout:
self.stunTimeout = None
self.render(blue)
else:
# Move the monkey
self.rect.x += self.velocity
# Don't let the monkey run past the edge of the viewable area
if self.rect.right > screenDimensions.right:
self.velocity = -2
elif self.rect.left < screenDimensions.left:
self.velocity = 2
def main():
pygame.init()
clock = pygame.time.Clock()
displayImg = pygame.display.set_mode(screenDimensions.size)
monkey = Monkey()
while True:
for event in pygame.event.get():
if event.type == c.QUIT:
return
elif event.type == c.MOUSEBUTTONDOWN:
monkey.attempt_punch(event.pos)
clock.tick(60) # aim for 60 FPS
monkey.update()
displayImg.fill(black)
displayImg.blit(monkey.image, monkey.rect)
pygame.display.flip()
if __name__ == '__main__':
main()
print 'Your score was', score