-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel_editor.py
More file actions
186 lines (158 loc) · 5.89 KB
/
Copy pathlevel_editor.py
File metadata and controls
186 lines (158 loc) · 5.89 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import pygame, csv
import button
from gamelib import *
pygame.init()
pygame.font.init()
side_panel = 200
bottom_panel = 100
level_number = 0
screen_width = 700
screen_height = 500
screen = pygame.display.set_mode((screen_width+side_panel, screen_height+bottom_panel))
pygame.display.set_caption("Level Editor")
pygame.display.set_icon(pygame.image.load("editor_icon.png"))
screen_constants = get_usefull_constants(screen)
running = True
fps = 60
clock = pygame.time.Clock()
# define editor variables
level = 0
ROWS = 15
MAX_COLS = 200
TILE_SIZE = screen_height // ROWS
current_tile = 0
scroll_left = False
scroll_right = False
scroll = 0
scroll_speed = 1
# create empty tile list
world_data = []
for row in range(ROWS):
r = [-1] * MAX_COLS
world_data.append(r)
# create ground
for tile in range(0, MAX_COLS):
world_data[ROWS - 1][tile] = 0
# store tiles in list
img_list = []
for x in list(LEVEL_OBJECTS.keys()):
img = pygame.transform.scale(pygame.image.load(LEVEL_OBJECTS[x]["image"]), LEVEL_OBJECTS[x]["size"])
img_list.append(img)
# World Background
background = pygame.image.load('assets/images/bg_shroom.png').convert()
# draw grid
def draw_grid():
# vertical lines
for c in range(MAX_COLS + 1):
pygame.draw.line(screen, WHITE, (c * TILE_SIZE - scroll, 0), (c * TILE_SIZE - scroll, screen_height))
# horizontal lines
for c in range(ROWS + 1):
pygame.draw.line(screen, WHITE, (0, c * TILE_SIZE), (screen_width, c * TILE_SIZE))
# drawing world tiles
def draw_world():
for y, row in enumerate(world_data):
for x, tile in enumerate(row):
if tile >= 0:
screen.blit(img_list[tile], (x * TILE_SIZE - scroll, y * TILE_SIZE))
# creating buttons
button_list = []
button_col, button_row = 0, 0
for i in range(len(img_list)):
tile_button = button.IconButton(screen_width + (60 * button_col) + 20, 50 * button_row + 20, img_list[i], 1)
button_list.append(tile_button)
button_col += 1
if button_col == 3:
button_row += 1
button_col = 0
#create buttons
save_img = pygame.image.load('assets/images/save_btn.png').convert_alpha()
load_img = pygame.image.load('assets/images/load_btn.png').convert_alpha()
save_button = button.IconButton(screen_width // 2 + 80, screen_height + bottom_panel - 70, save_img, 1)
load_button = button.IconButton(screen_width // 2 + 180, screen_height + bottom_panel - 70, load_img, 1)
while running:
clock.tick(fps)
# draw background and text
screen.fill((255, 213, 128))
width = background.get_width()
for x in range(4):
screen.blit(background, ((x * width) - scroll * 0.5, 0))
draw_grid()
draw_world()
#save and load data
if save_button.draw(screen):
#save level data
with open(f'levels/{level}.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter = ',')
for row in world_data:
writer.writerow(row)
if load_button.draw(screen):
#load in level data
#reset scroll back to the start of the level
scroll = 0
with open(f'levels/{level}.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter = ',')
for x, row in enumerate(reader):
for y, tile in enumerate(row):
world_data[x][y] = int(tile)
# drawing level info
draw_text(screen, FONTS['game_info'], "LEVEL: " + str(level), 30, (194, 255, 255), (75, screen_constants['margin_y']-30))
draw_text(screen, font_file=FONTS['game_info'], text="Press Up or Down to change level".upper(), font_size=19, color=BLACK, pos=(210, screen_height + bottom_panel - 50))
# draw tile side_panel
pygame.draw.rect(screen, (255, 213, 128), (screen_width, 0, side_panel, screen_height+bottom_panel))
button_count = 0
for button_count, i in enumerate(button_list):
if i.draw(screen):
current_tile = button_count
# highlight the selected tile
pygame.draw.rect(screen, RED, button_list[current_tile].rect, 2)
# scroll the map
if scroll_left == True and scroll > 0:
scroll -= 5 * scroll_speed
if scroll_right == True and scroll < (MAX_COLS * TILE_SIZE) - screen_width:
scroll += 5 * scroll_speed
# add new tiles to screen
# get mouse position
mpos = pygame.mouse.get_pos()
x = (mpos[0] + scroll) // TILE_SIZE
y = mpos[1] // TILE_SIZE
# check that the coordinates are within the tile area
if mpos[0] < screen_width and mpos[1] < screen_height:
# update tile value
if pygame.mouse.get_pressed()[0] == 1:
try:
if world_data[y][x] != current_tile:
world_data[y][x] = current_tile
except:
pass
if pygame.mouse.get_pressed()[2] == 1:
try:
world_data[y][x] = -1
except:
pass
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
pass
# keyboard presses
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
level += 1
if event.key == pygame.K_DOWN and level > 0:
level -= 1
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
scroll_left = True
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
scroll_right = True
if event.key == pygame.K_RSHIFT:
scroll_speed = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
scroll_left = False
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
scroll_right = False
if event.key == pygame.K_RSHIFT:
scroll_speed = 1
pygame.display.update()