-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevelEditor.py
133 lines (108 loc) · 3.33 KB
/
levelEditor.py
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
"""
Author: Enoc Mena
Description: This program lets you save, load and edit your game levels.
"""
import pygame
import button
import pickle
import csv
pygame.init()
clock = pygame.time.Clock()
FPS = 60
#game window
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 640
LOWER_MARGIN = 100
SIDE_MARGIN = 300
screen = pygame.display.set_mode((SCREEN_WIDTH + SIDE_MARGIN, SCREEN_HEIGHT + LOWER_MARGIN))
pygame.display.set_caption('Level Editor')
#define game variables
ROWS = 16
MAX_COLS = 150
TILE_SIZE = SCREEN_HEIGHT // ROWS
TILE_TYPES = 7
current_tile = 0
scroll_left = False
scroll_right = False
scroll = 0
scroll_speed = 1
#load images
city_img = pygame.image.load('images/Background/city.png').convert_alpha()
#store tiles in list
img_list = []
for x in range(TILE_TYPES):
img = pygame.image.load(f'images/tiles/{x}.png')
img = pygame.transform.scale(img, (TILE_SIZE, TILE_SIZE))
img_list.append(img)
#define colors
GREEN = (144, 201, 120)
WHITE = (255, 255, 255)
RED = (200, 25, 25)
#create function for drawing the background
def draw_bg():
screen.fill(GREEN)
width = city_img.get_width()
for x in range(4):
screen.blit(city_img, ((x * width)-scroll * 0.5, 0))
#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))
#create buttons
#make button list
button_list = []
button_col = 0
button_row = 0
for i in range(len(img_list)):
tile_button = button.Button(SCREEN_WIDTH + (75 * button_col) + 50, 75 * button_row + 50, img_list[i], 1)
button_list.append(tile_button)
button_col += 1
if button_col == 3:
button_row += 1
button_col = 0
#################################################Level*Editor*Runs*Here#################################################
run = True
while run:
clock.tick(FPS)
draw_bg()
draw_grid()
#draw tile panel and tiles
pygame.draw.rect(screen, GREEN, (SCREEN_WIDTH, 0, SIDE_MARGIN, SCREEN_HEIGHT))
#choose a tile
button_count = 0
for button_count, i in enumerate(button_list):
if i.draw(screen):
current_tile = button_count
#highlight selected tile
pygame.draw.rect(screen, RED, button_list[current_tile].rect, 3)
#scroll the map
if scroll_left == True and scroll > 0:
scroll -= 5 * scroll_speed
if scroll_right == True:
scroll += 5 * scroll_speed
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#keyboard presses
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
scroll_left = True
if event.key == pygame.K_RIGHT:
scroll_right = True
if event.key == pygame.K_RSHIFT:
scroll_speed = 5
if event.key == pygame.K_ESCAPE:
run = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
scroll_left = False
if event.key == pygame.K_RIGHT:
scroll_right = False
if event.key == pygame.K_RSHIFT:
scroll_speed = 1
pygame.display.update()
pygame.quit()