diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b7ff13a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_STORE \ No newline at end of file diff --git a/data/scripts/classes/player.py b/data/scripts/classes/player.py old mode 100644 new mode 100755 index 539a02b..8f56a0f --- a/data/scripts/classes/player.py +++ b/data/scripts/classes/player.py @@ -31,14 +31,31 @@ def __init__(self, start_pos, width, height, vel, jump_height, reach_distance=4) self.animation_counter = 0 self.animation_flip = False + self.death = False + + self.firstFall = True + + self.fallDistance = 0 + def move(self, tile_rects): self.rect, self.collision_types, self.hit_list = move(self.rect, tile_rects, self.movement) + print(self.fallDistance) + if self.collision_types['bottom'] and not self.jumping: self.movement[1] = 1 + if self.collision_types['bottom'] and self.fallDistance >= 5: + if not self.firstFall: + self.death = True + self.fallDistance = 0 + self.firstFall = False + + if not self.collision_types['bottom'] and not self.movement[1] <= 15: + self.fallDistance+= 1 + if not self.collision_types['bottom']: self.jumping = False self.movement[1] += GRAVITY_STRENGTH diff --git a/data/scripts/core_functions.py b/data/scripts/core_functions.py old mode 100644 new mode 100755 index 0d286bc..8e9f6c9 --- a/data/scripts/core_functions.py +++ b/data/scripts/core_functions.py @@ -1,3 +1,4 @@ +from data.variables import * import pygame import math @@ -57,3 +58,24 @@ def draw_rect_alpha(display, color, rect): shape_surf = pygame.Surface(pygame.Rect(rect).size, pygame.SRCALPHA) pygame.draw.rect(shape_surf, color, shape_surf.get_rect()) display.blit(shape_surf, rect) + +def draw_text(surf, text, size, x, y): + font = pygame.font.Font(None, size) + text_surface = font.render(text, True, (0,0,0)) + text_rect = text_surface.get_rect() + text_rect.midtop = (x, y) + surf.blit(text_surface, text_rect) + +def show_death_screen(screen): + WIDTH = WINDOW_SIZE[0] + HEIGHT = WINDOW_SIZE[1] + draw_text(screen, "You Died!", 64, WIDTH / 2, HEIGHT / 4) + draw_text(screen, "You Fell From High Place", 40, + WIDTH / 2, HEIGHT / 2) + draw_text(screen, "Noob", 30, WIDTH / 2, HEIGHT * 3 / 4) + pygame.display.flip() + while True: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + exit() diff --git a/data/variables.py b/data/variables.py old mode 100644 new mode 100755 index 41c7bdd..c780518 --- a/data/variables.py +++ b/data/variables.py @@ -1,6 +1,6 @@ import pygame -WINDOW_SIZE = (1920, 1080) # Measured in pixels +WINDOW_SIZE = (1280,720) # Measured in pixels GRAVITY_STRENGTH = 1 CHUNK_SIZE = 8 # Measured in blocks. I do not recommend changing this TILE_SIZE = 64 # Measured in pixels diff --git a/main.py b/main.py old mode 100644 new mode 100755 index a8f7f99..6eaa3e9 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ from data.scripts.classes.terrain import Terrain from data.scripts.classes.hotbar import Hotbar -from data.scripts.core_functions import draw, distance +from data.scripts.core_functions import draw, distance, show_death_screen, draw_text from data.variables import * @@ -16,12 +16,12 @@ screen = pygame.display.set_mode(WINDOW_SIZE) -player = Player((0, -200), TILE_SIZE-10, TILE_SIZE*2-10, 9, 13) +player = Player((0, -200), TILE_SIZE-10, TILE_SIZE+TILE_SIZE-10, 9, 13) hotbar = Hotbar() terrain = Terrain() terrain.generate_chunk(0, 0) -while __name__ == '__main__': +while True: clock.tick(60) pygame.display.set_caption(str(int(clock.get_fps()))) @@ -35,59 +35,70 @@ (player.rect.y - scroll[1] - (WINDOW_SIZE[1]/2 + player.height/2 - 100)) / SCROLL_STIFF ) - for event in pygame.event.get(): - if event.type == pygame.QUIT: - pygame.quit() - exit() - - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_a: - player.moving_left = True - if event.key == pygame.K_d: - player.moving_right = True - if event.key == pygame.K_SPACE or event.key == pygame.K_w: - player.jumping = True - - try: - if int(pygame.key.name(event.key)) != 0: - hotbar.selected_slot = int(pygame.key.name(event.key)) - except ValueError: - pass - - if event.type == pygame.KEYUP: - if event.key == pygame.K_d: - player.moving_right = False - if event.key == pygame.K_a: - player.moving_left = False - - if event.type == pygame.MOUSEBUTTONUP: - if event.button == 1: - player.break_block(terrain, hotbar) - if event.button == 3: - player.place_block(terrain, hotbar) - - if event.type == pygame.MOUSEBUTTONDOWN: - if event.button == 4: - if hotbar.selected_slot != 9: - hotbar.selected_slot += 1 - else: - hotbar.selected_slot = 1 - - if event.button == 5: - if hotbar.selected_slot != 1: - hotbar.selected_slot -= 1 - else: - hotbar.selected_slot = 9 - - for chunk in list(set([i.chunk for i in terrain.map])): - if distance(player.current_chunk, chunk) >= RENDER_DISTANCE: - terrain.unload_chunk(chunk) - - player.get_selected_block(terrain, mx, my) - - terrain.update(player) - player.update(terrain) - hotbar.update() - - draw(screen, terrain, player, hotbar) - + if not player.death: + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + exit() + + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_a: + player.moving_left = True + if event.key == pygame.K_d: + player.moving_right = True + if event.key == pygame.K_SPACE or event.key == pygame.K_w: + player.jumping = True + + try: + if int(pygame.key.name(event.key)) != 0: + hotbar.selected_slot = int(pygame.key.name(event.key)) + except ValueError: + pass + + if event.type == pygame.KEYUP: + if event.key == pygame.K_d: + player.moving_right = False + if event.key == pygame.K_a: + player.moving_left = False + + if event.type == pygame.MOUSEBUTTONUP: + if event.button == 1: + player.break_block(terrain, hotbar) + if event.button == 3: + player.place_block(terrain, hotbar) + + if event.type == pygame.MOUSEBUTTONDOWN: + if event.button == 4: + if hotbar.selected_slot != 9: + hotbar.selected_slot += 1 + else: + hotbar.selected_slot = 1 + + if event.button == 5: + if hotbar.selected_slot != 1: + hotbar.selected_slot -= 1 + else: + hotbar.selected_slot = 9 + + for chunk in list(set([i.chunk for i in terrain.map])): + if distance(player.current_chunk, chunk) >= RENDER_DISTANCE: + terrain.unload_chunk(chunk) + + player.get_selected_block(terrain, mx, my) + + terrain.update(player) + player.update(terrain) + hotbar.update() + + draw(screen, terrain, player, hotbar) + else: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + exit() + for chunk in list(set([i.chunk for i in terrain.map])): + if distance(player.current_chunk, chunk) >= RENDER_DISTANCE: + terrain.unload_chunk(chunk) + draw(screen,terrain) + show_death_screen(screen) \ No newline at end of file