-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkmn_overlay.py
215 lines (198 loc) · 7.97 KB
/
pkmn_overlay.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
import os
import random
from PIL import Image
import pygame
ASPECT_RATIO = 474/280
RESOLUTION = (1280,720)
#LEFT = 0.126
#UPPER = 0.133
#RIGHT = 0.916
#LOWER = 0.472
LEFT = 0.10
UPPER = 0.111
RIGHT = 0.9
LOWER = 0.472
STANDARD_SIZE = (734,1024)
IMG_SIZE = (int(RESOLUTION[0]/6),int((RESOLUTION[0]/6)/ASPECT_RATIO))
CROP_RATIOS = (int(STANDARD_SIZE[0]*LEFT),int(STANDARD_SIZE[1]*UPPER),int(STANDARD_SIZE[0]*RIGHT),int(STANDARD_SIZE[1]*LOWER))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
RSC_DIR = os.path.abspath(os.path.join(PROJECT_ROOT,"rsc/"))
CARDS_DIR = os.path.abspath(os.path.join(RSC_DIR,"cards/"))
SAVE_FILE = os.path.abspath(os.path.join(PROJECT_ROOT,"party.txt"))
def pilImageToSurface(pilImage : Image.Image):
return pygame.image.frombytes(
pilImage.tobytes(), pilImage.size, pilImage.mode).convert()
def get_card_image_by_pokemon(pokemon_name : str, card_index = None):
pokemon_name = pokemon_name.lower()
pkmn : pygame.Surface = pygame.Surface(IMG_SIZE)
if card_index is None:
card_index = random.randrange(6)
if card_index is not None:
try:
img = Image.open(os.path.abspath(os.path.join(CARDS_DIR,f"{pokemon_name}_{card_index}.png")))
img = img.resize(STANDARD_SIZE)
cropped = img.crop(CROP_RATIOS)
img = cropped.resize(IMG_SIZE)
pkmn = pilImageToSurface(img)
return pkmn, card_index, pokemon_name
except FileNotFoundError:
print("No Images Found")
index = 0
for i in range(5):
try:
img = Image.open(os.path.abspath(os.path.join(CARDS_DIR,f"{pokemon_name}_{i}.png")))
except FileNotFoundError:
print("No Images Found")
return None
img = img.resize(STANDARD_SIZE)
cropped = img.crop(CROP_RATIOS)
img = cropped.resize(IMG_SIZE)
pkmn = pilImageToSurface(img)
index = i
break
return pkmn, index, pokemon_name
def get_pokemon_slot_and_name(text : str)-> str:
choices = text.split(' ',1)
if len(choices) != 2:
print("Not enough parameters")
return None
try:
slot = int(choices[0])
except:
slot = -1
pkmn_name = choices[1]
if slot not in range(1,7):
print("Something is not right.")
return None
return (slot,pkmn_name)
def get_random_team()->list[pygame.surface.Surface,int,str]:
team = [None]*6
with open(os.path.abspath(os.path.join(RSC_DIR,'pokemon_names.txt')),'r') as file:
pokemon_names = file.readlines()
for i in range(6):
temp = get_card_image_by_pokemon(pokemon_names[random.randrange(len(pokemon_names))][:-1])
while temp is None:
temp = get_card_image_by_pokemon(pokemon_names[random.randrange(len(pokemon_names))][:-1])
team[i] = temp
return team
def clear_empty_slots_on_team(team : list[pygame.surface.Surface,int,str])->list[pygame.surface.Surface,int,str]:
compact_team = [None] * 6
counter = 0
for pkmn in team:
if pkmn is not None:
compact_team[counter] = pkmn
counter+=1
return compact_team
def load_save() -> list[pygame.surface.Surface,int,str]:
team = [None] * 6
if os.path.exists(SAVE_FILE):
with open(SAVE_FILE,'r+') as sav:
for i in range(6):
try:
pkmn_name, card_index = sav.readline().split(',',1)
pkmn = get_card_image_by_pokemon(pkmn_name,card_index[:-1])
if pkmn == None:
pass
else:
team[i] = pkmn
except Exception as e:
pass
team = clear_empty_slots_on_team(team)
return team
else:
return [None] * 6
def save_team(team : list[pygame.surface.Surface,int,str]):
with open(SAVE_FILE,'w+') as sav:
for pkmn in team:
if pkmn is not None:
sav.write(f"{pkmn[2]},{pkmn[1]}\n")
sav.close()
def main():
pygame.init()
pygame.display.init()
icon = pygame.image.load(os.path.abspath(os.path.join(RSC_DIR,'pokeball.ico')))
pygame.display.set_icon(icon)
pygame.display.set_caption('Pokémon Overlay')
screen = pygame.display.set_mode((RESOLUTION[0], IMG_SIZE[1]))
fuchsia = (255, 0, 128) # Transparency color
clock = pygame.time.Clock()
font = pygame.font.Font(size=24)
pkmns = load_save()
temp_text = ""
backspace_counter = 0
layout = 6,1
done = False
update = True
while not done:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
save_team(pkmns)
if event.type == pygame.MOUSEBUTTONDOWN:
update = True
if event.type == pygame.KEYDOWN:
update = True
if event.key == pygame.K_BACKSPACE:
if backspace_counter > 2:
temp_text = ""
backspace_counter = 0
else:
temp_text = temp_text[:-1]
backspace_counter +=1
elif event.key == pygame.K_RETURN:
if temp_text.lower() == "clear":
pkmns = [None] * 6
else:
selection = get_pokemon_slot_and_name(temp_text)
if selection is not None:
if selection[1].lower() == 'x':
pkmns[selection[0]-1] = None
elif selection[1].lower() == 'r':
if pkmns[selection[0]-1] is not None:
pkmns[selection[0]-1] = get_card_image_by_pokemon(pkmns[selection[0]-1][2],int(pkmns[selection[0]-1][1])+1)
else:
pkmns[selection[0]-1] = get_card_image_by_pokemon(selection[1])
temp_text = ""
elif event.key == pygame.K_F1: # 2x3
screen = pygame.display.set_mode(((IMG_SIZE[0]*2),IMG_SIZE[1]*3))
update = True
layout = 2,3
elif event.key == pygame.K_F2: # 3x2
screen = pygame.display.set_mode(((IMG_SIZE[0]*3),IMG_SIZE[1]*2))
update = True
layout = 3,2
elif event.key == pygame.K_F3: # 6x1
screen = pygame.display.set_mode(((IMG_SIZE[0]*6),IMG_SIZE[1]*1))
update = True
layout = 6,1
elif event.key == pygame.K_F4: # 1x6
screen = pygame.display.set_mode(((IMG_SIZE[0]*1),IMG_SIZE[1]*6))
update = True
layout = 1,6
elif event.key == pygame.K_TAB:
pkmns = clear_empty_slots_on_team(pkmns)
elif event.key == pygame.K_ESCAPE:
done = True
save_team(pkmns)
else:
backspace_counter = 0
temp_text += str(event.unicode)
if update: # Draw screen only when needed
screen.fill(fuchsia) # Transparent background
counter = 0
for i in range(layout[0]):
for j in range(layout[1]):
if pkmns[counter] is not None:
screen.blit(pkmns[counter][0],(pkmns[counter][0].get_width()*i,pkmns[counter][0].get_height()*j))
counter += 1
text_surf = font.render(temp_text, True, (0,0,0))
text_rect = text_surf.get_rect()
text_rect.x = 10
text_rect.y = 10
pygame.draw.rect(screen,(255,255,255),text_rect)
screen.blit(text_surf,(10,10))
pygame.display.update()
update = False
if __name__ == "__main__":
main()