clocks
This commit is contained in:
parent
80d7ebf7eb
commit
cedb02e77e
5 changed files with 110 additions and 90 deletions
|
|
@ -4,6 +4,8 @@ import pydantic
|
||||||
class Config(pydantic.BaseModel):
|
class Config(pydantic.BaseModel):
|
||||||
screen_width: int = 800
|
screen_width: int = 800
|
||||||
screen_height: int = 600
|
screen_height: int = 600
|
||||||
|
fps: int = 60
|
||||||
|
day_length: int = 60 * 1000
|
||||||
|
|
||||||
BLACK: tuple[int] = (0, 0, 0)
|
BLACK: tuple[int] = (0, 0, 0)
|
||||||
WHITE: tuple[int] = (255, 255, 255)
|
WHITE: tuple[int] = (255, 255, 255)
|
||||||
|
|
|
||||||
37
cave_survival/daylightcycle.py
Normal file
37
cave_survival/daylightcycle.py
Normal file
|
|
@ -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,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
@ -3,6 +3,7 @@ from pydantic import BaseModel
|
||||||
|
|
||||||
from cave_survival.config import config
|
from cave_survival.config import config
|
||||||
from cave_survival.console import console
|
from cave_survival.console import console
|
||||||
|
from cave_survival.daylightcycle import DayLightCycle
|
||||||
from cave_survival.map import Map, Point
|
from cave_survival.map import Map, Point
|
||||||
from cave_survival.player import Player
|
from cave_survival.player import Player
|
||||||
|
|
||||||
|
|
@ -17,11 +18,10 @@ class Game:
|
||||||
(config.screen_width, config.screen_height)
|
(config.screen_width, config.screen_height)
|
||||||
)
|
)
|
||||||
self.clock = pygame.time.Clock()
|
self.clock = pygame.time.Clock()
|
||||||
self.player = Player()
|
self.player = Player(game=self)
|
||||||
self.day_length = 60 * 60 # 60 seconds * 60 frames
|
self.map = Map(self)
|
||||||
self.day_timer = 0
|
self.daylightcycle = DayLightCycle(self)
|
||||||
self.inventory = ["sword", "pickaxe", "axe"]
|
self.inventory = ["sword", "pickaxe", "axe"]
|
||||||
self.map = Map()
|
|
||||||
self.inventory_surface = pygame.Surface((len(self.inventory) * 32, 32))
|
self.inventory_surface = pygame.Surface((len(self.inventory) * 32, 32))
|
||||||
self.inventory_surface.fill(config.GREY)
|
self.inventory_surface.fill(config.GREY)
|
||||||
|
|
||||||
|
|
@ -36,76 +36,12 @@ class Game:
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|
||||||
# Update the day/night cycle
|
# 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
|
# Draw the screen
|
||||||
self.screen.fill(config.BLACK)
|
self.screen.fill(config.BLACK)
|
||||||
|
|
||||||
# Draw the map
|
self.map.draw()
|
||||||
self.map.offset = Point(x=self.player.x, y=self.player.y)
|
self.player.draw()
|
||||||
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)
|
|
||||||
|
|
||||||
# Draw the inventory
|
# Draw the inventory
|
||||||
for i, item_name in enumerate(self.inventory):
|
for i, item_name in enumerate(self.inventory):
|
||||||
|
|
@ -119,19 +55,8 @@ class Game:
|
||||||
config.screen_height - self.inventory_surface.get_height(),
|
config.screen_height - self.inventory_surface.get_height(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
self.daylightcycle.draw()
|
||||||
# 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
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
self.clock.tick(config.fps)
|
||||||
|
|
||||||
# Limit the framerate
|
|
||||||
self.clock.tick(60)
|
|
||||||
|
|
||||||
# Quit pygame
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,8 @@ class Point(pydantic.BaseModel):
|
||||||
|
|
||||||
|
|
||||||
class Map:
|
class Map:
|
||||||
def __init__(self):
|
def __init__(self, game):
|
||||||
|
self.game = game
|
||||||
self.grass = pygame.image.load("grass.webp").convert_alpha()
|
self.grass = pygame.image.load("grass.webp").convert_alpha()
|
||||||
self.rock = pygame.image.load("rock.jpg").convert_alpha()
|
self.rock = pygame.image.load("rock.jpg").convert_alpha()
|
||||||
self.dirt = pygame.image.load("dirt.jpg").convert_alpha()
|
self.dirt = pygame.image.load("dirt.jpg").convert_alpha()
|
||||||
|
|
@ -45,8 +46,8 @@ class Map:
|
||||||
value = (value + 1) / 2 * 255
|
value = (value + 1) / 2 * 255
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def draw(self, screen):
|
def draw(self):
|
||||||
screen.blit(
|
self.game.screen.blit(
|
||||||
pygame.transform.scale(self.surf, (self.screen_width, self.screen_height)),
|
pygame.transform.scale(self.surf, (self.screen_width, self.screen_height)),
|
||||||
(0, 0),
|
(0, 0),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@ from cave_survival.console import console
|
||||||
|
|
||||||
|
|
||||||
class Player:
|
class Player:
|
||||||
def __init__(self):
|
def __init__(self, game):
|
||||||
|
self.game = game
|
||||||
self.width = 16
|
self.width = 16
|
||||||
self.height = 16
|
self.height = 16
|
||||||
self.x = config.screen_width / 2
|
self.x = config.screen_width / 2
|
||||||
|
|
@ -21,8 +22,62 @@ class Player:
|
||||||
)
|
)
|
||||||
self.hitbox_surface.set_alpha(0)
|
self.hitbox_surface.set_alpha(0)
|
||||||
|
|
||||||
def draw(self, surface, offset):
|
def move(self):
|
||||||
surface.blit(
|
# 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)),
|
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),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue