from hooman import Hooman
import pygame
hapi = Hooman (500 , 500 )
def handle_events (event ):
if event .type == pygame .QUIT :
hapi .is_running = False
hapi .handle_events = handle_events
while hapi .is_running :
hapi .background ((255 , 255 , 255 ))
hapi .stroke_size (5 )
hapi .stroke ((0 , 255 , 0 ))
for i in range (0 , hapi .WIDTH , 20 ):
hapi .line (i , 0 , hapi .mouseX (), hapi .mouseY ())
hapi .record ()
hapi .flip_display ()
hapi .event_loop ()
pygame .quit ()
hapi .save_record ('mov.mp4' , framerate = 25 )
(new) Integrate with other pygame codes
# from https://pythonguides.com/create-a-game-using-python-pygame/
# Hooman >= 0.8.2
import pygame
from hooman import Hooman # <-- this
black = (0 , 0 , 0 )
white = (255 , 255 , 255 )
red = (255 , 0 , 0 )
WIDTH = 20
HEIGHT = 20
MARGIN = 5
grid = []
for row in range (10 ):
grid .append ([])
for column in range (10 ):
grid [row ].append (0 )
grid [1 ][5 ] = 1
pygame .init ()
window_size = [255 , 255 ]
scr = pygame .display .set_mode (window_size )
hapi = Hooman (integrate = True , screen = scr ) # <-- this
pygame .display .set_caption ("Grid" )
done = False
clock = pygame .time .Clock ()
while not done :
for event in pygame .event .get ():
if event .type == pygame .QUIT :
done = True
elif event .type == pygame .MOUSEBUTTONDOWN :
pos = pygame .mouse .get_pos ()
column = pos [0 ] // (WIDTH + MARGIN )
row = pos [1 ] // (HEIGHT + MARGIN )
grid [row ][column ] = 1
print ("Click " , pos , "Grid coordinates: " , row , column )
scr .fill (black )
for row in range (10 ):
for column in range (10 ):
color = white
if grid [row ][column ] == 1 :
color = red
pygame .draw .rect (scr ,
color ,
[(MARGIN + WIDTH ) * column + MARGIN ,
(MARGIN + HEIGHT ) * row + MARGIN ,
WIDTH ,
HEIGHT ])
clock .tick (50 )
pygame .display .flip ()
hapi .record () # <-- this
pygame .quit ()
hapi .save_record ('movie.mp4' ) # <-- this
# https://github.com/mwaskom/seaborn-data/blob/master/penguins.csv
from hooman import Hooman
import pandas as pd
import os
window_width , window_height = 650 , 600
hapi = Hooman (window_width , window_height , svg = True )
base_path = os .path .dirname (os .path .abspath (__file__ ))
df = pd .read_csv (os .path .join (base_path , "data" , "penguins.csv" ))
df = df .fillna (0 )
data = {k :list (df [k ]) for k in df .columns .values .tolist ()}
hapi .background (255 )
colx = "bill_length_mm"
coly = "bill_depth_mm"
hapi .scatterchart (
40 ,
30 ,
500 ,
500 ,
{
"data" : data ,
"ticks_y" : 12 ,
"ticks_x" : 12 ,
"range_y" : [min (data [coly ]), max (data [coly ])],
"range_x" : [min (data [colx ]), max (data [colx ])],
"show_axes" : True ,
"tick_size" : 10 ,
"show_ticks_x" : True ,
"show_ticks_y" : True ,
"x" : colx ,
"y" : coly ,
"plot_background" : False ,
"plot_grid" : False ,
"line_color" : 200 ,
"type" : "hist" ,
"hist_color" : "g"
},
)
hapi .save_svg (os .path .join (base_path , 'file.svg' ))
(new) keyword argument same as dictionary
hapi .scatterchart (
40 ,
30 ,
500 ,
500 ,
{
"data" : data ,
"ticks_x" : 5 ,
"mouse_line" : False ,
"range_y" : [min (data [coly ]), max (data [coly ])],
"range_x" : [min (data [colx ]), max (data [colx ])],
"tick_size" : 10 ,
"show_ticks_x" : True ,
"show_ticks_y" : True ,
"x" : colx ,
"y" : coly ,
"hue" : "clarity" ,
"hue_order" : clarity_ranking ,
"size" : "depth" ,
"plot_background" : False ,
"plot_background_grid" : True ,
"plot_background_color" : (234 ,234 ,242 ),
"plot_background_grid_color" : 200 ,
"line_color" : 200
}
)
# same as
hapi .scatterchart (
40 ,
30 ,
500 ,
500 ,
{
"data" : data ,
"ticks_x" : 5 ,
"mouse_line" : False ,
"range_y" : [min (data [coly ]), max (data [coly ])],
"range_x" : [min (data [colx ]), max (data [colx ])],
"tick_size" : 10 ,
"show_ticks_x" : True ,
"show_ticks_y" : True ,
"hue" : "clarity" ,
"hue_order" : clarity_ranking ,
"size" : "depth" ,
"plot_background" : False ,
"plot_background_grid" : True ,
"plot_background_color" : (234 ,234 ,242 ),
"plot_background_grid_color" : 200 ,
"line_color" : 200
},
x = colx ,
y = coly
)
# i.e you can mix both or use one option over the other