From cedb02e77e9b3f0f2f20e312c99a0138c7713cac Mon Sep 17 00:00:00 2001 From: "Waylon S. Walker" Date: Sun, 19 Mar 2023 13:00:17 -0500 Subject: [PATCH] clocks --- cave_survival/config.py | 2 + cave_survival/daylightcycle.py | 37 ++++++++++++++ cave_survival/game.py | 91 +++------------------------------- cave_survival/map.py | 7 +-- cave_survival/player.py | 63 +++++++++++++++++++++-- 5 files changed, 110 insertions(+), 90 deletions(-) create mode 100644 cave_survival/daylightcycle.py diff --git a/cave_survival/config.py b/cave_survival/config.py index 2418ef9..8a51efa 100644 --- a/cave_survival/config.py +++ b/cave_survival/config.py @@ -4,6 +4,8 @@ import pydantic class Config(pydantic.BaseModel): screen_width: int = 800 screen_height: int = 600 + fps: int = 60 + day_length: int = 60 * 1000 BLACK: tuple[int] = (0, 0, 0) WHITE: tuple[int] = (255, 255, 255) diff --git a/cave_survival/daylightcycle.py b/cave_survival/daylightcycle.py new file mode 100644 index 0000000..16a9604 --- /dev/null +++ b/cave_survival/daylightcycle.py @@ -0,0 +1,37 @@ +import pygame + +from cave_survival.config import config +from cave_survival.console import console + + +class DayLightCycle: + def __init__(self, game): + self.game = game + self.console = console + self.day_length = config.day_length + self.day_timer = 0 + + @property + def game_time(self): + return pygame.time.get_ticks() / self.day_length + + @property + def days(self): + return int(self.game_time) + + @property + def time(self): + return self.game_time - self.days + + def draw(self): + + pygame.draw.rect( + self.game.screen, + config.GREY, + ( + 0, + 0, + self.time * config.screen_width, + 10, + ), + ) diff --git a/cave_survival/game.py b/cave_survival/game.py index ae16bf1..d13788f 100644 --- a/cave_survival/game.py +++ b/cave_survival/game.py @@ -3,6 +3,7 @@ from pydantic import BaseModel from cave_survival.config import config from cave_survival.console import console +from cave_survival.daylightcycle import DayLightCycle from cave_survival.map import Map, Point from cave_survival.player import Player @@ -17,11 +18,10 @@ class Game: (config.screen_width, config.screen_height) ) self.clock = pygame.time.Clock() - self.player = Player() - self.day_length = 60 * 60 # 60 seconds * 60 frames - self.day_timer = 0 + self.player = Player(game=self) + self.map = Map(self) + self.daylightcycle = DayLightCycle(self) self.inventory = ["sword", "pickaxe", "axe"] - self.map = Map() self.inventory_surface = pygame.Surface((len(self.inventory) * 32, 32)) self.inventory_surface.fill(config.GREY) @@ -36,76 +36,12 @@ class Game: self.running = False # Update the day/night cycle - self.day_timer += 1 - if self.day_timer > self.day_length: - self.day_timer = 0 - - # Update the self.player - if self.keys[pygame.K_w]: - self.player.y -= self.player.speed - if self.keys[pygame.K_s]: - self.player.y += self.player.speed - if self.keys[pygame.K_a]: - self.player.x -= self.player.speed - if self.keys[pygame.K_d]: - self.player.x += self.player.speed - - # Check for self.player collisions with the walls and the black tiles on the map - if self.player.x < 0: - self.player.x = 0 - if self.player.x > config.screen_width - self.player.width: - self.player.x = config.screen_width - self.player.width - if self.player.y < 0: - self.player.y = 0 - if self.player.y > config.screen_height - self.player.height: - self.player.y = config.screen_height - self.player.height - - self.player.pos = pygame.math.Vector2(self.player.x, self.player.y) - - if self.map.point_check_collision(self.player.pos.x, self.player.pos.y): - - start_pos = pygame.math.Vector2(self.player.x_last, self.player.y_last) - end_pos = pygame.math.Vector2(self.player.x, self.player.y) - movement_vector = end_pos - start_pos - try: - movement_direction = movement_vector.normalize() - except: - end_pos = pygame.math.Vector2( - self.player.x + 128, self.player.y + 128 - ) - movement_vector = end_pos - start_pos - movement_direction = movement_vector.normalize() - movement_speed = 0.05 - - self.player.x = self.player.x_last - self.player.y = self.player.y_last - - self.player.pos = pygame.math.Vector2(start_pos) - - while self.map.point_check_collision( - self.player.pos.x, self.player.pos.y - ): - self.player.pos += movement_speed * movement_direction - self.player.x = self.player.pos.x - self.player.y = self.player.pos.y - - self.player.pos -= movement_speed * movement_direction - self.player.x = self.player.pos.x - self.player.y = self.player.pos.y - - self.player.x_last = self.player.x - self.player.y_last = self.player.y # Draw the screen self.screen.fill(config.BLACK) - # Draw the map - self.map.offset = Point(x=self.player.x, y=self.player.y) - self.map.offset = Point(x=0, y=0) - self.map.draw(self.screen) - - # Draw the self.player - self.player.draw(self.screen, offset=self.map.offset) + self.map.draw() + self.player.draw() # Draw the inventory for i, item_name in enumerate(self.inventory): @@ -119,19 +55,8 @@ class Game: config.screen_height - self.inventory_surface.get_height(), ), ) - - # Draw the day/night cycle - pygame.draw.rect( - self.screen, - config.GREY, - (0, 0, self.day_timer / self.day_length * config.screen_width, 10), - ) - - # Update the display + self.daylightcycle.draw() pygame.display.flip() + self.clock.tick(config.fps) - # Limit the framerate - self.clock.tick(60) - - # Quit pygame pygame.quit() diff --git a/cave_survival/map.py b/cave_survival/map.py index 590502d..48a4f17 100644 --- a/cave_survival/map.py +++ b/cave_survival/map.py @@ -15,7 +15,8 @@ class Point(pydantic.BaseModel): class Map: - def __init__(self): + def __init__(self, game): + self.game = game self.grass = pygame.image.load("grass.webp").convert_alpha() self.rock = pygame.image.load("rock.jpg").convert_alpha() self.dirt = pygame.image.load("dirt.jpg").convert_alpha() @@ -45,8 +46,8 @@ class Map: value = (value + 1) / 2 * 255 return value - def draw(self, screen): - screen.blit( + def draw(self): + self.game.screen.blit( pygame.transform.scale(self.surf, (self.screen_width, self.screen_height)), (0, 0), ) diff --git a/cave_survival/player.py b/cave_survival/player.py index 23b9abd..aae910b 100644 --- a/cave_survival/player.py +++ b/cave_survival/player.py @@ -5,7 +5,8 @@ from cave_survival.console import console class Player: - def __init__(self): + def __init__(self, game): + self.game = game self.width = 16 self.height = 16 self.x = config.screen_width / 2 @@ -21,8 +22,62 @@ class Player: ) self.hitbox_surface.set_alpha(0) - def draw(self, surface, offset): - surface.blit( + def move(self): + # Update the self + if self.game.keys[pygame.K_w]: + self.y -= self.speed + if self.game.keys[pygame.K_s]: + self.y += self.speed + if self.game.keys[pygame.K_a]: + self.x -= self.speed + if self.game.keys[pygame.K_d]: + self.x += self.speed + + # Check for self collisions with the walls and the black tiles on the map + if self.x < 0: + self.x = 0 + if self.x > config.screen_width - self.width: + self.x = config.screen_width - self.width + if self.y < 0: + self.y = 0 + if self.y > config.screen_height - self.height: + self.y = config.screen_height - self.height + + self.pos = pygame.math.Vector2(self.x, self.y) + + if self.game.map.point_check_collision(self.pos.x, self.pos.y): + + start_pos = pygame.math.Vector2(self.x_last, self.y_last) + end_pos = pygame.math.Vector2(self.x, self.y) + movement_vector = end_pos - start_pos + try: + movement_direction = movement_vector.normalize() + except: + end_pos = pygame.math.Vector2(self.x + 128, self.y + 128) + movement_vector = end_pos - start_pos + movement_direction = movement_vector.normalize() + movement_speed = 0.05 + + self.x = self.x_last + self.y = self.y_last + + self.pos = pygame.math.Vector2(start_pos) + + while self.game.map.point_check_collision(self.pos.x, self.pos.y): + self.pos += movement_speed * movement_direction + self.x = self.pos.x + self.y = self.pos.y + + self.pos -= movement_speed * movement_direction + self.x = self.pos.x + self.y = self.pos.y + + self.x_last = self.x + self.y_last = self.y + + def draw(self): + self.move() + self.game.screen.blit( pygame.transform.scale(self.image, (16, 16)), - (self.x - 8 - offset.x, self.y - 8 - offset.y), + (self.x - 8 - self.game.map.offset.x, self.y - 8 - self.game.map.offset.y), )