Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import json
- pygame.init()
- class Player:
- def __init__(self, attributes):
- self.type = attributes[0]
- self.maxHp = attributes[1]
- self.hp = attributes[2]
- self.block = attributes[3]
- self.effects = attributes[4]
- def load(output_list):
- with open("players.json", "r") as f:
- output_list.clear()
- attr_dict = list(json.loads(f.read())['players'])
- for i in attr_dict:
- output_list.append(Player(list(i.values())))
- def save(file, input_list):
- with open(file, "w") as f:
- data = {"players": []}
- temp = data['players']
- for i in input_list:
- temp.append(i.__dict__)
- json.dump(data, f, indent=4)
- def create_render(player):
- # variables :
- offset = 0
- size = (600, 600)
- display = pygame.display.set_mode((600, 600))
- character = pygame.image.load("C:/users/jules/OneDrive/event sts serv cned/characters/%s/%s.png" % (player.type, player.type))
- w, h = character.get_size()
- done = False
- # health bar color, it changes when you have block
- if player.block == 0:
- hpColor = (230, 0, 0)
- else:
- hpColor = (90, 90, 230)
- # display offset, because 2 of the character have textures not centered on their bodies
- if player.type == "ironclad":
- offset = 50
- elif player.type == "watcher":
- offset = -15
- display.fill((255, 255, 255)) # background, temporary
- display.blit(character, (size[0] / 2 - w / 2 - offset, size[1] / 2 - h / 2)) # character, in the middle of the screen ofc
- # draw the health bar background
- pygame.draw.aaline(display, (100, 100, 100), (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + 2, size[1] / 2 + h / 2 + 20), (size[0] / 2 + (player.maxHp / 2) * (200 / player.maxHp) - 2, size[1] / 2 + h / 2 + 20), True)
- pygame.draw.aaline(display, (100, 100, 100), (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + 1, size[1] / 2 + h / 2 + 21), (size[0] / 2 + (player.maxHp / 2) * (200 / player.maxHp) - 1, size[1] / 2 + h / 2 + 21), True)
- pygame.draw.aaline(display, (100, 100, 100), (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + 0, size[1] / 2 + h / 2 + 22), (size[0] / 2 + (player.maxHp / 2) * (200 / player.maxHp) - 0, size[1] / 2 + h / 2 + 22), True)
- pygame.draw.aaline(display, (100, 100, 100), (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + 1, size[1] / 2 + h / 2 + 23), (size[0] / 2 + (player.maxHp / 2) * (200 / player.maxHp) - 1, size[1] / 2 + h / 2 + 23), True)
- pygame.draw.aaline(display, (100, 100, 100), (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + 2, size[1] / 2 + h / 2 + 24), (size[0] / 2 + (player.maxHp / 2) * (200 / player.maxHp) - 2, size[1] / 2 + h / 2 + 24), True)
- # draw the health bar, only if the players hp is not 0
- if player.hp != 0:
- pygame.draw.aaline(display, hpColor, (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + 2, size[1] / 2 + h / 2 + 20), (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + player.hp * (200 / player.maxHp) - 2, size[1] / 2 + h / 2 + 20), True)
- pygame.draw.aaline(display, hpColor, (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + 1, size[1] / 2 + h / 2 + 21), (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + player.hp * (200 / player.maxHp) - 1, size[1] / 2 + h / 2 + 21), True)
- pygame.draw.aaline(display, hpColor, (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + 0, size[1] / 2 + h / 2 + 22), (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + player.hp * (200 / player.maxHp) - 0, size[1] / 2 + h / 2 + 22), True)
- pygame.draw.aaline(display, hpColor, (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + 1, size[1] / 2 + h / 2 + 23), (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + player.hp * (200 / player.maxHp) - 1, size[1] / 2 + h / 2 + 23), True)
- pygame.draw.aaline(display, hpColor, (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + 2, size[1] / 2 + h / 2 + 24), (size[0] / 2 - (player.maxHp / 2) * (200 / player.maxHp) + player.hp * (200 / player.maxHp) - 2, size[1] / 2 + h / 2 + 24), True)
- pygame.display.flip() # update the screen
- # wait for the user to close the window
- while not done:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- players = []
- # save("players.json", players)
- load(players)
- for i in players:
- create_render(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement