This commit is contained in:
Waylon Walker 2023-03-19 13:00:17 -05:00
parent 80d7ebf7eb
commit cedb02e77e
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
5 changed files with 110 additions and 90 deletions

View file

@ -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()