-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc_gen_pygame.py
More file actions
162 lines (127 loc) · 4.77 KB
/
proc_gen_pygame.py
File metadata and controls
162 lines (127 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import pygame
import numpy as np
from noise import snoise2
import random
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Colored Perlin Noise Map with Obstacles")
player_x, player_y = 400, 400
player_size = 5
num_obstacles = 10
obstacle_size = 10
obstacles = []
character_sheet_visible = False
font = pygame.font.SysFont(None, 36) # Initialize a font for the text display
character_data = {
"Name": "John Doe",
"Level": 1,
"HP": 100,
"MP": 50,
"Skills": ["Fireball", "Heal"],
"Equipment": ["Sword", "Shield"]
}
def draw_character_sheet():
pygame.draw.rect(WIN, (150, 150, 150), (50, 50, 300, 300))
title_text = font.render("Character Sheet", True, (0, 0, 0))
WIN.blit(title_text, (150, 60))
y_offset = 90
for key, value in character_data.items():
text = f"{key}: {value}"
info_text = font.render(text, True, (0, 0, 0))
WIN.blit(info_text, (60, y_offset))
y_offset += 30
def get_terrain_name(color):
if color == (0, 0, 128):
return "Deep Water"
elif color == (0, 0, 255):
return "Shallow Water"
elif color == (34, 139, 34):
return "Land"
elif color == (139, 69, 19):
return "Mountains"
else:
return "Unknown"
# Generate a random seed for Perlin noise
random_seed = random.randint(0, 10000)
# Generate Perlin noise map
grid = np.zeros((WIDTH, HEIGHT, 3), dtype=np.uint8)
for i in range(WIDTH):
for j in range(HEIGHT):
noise_value = snoise2(x=i / 40.0, y=j / 40.0, octaves=6, persistence=0.5, lacunarity=2.0, base=random_seed)
# Map noise_value to a color
if noise_value < -0.1:
color = (0, 0, 128) # Dark blue for deep water
elif noise_value < 0.1:
color = (0, 0, 255) # Light blue for shallow water
elif noise_value < 0.6:
color = (34, 139, 34) # Green for land
else:
color = (139, 69, 19) # Brown for mountains
grid[i, j] = color
# Generate random obstacles
for _ in range(num_obstacles):
x, y = random.randint(0, WIDTH - obstacle_size), random.randint(0, HEIGHT - obstacle_size)
obstacles.append((x, y))
# Main loop
run = True
movement_increment = .1 # Smaller increment for smoother movement
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
character_sheet_visible = not character_sheet_visible
keys = pygame.key.get_pressed()
new_x, new_y = player_x, player_y
if keys[pygame.K_LEFT]:
new_x -= movement_increment
if keys[pygame.K_RIGHT]:
new_x += movement_increment
if keys[pygame.K_UP]:
new_y -= movement_increment
if keys[pygame.K_DOWN]:
new_y += movement_increment
# Check for collisions with obstacles
for ox, oy in obstacles:
if pygame.Rect(new_x, new_y, player_size, player_size).colliderect(pygame.Rect(ox, oy, obstacle_size, obstacle_size)):
new_x, new_y = player_x, player_y
break
# Wrap-around logic
if new_x < 0:
new_x = WIDTH - player_size
elif new_x >= WIDTH:
new_x = 0
if new_y < 0:
new_y = HEIGHT - player_size
elif new_y >= HEIGHT:
new_y = 0
player_x, player_y = new_x, new_y
# Draw everything
pygame.surfarray.blit_array(WIN, grid)
pygame.draw.rect(WIN, (255, 0, 0), (int(player_x), int(player_y), player_size, player_size))
for ox, oy in obstacles:
pygame.draw.rect(WIN, (0, 0, 0), (ox, oy, obstacle_size, obstacle_size))
# Get the terrain under the player
terrain_color = tuple(grid[int(player_x)][int(player_y)])
terrain_name = get_terrain_name(terrain_color)
# Render and display the text
# Create a background for the terrain information text
background_color = (200, 200, 200) # Light gray
border_color = (150, 150, 150) # Darker gray
text_color = (0, 0, 0) # Black
# Determine text size to create a background rectangle
text_surface = font.render(f"Terrain: {terrain_name}", True, text_color)
text_rect = text_surface.get_rect()
# Draw background rectangle and border
pygame.draw.rect(WIN, background_color, (text_rect.x + 8, text_rect.y + 8, text_rect.width + 4, text_rect.height + 4))
pygame.draw.rect(WIN, border_color, (text_rect.x + 8, text_rect.y + 8, text_rect.width + 4, text_rect.height + 4), 1)
# Render and display the text on top of the background
WIN.blit(text_surface, (text_rect.x + 10, text_rect.y + 10))
if character_sheet_visible:
draw_character_sheet()
pygame.display.update()
pygame.quit()