Commit 92d4bc41 by source_reader

added simple UI for Marvel API

parent 64ab234b
Showing with 72 additions and 0 deletions
import marvel_client
import pygame
import sys
import tempfile
import requests
import io
def main():
client = marvel_client.MarvelClient("7e583905d15a48c849ef155bc9d6af29",
"f8307f988ea51a41040df0c273cd5ab36606d58d")
characters = client.get_characters()
pygame.init()
surf: pygame.Surface = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Marvel Hero Browser")
idx = 0
display_character(surf, characters[idx])
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and idx>0:
idx -= 1
if event.key == pygame.K_RIGHT and idx<(len(characters)-1):
idx += 1
# display the new character
display_character(surf, characters[idx])
pygame.display.update()
def display_character(surf, character):
# create a background for the screen (erases previous character)
surf.fill((200, 40, 80))
pygame.draw.rect(surf, (250, 250, 40), (10, 10, 620, 460), 10)
# center the character's name at the top
name = create_text(character.name, 80, max_width=surf.get_width()-40)
rect = name.get_rect()
rect.centerx = surf.get_rect().centerx
rect.y = 30
surf.blit(name, rect)
# put the character's image in the center
image = character.get_image()
rect = image.get_rect()
rect.centerx = surf.get_rect().centerx
rect.y = 150
surf.blit(image, rect)
def create_text(text, size, max_width=None, color=(0, 0, 0)):
font = pygame.font.Font('./assets/FutilePro.ttf', size)
image = font.render(text, True, color)
if max_width is None:
return image
# otherwise make sure the font fits within max_width
while True:
rect = image.get_rect()
if rect.width > max_width:
size -= 1
# try with the next size smaller
font = pygame.font.Font('./assets/FutilePro.ttf', size)
image = font.render(text, True, color)
else:
return image
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment