-
Notifications
You must be signed in to change notification settings - Fork 0
/
catch_the_monster.py
123 lines (97 loc) · 3.26 KB
/
catch_the_monster.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
import pygame
class Character(object):
def __init__(self,x,y,path):
self.x = x
self.y = y
self.path = path
self.image = pygame.image.load(self.path).convert_alpha()
print type(self.image)
self.width = self.image.get_width()
def render(self,screen):
screen.blit(self.image, (x, y))
class Hero(Character):
def __init__(self,x,y,path):
Character.__init__(self,x,y,path)
self.speed_x = 0
self.speed_y = 0
def update(self, width, height):
self.x += self.speed_x
self.y += self.speed_y
if self.x + self.width>= width:
self.x=0
if self.y + self.width >= height:
self.y=0
if self.x - self.width < 0:
self.speed_x=5
if self.y - self.width < 0:
self.speed_y=5
class Monster(Character):
def __init__(self,x,y,path):
Character.__init__(self,x,y,path)
self.speed_x = -2
self.speed_y = 0
def update(self, width, height):
self.x += self.speed_x
self.y += self.speed_y
if self.x + self.width >= width:
self.x=0
if self.y + self.width >= height:
self.y=0
if self.x < 0:
self.x = width - self.width
if self.y < 0:
self.y = height - self.width
def main():
# declare the size of the canvas
width = 512
height = 480
# blue_color = (97, 159, 182)
# initialize the pygame framework
pygame.init()
# create screen
screen = pygame.display.set_mode((width, height))
# set window caption
pygame.display.set_caption('Catch the Monster')
# create a clock
clock = pygame.time.Clock()
################################
# PUT INITIALIZATION CODE HERE #
################################
background = pygame.image.load('images/background.png').convert_alpha()
hero = Hero(255,255,'images/hero.png')
monster = Monster(30,100,'images/monster.png')
chars = [
hero,
monster
]
# game loop
stop_game = False
while not stop_game:
# look through user events fired
for event in pygame.event.get():
################################
# PUT EVENT HANDLING CODE HERE #
################################
if event.type == pygame.QUIT:
# if they closed the window, set stop_game to True
# to exit the main loop
stop_game = True
#######################################
# PUT LOGIC TO UPDATE GAME STATE HERE #
#######################################
screen.blit(hero.image, (hero.x, hero.y))
screen.blit(monster.image, (monster.x, monster.y))
################################
# PUT CUSTOM DISPLAY CODE HERE #
################################
screen.blit(background, (0, 0))
screen.blit(hero.image, (hero.x, hero.y))
screen.blit(monster.image, (monster.x, monster.y))
# update the canvas display with the currently drawn frame
pygame.display.update()
# tick the clock to enforce a max framerate
clock.tick(60)
# quit pygame properly to clean up resources
pygame.quit()
if __name__ == '__main__':
main()