-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (69 loc) · 2.37 KB
/
main.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
import pygame
import json
import sys
# Define some colors
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
# Load the map data
with open("map.json") as f:
map_data = json.load(f)
# Convert map_size values to integers
map_width = int(map_data["map_size"]["width"])
map_height = int(map_data["map_size"]["height"])
screen = pygame.display.set_mode((map_width, map_height))
# Load the custom images for houses, factories, and gardens
house_image = pygame.image.load("house.png")
factory_image = pygame.image.load("factory.png")
garden_image = pygame.image.load("garden.png")
# Create a sprite group for the objects
objects = pygame.sprite.Group()
# Create a function to draw the map tiles
def draw_map(map_data):
for tile in map_data:
tile_x = tile["x"]
tile_y = tile["y"]
tile_type = tile["type"]
if tile_type == "grass":
pygame.draw.rect(screen, GREEN, (tile_x, tile_y, 32, 32))
elif tile_type == "water":
pygame.draw.rect(screen, BLUE, (tile_x, tile_y, 32, 32))
# Create objects based on the map data
for obj_data in map_data["objects"]:
obj_type = obj_data["type"]
obj_x = obj_data["x"]
obj_y = obj_data["y"]
if obj_type == "house":
obj = pygame.sprite.Sprite()
obj.image = house_image # Use the loaded house image
obj.rect = obj.image.get_rect()
obj.rect.center = (obj_x, obj_y)
objects.add(obj)
elif obj_type == "factory":
obj = pygame.sprite.Sprite()
obj.image = factory_image # Use the loaded factory image
obj.rect = obj.image.get_rect()
obj.rect.center = (obj_x, obj_y)
objects.add(obj)
elif obj_type == "garden":
obj = pygame.sprite.Sprite()
obj.image = garden_image # Use the loaded garden image
obj.rect = obj.image.get_rect()
obj.rect.center = (obj_x, obj_y)
objects.add(obj)
# Run the game loop
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Clear the screen
screen.fill((255, 250, 250))
# Draw the map tiles
draw_map(map_data["tiles"])
# Draw the objects
objects.draw(screen)
# Update the screen
pygame.display.update()