-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
262 lines (211 loc) · 8.08 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import pygame
pygame.init()
clock = pygame.time.Clock()
fps = 60
moon = False
screen_width = 800
screen_height = 650
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Platformer')
#define game variables
tile_size = 25
#load images
sun_img = pygame.image.load('rsc/sun.png')
sun_img = pygame.transform.scale(sun_img, (100, 100))
bg_img = pygame.image.load('rsc/sky.jpg')
bg_img = pygame.transform.scale(bg_img, (screen_width, screen_height))
pygame.mixer.music.load("rsc\MrScruffKalimbaNinjaTuna.mp3")
pygame.mixer.music.play()
pygame.mixer.music.set_volume(0.1)
class Player():
def __init__(self, x, y):
self.images_right = []
self.images_left = []
self.index = 0
self.counter = 0
for num in range(1, 7):
img_right = pygame.image.load(f'rsc/player{num}.png')
img_right = pygame.transform.scale(img_right, (30, 60))
img_left = pygame.transform.flip(img_right, True, False)
self.images_right.append(img_right)
self.images_left.append(img_left)
self.image = self.images_right[self.index]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.width = self.image.get_width()
self.height = self.image.get_height()
self.vel_y = 0
self.jumped = False
self.direction = 0
self.two_jumps = []
def update(self):
dx = 0
dy = 0
walk_cooldown = 5
#get keypresses
key = pygame.key.get_pressed()
#2-jumps system
if len(self.two_jumps)==0:
if key[pygame.K_SPACE] and self.jumped == False:
self.vel_y = -15
self.jumped = True
self.two_jumps.append(1)
if key[pygame.K_SPACE] == False:
self.jumped = False
# if len(self.two_jumps)==1:
# if key[pygame.K_SPACE] and self.jumped == False:
# self.vel_y = -15
# self.jumped = True
# self.two_jumps.append(1)
# if key[pygame.K_SPACE] == False:
# self.jumped = False
# if len(self.two_jumps)==1:
# if self.rect.y == screen_height-130:
# self.two_jumps.clear()
if key[pygame.K_LEFT]:
dx -= 5
self.counter += 1
self.direction = -1
if key[pygame.K_RIGHT]:
dx += 5
self.counter += 1
self.direction = 1
if key[pygame.K_LEFT] == False and key[pygame.K_RIGHT] == False:
self.counter = 0
self.index = 0
if self.direction == 1:
self.image = self.images_right[self.index]
if self.direction == -1:
self.image = self.images_left[self.index]
#handle animation
if self.counter > walk_cooldown:
self.counter = 0
self.index += 1
if self.index >= len(self.images_right):
self.index = 0
if self.direction == 1:
self.image = self.images_right[self.index]
if self.direction == -1:
self.image = self.images_left[self.index]
#add gravity
self.vel_y += 1
if self.vel_y > 10:
self.vel_y = 10
dy += self.vel_y
#check for collision
for tile in world.tile_list:
#check for collision in x direction
if tile[1].colliderect(self.rect.x + dx, self.rect.y, self.width, self.height):
dx = 0
#check for collision in y direction
if tile[1].colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
#check if below the ground i.e. jumping
if self.vel_y < 0:
dy = tile[1].bottom - self.rect.top
self.vel_y = 0
#check if above the ground i.e. falling
elif self.vel_y >= 0:
dy = tile[1].top - self.rect.bottom
self.vel_y = 0
if len(self.two_jumps)==1:
self.two_jumps.clear()
#update player coordinates
self.rect.x += dx
self.rect.y += dy
if self.rect.bottom > screen_height:
self.rect.bottom = screen_height
dy = 0
if self.rect.y < 0:
self.rect.y = 0
self.vel_y = 1
if self.rect.x > (screen_width-30):
self.rect.x = screen_width-30
if self.rect.x < 0:
self.rect.x = 0
if self.rect.x < 100 and self.rect.y < 100 and not moon:
self.rect.x = 5
self.rect.y = screen_height - 130
if self.rect.x < 100 and self.rect.y < 100 and moon:
pygame.quit()
#draw player onto screen
screen.blit(self.image, self.rect)
#pygame.draw.rect(screen, (255, 255, 255), self.rect, 2)
class World():
def __init__(self, data):
self.tile_list = []
#load images
block1_img = pygame.image.load('rsc/block1.png')
# grass_img = pygame.image.load('img/grass.png')
row_count = 0
for row in data:
col_count = 0
for tile in row:
if tile == 1:
img = pygame.transform.scale(block1_img, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = (col_count * tile_size)
img_rect.y = (row_count * tile_size)
tile = (img, img_rect)
self.tile_list.append(tile)
# if tile == 2:
# img = pygame.transform.scale(grass_img, (tile_size, tile_size))
# img_rect = img.get_rect()
# img_rect.x = col_count * tile_size
# img_rect.y = row_count * tile_size
# tile = (img, img_rect)
# self.tile_list.append(tile)
col_count += 1
row_count += 1
def draw(self):
for tile in self.tile_list:
screen.blit(tile[0], tile[1])
#pygame.draw.rect(screen, (255, 255, 255), tile[1], 2)
world_data = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
player = Player(5, screen_height - 130)
world = World(world_data)
run = True
while run:
clock.tick(fps)
screen.blit(bg_img, (0, 0))
screen.blit(sun_img, (0, 0))
world.draw()
player.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if pygame.mixer.music.get_pos() > 350000 and not moon:
sun_img = pygame.image.load('rsc/moon.png')
sun_img = pygame.transform.scale(sun_img, (100, 100))
bg_img = pygame.image.load('rsc/night.jpg')
bg_img = pygame.transform.scale(bg_img, (screen_width, screen_height))
moon = True
pygame.display.update()
pygame.quit()