Advertisement
akasaka-kun

Untitled

Apr 7th, 2020
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.48 KB | None | 0 0
  1. import pygame
  2. import json
  3.  
  4. pygame.init()
  5.  
  6.  
  7. class Player:
  8.  
  9.     def __init__(self, attributes):
  10.         self.type = attributes[0]
  11.         self.maxHp = attributes[1]
  12.         self.hp = attributes[2]
  13.         self.block = attributes[3]
  14.         self.effects = attributes[4]
  15.  
  16.  
  17. def load(output_list):
  18.     with open("players.json", "r") as f:
  19.         output_list.clear()
  20.         attr_dict = list(json.loads(f.read())['players'])
  21.         for i in attr_dict:
  22.             output_list.append(Player(list(i.values())))
  23.  
  24.  
  25. def save(file, input_list):
  26.     with open(file, "w") as f:
  27.         data = {"players": []}
  28.         temp = data['players']
  29.         for i in input_list:
  30.             temp.append(i.__dict__)
  31.         json.dump(data, f, indent=4)
  32.  
  33.  
  34. def create_render(player):
  35.     # variables :
  36.     offset = 0
  37.     size = (600, 600)
  38.     display = pygame.display.set_mode((600, 600))
  39.     character = pygame.image.load("C:/users/jules/OneDrive/event sts serv cned/characters/%s/%s.png" % (player.type, player.type))
  40.     w, h = character.get_size()
  41.     done = False
  42.     # health bar color, it changes when you have block
  43.     if player.block == 0:
  44.         hpColor = (230, 0, 0)
  45.     else:
  46.         hpColor = (90, 90, 230)
  47.     # display offset, because 2 of the character have textures not centered on their bodies
  48.     if player.type == "ironclad":
  49.         offset = 50
  50.     elif player.type == "watcher":
  51.         offset = -15
  52.     display.fill((255, 255, 255))  # background, temporary
  53.     display.blit(character, (size[0] / 2 - w / 2 - offset, size[1] / 2 - h / 2))  # character, in the middle of the screen ofc
  54.     # draw the health bar background
  55.     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)
  56.     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)
  57.     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)
  58.     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)
  59.     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)
  60.     # draw the health bar, only if the players hp is not 0
  61.     if player.hp != 0:
  62.         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)
  63.         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)
  64.         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)
  65.         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)
  66.         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)
  67.     pygame.display.flip()  # update the screen
  68.     # wait for the user to close the window
  69.     while not done:
  70.         for event in pygame.event.get():
  71.             if event.type == pygame.QUIT:
  72.                 done = True
  73.  
  74.  
  75. players = []
  76. # save("players.json", players)
  77. load(players)
  78. for i in players:
  79.     create_render(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement