wip
This commit is contained in:
parent
44ecb00701
commit
80d7ebf7eb
7 changed files with 189 additions and 294 deletions
Binary file not shown.
14
cave_survival/config.py
Normal file
14
cave_survival/config.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import pydantic
|
||||
|
||||
|
||||
class Config(pydantic.BaseModel):
|
||||
screen_width: int = 800
|
||||
screen_height: int = 600
|
||||
|
||||
BLACK: tuple[int] = (0, 0, 0)
|
||||
WHITE: tuple[int] = (255, 255, 255)
|
||||
GREY: tuple[int] = (128, 128, 128)
|
||||
RED: tuple[int] = (255, 128, 128)
|
||||
|
||||
|
||||
config = Config()
|
||||
137
cave_survival/game.py
Normal file
137
cave_survival/game.py
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
import pygame
|
||||
from pydantic import BaseModel
|
||||
|
||||
from cave_survival.config import config
|
||||
from cave_survival.console import console
|
||||
from cave_survival.map import Map, Point
|
||||
from cave_survival.player import Player
|
||||
|
||||
|
||||
class Game:
|
||||
def __init__(self):
|
||||
# Initialize pygame
|
||||
pygame.init()
|
||||
pygame.display.set_caption("Underground Survival")
|
||||
|
||||
self.screen = pygame.display.set_mode(
|
||||
(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.inventory = ["sword", "pickaxe", "axe"]
|
||||
self.map = Map()
|
||||
self.inventory_surface = pygame.Surface((len(self.inventory) * 32, 32))
|
||||
self.inventory_surface.fill(config.GREY)
|
||||
|
||||
self.running = True
|
||||
|
||||
def run(self):
|
||||
while self.running:
|
||||
self.events = pygame.event.get()
|
||||
self.keys = pygame.key.get_pressed()
|
||||
for event in self.events:
|
||||
if event.type == pygame.QUIT:
|
||||
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)
|
||||
|
||||
# Draw the inventory
|
||||
for i, item_name in enumerate(self.inventory):
|
||||
self.inventory_surface.blit(
|
||||
pygame.image.load(item_name + ".png").convert_alpha(), (i * 32, 0)
|
||||
)
|
||||
self.screen.blit(
|
||||
self.inventory_surface,
|
||||
(
|
||||
config.screen_width / 2 - self.inventory_surface.get_width() / 2,
|
||||
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
|
||||
pygame.display.flip()
|
||||
|
||||
# Limit the framerate
|
||||
self.clock.tick(60)
|
||||
|
||||
# Quit pygame
|
||||
pygame.quit()
|
||||
|
|
@ -4,15 +4,10 @@ from noise import snoise2
|
|||
from PIL import Image, ImageFilter
|
||||
from rich.console import Console
|
||||
|
||||
from cave_survival.config import config
|
||||
|
||||
console = Console()
|
||||
|
||||
# Generate the map using perlin noise
|
||||
|
||||
BLACK = (0, 0, 0)
|
||||
WHITE = (255, 255, 255)
|
||||
GREY = (128, 128, 128)
|
||||
RED = (255, 128, 128)
|
||||
|
||||
|
||||
class Point(pydantic.BaseModel):
|
||||
x: int
|
||||
|
|
@ -28,10 +23,8 @@ class Map:
|
|||
self.scale = 0.14 # Determines the "smoothness" of the terrain
|
||||
self.offset = Point(x=0, y=0)
|
||||
self.last_offset = self.offset
|
||||
# self.width = int(screen_width / self.resolution)
|
||||
# self.height = int(screen_height / self.resolution)
|
||||
self.screen_width = 800
|
||||
self.screen_height = 600
|
||||
self.screen_width = config.screen_width
|
||||
self.screen_height = config.screen_height
|
||||
self.octaves = 2 # Number of layers of noise to combine
|
||||
self.persistence = 0.05 # Amplitude of each octave
|
||||
self.lacunarity = 1.0 # Frequency of each octave
|
||||
|
|
@ -40,17 +33,6 @@ class Map:
|
|||
|
||||
def refresh_surf(self):
|
||||
self.surf = pygame.Surface((self.screen_width, self.screen_height))
|
||||
# self.surf.blit(self.grass, (0, 0))
|
||||
|
||||
# self.noise_map = self.generate_noise_map()
|
||||
|
||||
# def generate_noise_map(self):
|
||||
|
||||
# noise_map = [
|
||||
# [self.get_noise(x, y) for x in range(self.width)]
|
||||
# for y in range(self.height)
|
||||
# ]
|
||||
# return noise_map
|
||||
|
||||
def get_noise(self, x, y):
|
||||
value = snoise2(
|
||||
|
|
@ -64,8 +46,6 @@ class Map:
|
|||
return value
|
||||
|
||||
def draw(self, screen):
|
||||
# if self.last_offset != self.offset:
|
||||
# self.last_offset = self.offset
|
||||
screen.blit(
|
||||
pygame.transform.scale(self.surf, (self.screen_width, self.screen_height)),
|
||||
(0, 0),
|
||||
|
|
@ -84,14 +64,12 @@ class Map:
|
|||
if not self.point_check_collision(x, y):
|
||||
pygame.draw.rect(
|
||||
self.surf,
|
||||
WHITE,
|
||||
config.WHITE,
|
||||
(
|
||||
x,
|
||||
y,
|
||||
1,
|
||||
1,
|
||||
# self.resolution,
|
||||
# self.resolution,
|
||||
),
|
||||
)
|
||||
pygame.image.save(self.surf, "map.png")
|
||||
|
|
|
|||
28
cave_survival/player.py
Normal file
28
cave_survival/player.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import pygame
|
||||
|
||||
from cave_survival.config import config
|
||||
from cave_survival.console import console
|
||||
|
||||
|
||||
class Player:
|
||||
def __init__(self):
|
||||
self.width = 16
|
||||
self.height = 16
|
||||
self.x = config.screen_width / 2
|
||||
self.y = config.screen_height / 2
|
||||
self.speed = 5
|
||||
self.image = pygame.image.load("player.png").convert_alpha()
|
||||
self.x_last = self.x
|
||||
self.y_last = self.y
|
||||
self.hitbox_surface = pygame.Surface((self.width, self.height))
|
||||
self.hitbox_surface.fill(config.WHITE)
|
||||
pygame.draw.rect(
|
||||
self.hitbox_surface, (255, 0, 0), (0, 0, self.width, self.height), 1
|
||||
)
|
||||
self.hitbox_surface.set_alpha(0)
|
||||
|
||||
def draw(self, surface, offset):
|
||||
surface.blit(
|
||||
pygame.transform.scale(self.image, (16, 16)),
|
||||
(self.x - 8 - offset.x, self.y - 8 - offset.y),
|
||||
)
|
||||
|
|
@ -1,2 +1,6 @@
|
|||
from cave_survival.game import Game
|
||||
|
||||
|
||||
def run():
|
||||
from cave_survival import underground
|
||||
game = Game()
|
||||
game.run()
|
||||
|
|
|
|||
|
|
@ -1,266 +0,0 @@
|
|||
from pydantic import BaseModel
|
||||
import pygame
|
||||
from rich.console import Console
|
||||
|
||||
from cave_survival.map import Map, Point
|
||||
|
||||
console = Console()
|
||||
|
||||
# Initialize pygame
|
||||
pygame.init()
|
||||
|
||||
# Set up the display
|
||||
screen_width = 800
|
||||
screen_height = 600
|
||||
screen = pygame.display.set_mode((screen_width, screen_height))
|
||||
pygame.display.set_caption("Underground Survival")
|
||||
|
||||
# Set up the clock
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
# Set up the colors
|
||||
BLACK = (0, 0, 0)
|
||||
WHITE = (255, 255, 255)
|
||||
GREY = (128, 128, 128)
|
||||
RED = (255, 128, 128)
|
||||
|
||||
# Set up the player
|
||||
|
||||
|
||||
class Collisions(BaseModel):
|
||||
top: bool = False
|
||||
right: bool = False
|
||||
bottom: bool = False
|
||||
left: bool = False
|
||||
|
||||
|
||||
class Player:
|
||||
def __init__(self):
|
||||
self.width = 16
|
||||
self.height = 16
|
||||
self.x = screen_width / 2
|
||||
self.y = screen_height / 2
|
||||
self.speed = 5
|
||||
self.image = pygame.image.load("player.png").convert_alpha()
|
||||
self.x_last = self.x
|
||||
self.y_last = self.y
|
||||
self.hitbox_surface = pygame.Surface((self.width, self.height))
|
||||
self.hitbox_surface.fill(WHITE)
|
||||
pygame.draw.rect(
|
||||
self.hitbox_surface, (255, 0, 0), (0, 0, self.width, self.height), 1
|
||||
)
|
||||
self.hitbox_surface.set_alpha(0)
|
||||
|
||||
def draw(self, surface, offset):
|
||||
surface.blit(
|
||||
pygame.transform.scale(self.image, (16, 16)),
|
||||
(self.x - 8 - offset.x, self.y - 8 - offset.y),
|
||||
)
|
||||
|
||||
|
||||
# def generate_perlin_noise(width, height, scale):
|
||||
# map.noise_map = [[0 for y in range(height)] for x in range(width)]
|
||||
# for i in range(width):
|
||||
# for j in range(height):
|
||||
# map.noise_map[i][j] = noise.pnoise2(
|
||||
# i / float(scale),
|
||||
# j / float(scale),
|
||||
# octaves=6,
|
||||
# persistence=0.5,
|
||||
# lacunarity=2.0,
|
||||
# repeatx=1024,
|
||||
# repeaty=1024,
|
||||
# base=0,
|
||||
# )
|
||||
# return map.noise_map
|
||||
|
||||
|
||||
player = Player()
|
||||
|
||||
# Set up the enemies
|
||||
enemy_width = 32
|
||||
enemy_height = 32
|
||||
enemy_speed = 2
|
||||
enemy_image = pygame.image.load("enemy.png").convert_alpha()
|
||||
|
||||
# Set up the items
|
||||
item_width = 32
|
||||
item_height = 32
|
||||
item_image = pygame.image.load("item.png").convert_alpha()
|
||||
|
||||
# Set up the day/night cycle
|
||||
day_length = 60 * 60 # 60 seconds * 60 frames
|
||||
day_timer = 0
|
||||
|
||||
# Set up the inventory
|
||||
inventory = ["sword", "pickaxe", "axe"]
|
||||
|
||||
# Set up the map
|
||||
|
||||
map = Map()
|
||||
map_image = pygame.image.load("map.png").convert_alpha()
|
||||
|
||||
# Set up the inventory surface
|
||||
inventory_surface = pygame.Surface((len(inventory) * 32, 32))
|
||||
inventory_surface.fill(GREY)
|
||||
|
||||
# Generate the map using perlin noise
|
||||
# map.noise_map = [[0 for x in range(map.width)] for y in range(map.height)]
|
||||
# map.noise_map = generate_perlin_noise(map.width, map.height, map.scale)
|
||||
# for x in range(map.width):
|
||||
# for y in range(map.height):
|
||||
# map.noise_map[x][y] = random.randint(0, 255)
|
||||
|
||||
# Make sure the player starts on a white block
|
||||
# while (
|
||||
# map.noise_map[int(player.x / map.resolution)][int(player.y / map.resolution)]
|
||||
# < map.thresh
|
||||
# ):
|
||||
|
||||
# while map.point_check_collision(player.pos.x, player.pos.y):
|
||||
# player.x += 1
|
||||
# player.y += 1
|
||||
# player.x -= 1
|
||||
# player.y -= 1
|
||||
|
||||
# Main game loop
|
||||
running = True
|
||||
while running:
|
||||
# Handle events
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
# Update the day/night cycle
|
||||
day_timer += 1
|
||||
if day_timer > day_length:
|
||||
day_timer = 0
|
||||
|
||||
# Update the player
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_w]:
|
||||
player.y -= player.speed
|
||||
if keys[pygame.K_s]:
|
||||
player.y += player.speed
|
||||
if keys[pygame.K_a]:
|
||||
player.x -= player.speed
|
||||
if keys[pygame.K_d]:
|
||||
player.x += player.speed
|
||||
|
||||
# Check for player collisions with the walls and the black tiles on the map
|
||||
collisions = Collisions()
|
||||
if player.x < 0:
|
||||
player.x = 0
|
||||
collisions.left = True
|
||||
if player.x > screen_width - player.width:
|
||||
player.x = screen_width - player.width
|
||||
collisions.right = True
|
||||
if player.y < 0:
|
||||
player.y = 0
|
||||
collisions.top = True
|
||||
if player.y > screen_height - player.height:
|
||||
player.y = screen_height - player.height
|
||||
collisions.bottom = True
|
||||
|
||||
player.pos = pygame.math.Vector2(player.x, player.y)
|
||||
|
||||
if map.point_check_collision(player.pos.x, player.pos.y):
|
||||
|
||||
start_pos = pygame.math.Vector2(player.x_last, player.y_last)
|
||||
end_pos = pygame.math.Vector2(player.x, player.y)
|
||||
movement_vector = end_pos - start_pos
|
||||
try:
|
||||
movement_direction = movement_vector.normalize()
|
||||
except:
|
||||
end_pos = pygame.math.Vector2(player.x + 128, player.y + 128)
|
||||
movement_vector = end_pos - start_pos
|
||||
movement_direction = movement_vector.normalize()
|
||||
movement_speed = 0.05
|
||||
|
||||
player.x = player.x_last
|
||||
player.y = player.y_last
|
||||
|
||||
player.pos = pygame.math.Vector2(start_pos)
|
||||
|
||||
while map.point_check_collision(player.pos.x, player.pos.y):
|
||||
# map.noise_map[int(player.pos.x / map.resolution)][
|
||||
# int(player.pos.y / map.resolution)
|
||||
# ] <
|
||||
# map.thresh
|
||||
# ):
|
||||
print("moving")
|
||||
print(movement_speed)
|
||||
print(movement_direction)
|
||||
|
||||
player.pos += movement_speed * movement_direction
|
||||
player.x = player.pos.x
|
||||
player.y = player.pos.y
|
||||
|
||||
player.pos -= movement_speed * movement_direction
|
||||
player.x = player.pos.x
|
||||
player.y = player.pos.y
|
||||
|
||||
player.x_last = player.x
|
||||
player.y_last = player.y
|
||||
|
||||
# Update the enemies
|
||||
# for enemy in enemies:
|
||||
# enemy_x += enemy_speed
|
||||
# enemy_y += enemy_speed
|
||||
|
||||
# Draw the screen
|
||||
screen.fill(BLACK)
|
||||
|
||||
# Draw the map
|
||||
map.offset = Point(x=player.x, y=player.y)
|
||||
map.offset = Point(x=0, y=0)
|
||||
map.draw(screen)
|
||||
|
||||
# Draw the player
|
||||
player.draw(screen, offset=map.offset)
|
||||
|
||||
# Draw the enemies
|
||||
# for enemy in enemies:
|
||||
# screen.blit(enemy_image, (enemy_x, enemy_y))
|
||||
|
||||
# Draw the items
|
||||
# for item in items:
|
||||
# screen.blit(item_image, (item_x, item_y))
|
||||
|
||||
# Draw the inventory
|
||||
for i, item_name in enumerate(inventory):
|
||||
inventory_surface.blit(
|
||||
pygame.image.load(item_name + ".png").convert_alpha(), (i * 32, 0)
|
||||
)
|
||||
screen.blit(
|
||||
inventory_surface,
|
||||
(
|
||||
screen_width / 2 - inventory_surface.get_width() / 2,
|
||||
screen_height - inventory_surface.get_height(),
|
||||
),
|
||||
)
|
||||
|
||||
# Draw the day/night cycle
|
||||
pygame.draw.rect(screen, GREY, (0, 0, day_timer / day_length * screen_width, 10))
|
||||
|
||||
# collision debug
|
||||
# top
|
||||
|
||||
# pygame.draw.rect(screen, RED, (0, 0, screen_width, 5))
|
||||
# # bottom
|
||||
# pygame.draw.rect(screen, RED, (0, screen_height - 5, screen_width, 5))
|
||||
# # left
|
||||
# pygame.draw.rect(screen, RED, (0, 0, 5, screen_height))
|
||||
# # right
|
||||
# pygame.draw.rect(screen, RED, (screen_width - 5, 0, 5, screen_height))
|
||||
|
||||
# Update the display
|
||||
pygame.display.flip()
|
||||
|
||||
# Limit the framerate
|
||||
clock.tick(60)
|
||||
# console.log(clock.get_fps())
|
||||
|
||||
|
||||
# Quit pygame
|
||||
pygame.quit()
|
||||
Loading…
Add table
Add a link
Reference in a new issue