This commit is contained in:
Waylon Walker 2023-03-18 12:12:34 -05:00
parent eba8fd76fe
commit f67570eed4
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
18 changed files with 1435 additions and 77 deletions

90
cave_survival/map.py Normal file
View file

@ -0,0 +1,90 @@
from noise import snoise2
import pydantic
import pygame
from rich.console import Console
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
y: int
class Map:
def __init__(self):
self.resolution = 32
self.scale = 0.1 # 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.octaves = 6 # Number of layers of noise to combine
self.persistence = 0.5 # Amplitude of each octave
self.lacunarity = 2.0 # Frequency of each octave
self.thresh = 128
self.surf = pygame.Surface(
(self.screen_width / self.resolution, self.screen_height / self.resolution)
)
self.pre_draw()
# 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(
(x + self.offset.x) * self.scale,
(y + self.offset.y) * self.scale,
self.octaves,
self.persistence,
self.lacunarity,
)
value = (value + 1) / 2 * 255
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),
)
def point_check_collision(self, x, y):
return self.get_noise(x / self.resolution, y / self.resolution) < self.thresh
def pre_draw(self):
console.log("drawing")
self.surf = pygame.Surface((self.screen_width, self.screen_height))
for x in range(self.screen_width):
for y in range(self.screen_height):
if not self.point_check_collision(x, y):
pygame.draw.rect(
self.surf,
WHITE,
(
x,
y,
1,
1,
# self.resolution,
# self.resolution,
),
)
pygame.image.save(self.surf, "map.png")

View file

@ -1,78 +1,2 @@
import pygame
def run():
# Initialize Pygame
pygame.init()
# Set the window size and caption
size = (700, 500)
caption = "Pygame Image Movement"
screen = pygame.display.set_mode(size)
pygame.display.set_caption(caption)
# Load an image and get its rectangle
image1 = pygame.image.load("cave_survival/assets/stev/1.png")
image2 = pygame.image.load("cave_survival/assets/stev/2.png")
image_rect = image1.get_rect()
# Set the initial position of the image
image_rect.x = 350
image_rect.y = 250
movement_speed = 0.5
# Set the animation speed
animation_speed = 0.005
# Set the animation frame
animation_frame = 0
# Set the movement speed
movement_speed = 0.5
# Main game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Get the current key presses
pressed = pygame.key.get_pressed()
# Move the image based on the key presses
if pressed[pygame.K_w]:
image_rect.y -= movement_speed
if pressed[pygame.K_a]:
image_rect.x -= movement_speed
if pressed[pygame.K_s]:
image_rect.y += movement_speed
if pressed[pygame.K_d]:
image_rect.x += movement_speed
# Animate the image if the player is moving
if (
pressed[pygame.K_w]
or pressed[pygame.K_a]
or pressed[pygame.K_s]
or pressed[pygame.K_d]
):
animation_frame += animation_speed
if animation_frame >= 2:
animation_frame = 0
image_rect.x += movement_speed
# Draw the image on the screen
screen.fill((0, 0, 0))
if animation_frame < 1:
screen.blit(image1, image_rect)
else:
screen.blit(image2, image_rect)
else:
screen.fill((0, 0, 0))
screen.blit(image1, image_rect)
pygame.display.flip()
# Clean up
pygame.quit()
from cave_survival import underground

View file

@ -0,0 +1,266 @@
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()