This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soil.py
221 lines (186 loc) · 8.47 KB
/
soil.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import pygame
from settings import *
from pytmx.util_pygame import load_pygame
from support import *
from random import choice
class SoilTile(pygame.sprite.Sprite):
def __init__(self, pos, surf, groups):
super().__init__(groups)
self.image = surf
self.rect = self.image.get_rect(topleft=pos)
self.z = LAYERS['soil']
class WaterTile(pygame.sprite.Sprite):
def __init__(self, pos, surf, groups):
super().__init__(groups)
self.image = surf
self.rect = self.image.get_rect(topleft=pos)
self.z = LAYERS['soil water']
class Plant(pygame.sprite.Sprite):
def __init__(self, plant_type, groups, soil, check_watered):
super().__init__(groups)
# setup
self.plant_type = plant_type
self.frames = import_folder(f'./graphics/fruit/{plant_type}')
self.soil = soil
self.check_watered = check_watered
# plant growing
self.age = 0
self.max_age = len(self.frames) - 1
self.grow_speed = GROW_SPEED[self.plant_type]
self.harvestable = False
# sprite setup
self.image = self.frames[self.age]
self.y_offset = -16 if plant_type == 'corn' else -8
self.rect = self.image.get_rect(midbottom=soil.rect.midbottom + pygame.math.Vector2(0, self.y_offset))
self.z = LAYERS['ground plant']
def grow(self):
if self.check_watered(self.rect.center):
self.age += self.grow_speed
if int(self.age) > 0:
self.z = LAYERS['main']
self.hitbox = self.rect.copy().inflate(-26, self.rect.height * 0.4)
if self.age >= self.max_age:
self.age = self.max_age
self.harvestable = True
self.image = self.frames[int(self.age)]
# the image changed
self.rect = self.image.get_rect(midbottom=self.soil.rect.midbottom + pygame.math.Vector2(0, self.y_offset))
class SoilLayer:
def __init__(self, all_sprites, collision_sprites):
# requirements
# if the area is farm-able
# if the soil has been watered
# if the soil has plant
# sprite groups
self.all_sprites = all_sprites
self.collision_sprites = collision_sprites
self.soil_sprites = pygame.sprite.Group()
self.water_sprites = pygame.sprite.Group()
self.plant_sprites = pygame.sprite.Group()
# graphics
self.soil_surfs = import_folder_dict('./graphics/soil/')
self.water_surfs = import_folder('./graphics/soil_water/')
self.create_soil_grid()
self.create_hit_rects()
# raining
self.raining = False
# sounds
self.hoe_sound = pygame.mixer.Sound('./audio/hoe.wav')
self.hoe_sound.set_volume(0.1)
self.plant_sound = pygame.mixer.Sound('./audio/plant.wav')
self.plant_sound.set_volume(0.2)
def create_soil_grid(self):
# maintain a grid list of farm-able tiles.
ground = pygame.image.load('./graphics/world/ground.png')
h_tiles, v_tiles = ground.get_width() // TILE_SIZE, ground.get_height() // TILE_SIZE
self.grid = [[[] for col in range(h_tiles)] for row in range(v_tiles)]
for x, y, _ in load_pygame('./data/map.tmx').get_layer_by_name('Farmable').tiles():
self.grid[y][x].append('F')
def create_hit_rects(self):
self.hit_rects = []
for index_row, row in enumerate(self.grid):
for index_col, cell in enumerate(row):
if 'F' in cell:
x = index_col * TILE_SIZE
y = index_row * TILE_SIZE
rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
self.hit_rects.append(rect)
def get_hit(self, point):
for rect in self.hit_rects:
if rect.collidepoint(point):
self.hoe_sound.play()
x = rect.x // TILE_SIZE
y = rect.y // TILE_SIZE
if 'F' in self.grid[y][x]:
self.grid[y][x].append('X')
self.create_soil_tiles()
if self.raining:
self.water_all()
def water(self, tartget_pos):
for soil_sprite in self.soil_sprites.sprites():
if soil_sprite.rect.collidepoint(tartget_pos):
# 1. add an entry to the soil grid -> 'w'
x = soil_sprite.rect.x // TILE_SIZE
y = soil_sprite.rect.y // TILE_SIZE
self.grid[y][x].append('W')
# 2. create a water sprite
# 2.1.copy the position from the soil sprite
# 2.2. for the surface -> import the folder '../graphics/soil_water'
# 2.3.randomly select on surface
# 2.4.create one more group 'water_sprites'
pos = soil_sprite.rect.topleft
surf = choice(self.water_surfs)
WaterTile(pos, surf, [self.all_sprites, self.water_sprites])
def water_all(self):
for index_row, row in enumerate(self.grid):
for index_col, cell in enumerate(row):
if 'X' in cell and 'W' not in cell:
cell.append('W')
x = index_col * TILE_SIZE
y = index_row * TILE_SIZE
WaterTile((x, y), choice(self.water_surfs), [self.all_sprites, self.water_sprites])
def remove_water(self):
# destory all water sprites
for sprite in self.water_sprites.sprites():
sprite.kill()
# clean up the grid
for row in self.grid:
for cell in row:
if 'W' in cell:
cell.remove('W')
def check_watered(self, pos):
x = pos[0] // TILE_SIZE
y = pos[1] // TILE_SIZE
cell = self.grid[y][x]
is_watered = 'W' in cell
return is_watered
def plant_seed(self, target_pos, seed):
for soil_sprite in self.soil_sprites.sprites():
if soil_sprite.rect.collidepoint(target_pos):
self.plant_sound.play()
x = soil_sprite.rect.x // TILE_SIZE
y = soil_sprite.rect.y // TILE_SIZE
if 'P' not in self.grid[y][x]:
self.grid[y][x].append('P')
Plant(seed, [self.all_sprites, self.collision_sprites, self.plant_sprites], soil_sprite,
self.check_watered)
def update_plants(self):
for plant in self.plant_sprites.sprites():
plant.grow()
def create_soil_tiles(self):
# to change the shape of the old soil when a new soil is next to it.It means we need to redraw all the soil.
self.soil_sprites.empty()
for index_row, row in enumerate(self.grid):
for index_col, cell in enumerate(row):
if 'X' in cell:
# tile options
t = 'X' in self.grid[index_row - 1][index_col]
b = 'X' in self.grid[index_row + 1][index_col]
r = 'X' in row[index_col + 1]
l = 'X' in row[index_col - 1]
tile_tpye = 'o'
# all sides
if all((t, r, b, l)): tile_tpye = 'x'
# horizontal tiles only
if l and not any((t, r, b)): tile_tpye = 'r'
if r and not any((t, l, b)): tile_tpye = 'l'
if r and l and not any((t, b)): tile_tpye = 'lr'
# vertical only
if b and not any((l, r, t)): tile_tpye = 't'
if t and not any((r, l, b)): tile_tpye = 'b'
if t and b and not any((r, l)): tile_tpye = 'tb'
# corners
if l and b and not any((t, r)): tile_tpye = 'tr'
if l and t and not any((b, r)): tile_tpye = 'br'
if r and b and not any((t, l)): tile_tpye = 'tl'
if r and t and not any((b, l)): tile_tpye = 'bl'
# T shapes
if all((t, b, r)) and not l: tile_tpye = 'tbr'
if all((t, b, l)) and not r: tile_tpye = 'tbl'
if all((t, l, r)) and not b: tile_tpye = 'lrb'
if all((l, b, r)) and not t: tile_tpye = 'lrt'
SoilTile(
pos=(index_col * TILE_SIZE, index_row * TILE_SIZE),
surf=self.soil_surfs[tile_tpye],
groups=[self.all_sprites, self.soil_sprites]
)