/
main.py
346 lines (288 loc) · 12.4 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import pygame, sys, os, random, noise # import pygame and sys
#game Files
clock = pygame.time.Clock() # set up the clock
from pygame.locals import *
pygame.init() # initiates pygame
# Handles Sound for the game
pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.mixer.set_num_channels(64)
# Sets the Caption of the window
pygame.display.set_caption('Icurus World') # set the window name
# Window Size
WINDOW_SIZE = (800,600)
# initiate screen
screen = pygame.display.set_mode(WINDOW_SIZE,0,32)
# Used as the surface for rendering, which is scaled
display = pygame.Surface((400,300))
import constants
#Sets the Chunk size of the world default is set to 8
CHUNK_SIZE = 20
CHUNK_WIDTH = 20
CHUNK_HEIGHT = 20
#Font for the text in game. Set to something different later on
font = pygame.font.SysFont(None, 20)
#Sets the rendering for the text to be displayed
def draw_text(text, font, color, surface, x, y):
textobj = font.render(text, 1, color)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
#Main Menu
def main_menu():
while True:
screen.fill((0,0,0))
draw_text('Icurus World', font, (255, 255, 255), screen, 20, 20)
mx, my = pygame.mouse.get_pos()
button_1 = screen.blit(constants.BUTTON_START, (50,100))
button_2 = screen.blit(constants.BUTTON_OPTIONS, (50, 200))
#button_2 = pygame.Rect(50, 200, 200, 30)
if button_1.collidepoint((mx, my)):
if click:
game()
if button_2.collidepoint((mx, my)):
if click:
options()
#pygame.draw.rect(screen, (255, 0, 0), button_1)
#draw_text('Start Game', font, (255, 255, 255), screen, (50+(100/2)), (100+(50/2)))
#pygame.draw.rect(screen, (255, 0, 0), button_2)
#draw_text('Options', font, (255, 255, 255), screen, (50+(100/2)), (200+(50/2)))
click = False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
click = True
pygame.display.update()
clock.tick(60)
#Generates the Chunks in the world. I need to find a better method for this other than noise.
def generate_chunk(x,y):
chunk_data = []
for y_pos in range(CHUNK_SIZE):
chunk_data.append([])
for x_pos in range(CHUNK_SIZE):
target_x = x * CHUNK_SIZE + x_pos
target_y = y * CHUNK_SIZE + y_pos
tile_type = 0 # nothing
height = int(noise.pnoise1(target_x * 0.1, repeat=9999999) * 5)
if target_y > 8 - height:
tile_type = 2 # dirt
elif target_y == 8 - height :
tile_type = 1 # grass
elif target_y == 8 - height - 1:
if random.randint(1,5) == 1:
tile_type = 3 # plant
if target_y == 3 - height - 1:
if random.randint(1,10) == 1:
tile_type = 11 # tree
chunk_data[y_pos].append([[target_x,target_y],tile_type, [x_pos,y_pos]])
chunk_data = load_map(chunk_data)
return chunk_data
#This checks the tile next to dirt, if it is air than it will add the proper corner blocks.
def load_map(chunk_data):
for y in range(CHUNK_SIZE):
for x in range(CHUNK_SIZE):
if chunk_data[y][x][1] == 1 and x + 1 < CHUNK_SIZE:
if chunk_data[y][x + 1][1] == 0 and x > 1:
chunk_data[y][x][1] = 5
elif chunk_data[y][x - 1][1] == 0 and x > 1:
chunk_data[y][x][1] = 4
return chunk_data
#Animation
global animation_frames
animation_frames = {}
#sets up the animation of the player.
def load_animation(path,frame_durations):
global animation_frames
animation_name = path.split('/')[-1]
animation_frame_data = []
n = 0
for frame in frame_durations:
animation_frame_id = animation_name + '_' + str(n)
img_loc = path + '/' + animation_frame_id + '.png'
# player_animations/idle/idle_0.png
animation_image = pygame.image.load(img_loc)
animation_image.set_colorkey((255,255,255))
animation_frames[animation_frame_id] = animation_image.copy()
for i in range(frame):
animation_frame_data.append(animation_frame_id)
n += 1
return animation_frame_data
def change_action(action_var,frame,new_value):
if action_var != new_value:
action_var = new_value
frame = 0
return action_var,frame
animation_database = {}
animation_database['run'] = load_animation('images/PlayerSprite/run',[7,7])
animation_database['idle'] = load_animation('images/PlayerSprite/idle',[7,7,40])
game_map = {}
#Load Tile Set. default tile set for now. biomesbiomes will be added later
tleft = pygame.image.load('images/TileMap/DirtGrass/t-left.png').convert_alpha() # Top Left Tile Map
grass = pygame.image.load('images/TileMap/DirtGrass/t-center.png').convert_alpha() # Top Center Tile Map
tright = pygame.image.load('images/TileMap/DirtGrass/t-right.png').convert_alpha() # Top Right Tile Map
cleft = pygame.image.load('images/TileMap/DirtGrass/c-left.png').convert_alpha() # Center Left Tile Map
cright = pygame.image.load('images/TileMap/DirtGrass/c-right.png').convert_alpha() # Center Right Tile Map
dirt = pygame.image.load('images/TileMap/DirtGrass/center.png').convert_alpha() # Center This is the main Dirt Tile as of now
bleft = pygame.image.load('images/TileMap/DirtGrass/b-left.png').convert_alpha() # Bottom Left Tile Map
bcenter = pygame.image.load('images/TileMap/DirtGrass/b-center.png').convert_alpha() # Bottom Center Tile Map
bright = pygame.image.load('images/TileMap/DirtGrass/b-right.png').convert_alpha() # Bottom Right Tile Map
plant = pygame.image.load('images/WorldObjects/plant.png').convert_alpha() # Plant Image
plant.set_colorkey((255,255,255))
tree = pygame.image.load('images/WorldObjects/tree.png').convert_alpha() # Tree Image
tile_index = {
1:grass, #Main Block for Grass
2:dirt, #Main Block for Dirt
3:plant, #Horrible Grass Tile, Will remake it
4:tleft, #Top left
5:tright, #Top Right
6:cleft, #Center left
7:cright, #Center Right
8:bleft, #Bottom left
9:bcenter, #Bottom Center
10:bright, #Bottom Right
11:tree #Tree Image
}
#handles the sound for the world.
#jump_sound = pygame.mixer.Sound('Sounds/WorldObjects/jump.wav')
grass_sounds = [pygame.mixer.Sound('Sounds/WorldObjects/grass_0.wav'), pygame.mixer.Sound('Sounds/WorldObjects/grass_1.wav')]
grass_sounds[0].set_volume(1.0)
grass_sounds[1].set_volume(1.0)
pygame.mixer.music.load('Sounds/WorldObjects/grass_0.wav')
pygame.mixer.music.play(-1)
# Parallax background
background_objects = [[0.25,[120,10,70,400]],[0.25,[280,30,40,400]],[0.5,[30,40,40,400]],[0.5,[130,90,100,400]],[0.1,[300,80,120,400]]]
#handles collision
def collision_test(rect,tiles):
hit_list = []
for tile in tiles:
if rect.colliderect(tile):
hit_list.append(tile)
return hit_list
#handles player move and collosion
def move(rect,movement,tiles):
collision_types = {'top':False,'bottom':False,'right':False,'left':False}
rect.x += movement[0]
hit_list = collision_test(rect,tiles)
for tile in hit_list:
if movement[0] > 0:
rect.right = tile.left
collision_types['right'] = True
elif movement[0] < 0:
rect.left = tile.right
collision_types['left'] = True
rect.y += movement[1]
hit_list = collision_test(rect,tiles)
for tile in hit_list:
if movement[1] > 0:
rect.bottom = tile.top
collision_types['bottom'] = True
elif movement[1] < 0:
rect.top = tile.bottom
collision_types['top'] = True
return rect, collision_types
#Main game
def game():
running = True
moving_right = False
moving_left = False
vertical_momentum = 0
air_timer = 0
grass_sound_timer = 0
player_rect = pygame.Rect(100,100,18,27)
player_flip = False
player_action = 'idle'
player_frame = 0
tile_rects = []
true_scroll = [0,0]
if grass_sound_timer > 0:
grass_sound_timer -= 1
while running:
display.fill((146,244,255))
true_scroll[0] += (player_rect.x-true_scroll[0]-152)/20
true_scroll[1] += (player_rect.y-true_scroll[1]-106)/20
scroll = true_scroll.copy()
scroll[0] = int(scroll[0])
scroll[1] = int(scroll[1])
pygame.draw.rect(display,(7,80,75),pygame.Rect(0,120,300,80))
for background_object in background_objects:
obj_rect = pygame.Rect(background_object[1][0]-scroll[0]*background_object[0],background_object[1][1]-scroll[1]*background_object[0],background_object[1][2],background_object[1][3])
if background_object[0] == 0.5:
pygame.draw.rect(display,(20,170,150),obj_rect)
else:
pygame.draw.rect(display,(15,76,73),obj_rect)
for y in range(3):
for x in range(4):
target_x = x - 1 + int(round(scroll[0]/(CHUNK_SIZE*16)))
target_y = y - 1 + int(round(scroll[1]/(CHUNK_SIZE*16)))
target_chunk = str(target_x) + ';' + str(target_y)
if target_chunk not in game_map:
game_map[target_chunk] = generate_chunk(target_x,target_y)
for row in game_map[target_chunk]:
for tile in row:
if tile[1] != 0:
display.blit(tile_index[tile[1]],(tile[0][0]*16-scroll[0],tile[0][1]*16-scroll[1]))
if tile[1] in [1,2,4,5,6,7,8,9,10]: #add collosion tiles here
tile_rects.append(pygame.Rect(tile[0][0]*16,tile[0][1]*16,16,16))
player_movement = [0,0]
if moving_right == True:
player_movement[0] += 2
if moving_left == True:
player_movement[0] -= 2
player_movement[1] += vertical_momentum
vertical_momentum += 0.2
if vertical_momentum > 3:
vertical_momentum = 3
if player_movement[0] == 0:
player_action,player_frame = change_action(player_action,player_frame,'idle')
if player_movement[0] > 0:
player_flip = False
player_action,player_frame = change_action(player_action,player_frame,'run')
if player_movement[0] < 0:
player_flip = True
player_action,player_frame = change_action(player_action,player_frame,'run')
player_rect, collisions = move(player_rect, player_movement, tile_rects)
if collisions['bottom']:
vertical_momentum = 0
air_timer = 0
if player_movement[0] != 0:
if grass_sound_timer == 0:
grass_sound_timer = 30
random.choice(grass_sounds).play()
else:
air_timer += 1
player_frame += 1
if player_frame >= len(animation_database[player_action]):
player_frame = 0
player_img_id = animation_database[player_action][player_frame]
player_img = animation_frames[player_img_id]
display.blit(pygame.transform.flip(player_img,player_flip,False),(player_rect.x-scroll[0],player_rect.y-scroll[1]))
for event in pygame.event.get(): # event loop
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_d:
moving_right = True
if event.key == K_a:
moving_left = True
if event.key == K_SPACE:
if air_timer < 6:
vertical_momentum = -5
if event.type == KEYUP:
if event.key == K_d:
moving_right = False
if event.key == K_a:
moving_left = False
tile_rects = []
screen.blit(pygame.transform.scale(display,WINDOW_SIZE),(0,0))
pygame.display.update()
pygame.display.flip()
clock.tick(60)
main_menu()
game()