-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
197 lines (157 loc) · 5.97 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""
Copyright 2020 Parth Agrawal
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import pygame
from pygame.locals import (
RLEACCEL,
K_LEFT,
K_RIGHT,
K_SPACE
)
import random
pygame.init()
class Player(pygame.sprite.Sprite):
def load_image(self, image):
curr_image = pygame.image.load(image)
curr_image.set_colorkey((0, 0, 0), RLEACCEL)
curr_image = pygame.transform.scale(curr_image, (100, 100))
return curr_image
def __init__(self):
super(Player, self).__init__()
self.images_running = []
self.images_jumping = []
self.ir = 0
while self.ir < 10:
self.ir_num = "Player/Run__00" + str(self.ir) + ".png"
self.images_running.append(self.load_image(self.ir_num))
self.ir = self.ir + 1
self.ij = 0
while self.ij < 10:
self.ij_num = "Player/Jump__00" + str(self.ij) + ".png"
self.images_jumping.append(self.load_image(self.ij_num))
self.ij = self.ij + 1
self.index_running = 0
self.image_running = self.images_running[self.index_running]
self.index_jumping = 0
self.image_jumping = self.images_jumping[self.index_jumping]
self.curr_image = self.image_running
self.rect = pygame.Rect(5, 350, 100, 100)
def update(self, pressed_keys):
self.index_running = self.index_running + 1
if self.index_running >= len(self.images_running):
self.index_running = 0
self.image_running = self.images_running[self.index_running]
self.curr_image = self.image_running
if pressed_keys[K_LEFT]:
self.rect.move_ip(-5, 0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(5, 0)
if pressed_keys[K_SPACE]:
self.index_jumping = self.index_jumping + 1
if self.index_jumping >= len(self.images_jumping):
self.index_jumping = 0
self.image_jumping = self.images_jumping[self.index_jumping]
self.curr_image = self.image_jumping
self.rect.move_ip(0, -5)
if not pressed_keys[K_SPACE]:
if self.rect.top < 350:
self.rect.move_ip(0, 10)
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > 750:
self.rect.right = 750
class Zombie(pygame.sprite.Sprite):
def load_image(self, image):
curr_image_zombie = pygame.image.load(image)
curr_image_zombie.set_colorkey((0, 0, 0), RLEACCEL)
curr_image_zombie = pygame.transform.scale(curr_image_zombie, (100, 100))
curr_image_zombie = pygame.transform.flip(curr_image_zombie, True, False)
return curr_image_zombie
def __init__(self):
super(Zombie, self).__init__()
self.images_walking = []
self.iw = 1
while self.iw < 11:
self.iw_str = "Zombie/Walk (" + str(self.iw) + ").png"
self.images_walking.append(self.load_image(self.iw_str))
self.iw = self.iw + 1
self.index_walking = 0
self.image_walking = self.images_walking[self.index_walking]
self.curr_image = self.image_walking
self.rect = pygame.Rect(650, 350, 100, 100)
def update(self):
self.index_walking = self.index_walking + 1
if self.index_walking >= len(self.images_walking):
self.index_walking = 0
self.image_walking = self.images_walking[self.index_walking]
self.curr_image = self.image_walking
self.rect.move_ip(-2.5, 0)
if self.rect.right < 0:
self.kill()
class Cloud(pygame.sprite.Sprite):
def __init__(self):
super(Cloud, self).__init__()
self.surf = pygame.image.load("cloud.png").convert()
self.surf.set_colorkey((0, 0, 0), RLEACCEL)
self.rect = self.surf.get_rect(
center = (
random.randint(750 + 20, 750 + 100),
random.randint(0, 200)
)
)
def update(self):
self.rect.move_ip(-5, 0)
if self.rect.right < 0:
self.kill()
screen = pygame.display.set_mode([750, 500])
bg = pygame.image.load("Background.png")
bg = pygame.transform.scale(bg, (750, 500))
bg_rect = bg.get_rect()
ADDCLOUD = pygame.USEREVENT + 1
pygame.time.set_timer(ADDCLOUD, 2000)
ADDZOMBIE = pygame.USEREVENT + 2
pygame.time.set_timer(ADDZOMBIE, random.randint(3000, 5000))
player = Player()
clouds = pygame.sprite.Group()
zombies = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == ADDCLOUD:
new_cloud = Cloud()
clouds.add(new_cloud)
all_sprites.add(new_cloud)
elif event.type == ADDZOMBIE:
new_zombie = Zombie()
zombies.add(new_zombie)
all_sprites.add(new_zombie)
clouds.update()
zombies.update()
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)
screen.blit(bg, bg_rect)
for entity in all_sprites:
try:
screen.blit(entity.surf, entity.rect)
except AttributeError:
screen.blit(entity.curr_image, entity.rect)
if pygame.sprite.spritecollideany(player, zombies):
player.kill()
running = False
pygame.display.flip()
clock.tick(30)
pygame.quit()