Pygame for humans (pip install hooman) (25k+ downloads)

Overview


Hooman

hooman Downloads PyPI

~ pygame for humans

pip install hooman

join discord: https://discord.gg/Q23ATve

The package for clearer, shorter and cleaner PyGame codebases!

Fun fact: Codementor.io tweeted about Hooman tagged #LearnPython #100DaysOfCode

hooman is powerful!

See a snake game by TheBigKahuna353

0: self.body[0] = self.head self.head = [self.head[i] + self.direction[i] for i in range(2)] self.move = hapi.timer(seconds=self.speed_slider.value()) if self.head in self.body: self.Died() elif self.head[0] >= WIDTH or self.head[0] < 0 or self.head[1] < 0 or self.head[1] >= HEIGHT: self.Died() if self.head == self.food_pos: self.body.append(self.head[:]) self.food_pos = [random.randint(0, WIDTH) // self.size * self.size for x in range(2)] hapi.fill(hapi.color['green']) hapi.rect(self.food_pos[0], self.food_pos[1], self.size-1, self.size-1) def Died(self): self.head = [x * self.size for x in self.start_pos] self.body = [] self.food_pos = [random.randint(0, WIDTH) // self.size * self.size for x in range(2)] self.direction = [0, 0] self.current_screen = self.Main_menu def Settings(self): hapi.background(hapi.color['white']) self.speed_slider.update() self.rows_slider.update() hapi.fill(hapi.color['black']) hapi.font_size(30) hapi.text(self.slider_names[0], 100, 200) hapi.text(self.slider_names[1], 100, 300) if self.back_btn.update(): self.current_screen = self.Main_menu def Main_menu(self): hapi.background(hapi.color['white']) hapi.fill(hapi.color['black']) hapi.font_size(50) hapi.text("Snake", 220, 30) if self.menu_btn_quit.update(): hapi.is_running = False if self.menu_btn_start.update(): self.current_screen = self.Main if self.settings_btn.update(): self.current_screen = self.Settings def Events(self, event): if event.type == pygame.QUIT: hapi.is_running = False elif event.type == pygame.KEYDOWN: if event.unicode == "a" or event.key == 276: self.direction = [-self.size, 0] elif event.unicode == "d" or event.key == 275: self.direction = [self.size, 0] elif event.unicode == "w" or event.key == 273: self.direction = [0, -self.size] elif event.unicode == "s" or event.key == 274: self.direction = [0, self.size] if __name__ == '__main__': game = Game() game.Start() ">
import hooman
import random
import pygame

WIDTH, HEIGHT = 600, 600

hapi = hooman.Hooman(WIDTH, HEIGHT)

btn_style = {'outline': True,
             'background_color': (255, 0, 0),
             'curve': 0.7,
             'outline_thickness': 3}

slider_style = {'background_color': (200, 200, 200),
                'slider_height': 60,
                'slider_color': (240, 240, 240)
                }

class Game:
    def __init__(self):
        self.current_screen = self.Main_menu
        hapi.handle_events = self.Events

        self.menu_btn_start = hapi.button(200, 200, 200, 50, "Start", btn_style)
        self.menu_btn_quit = hapi.button(200, 400, 200, 50, "Quit", btn_style)
        self.settings_btn = hapi.button(200, 300, 200, 50, 'Settings', btn_style)

        slider_style.update({'value_range': [5, 30],
                                 'step': 1,
                                 'starting_value': 20})
        self.rows_slider = hapi.slider(300, 200, 200, 30, slider_style)
        self.rows_slider = hapi.slider_with_text(self.rows_slider)
        
        slider_style.update({'value_range': [0.01, 1],
                             'step': 0,
                             'starting_value': 0.1})
        self.speed_slider = hapi.slider(300, 300, 200, 30, slider_style)
        self.speed_slider = hapi.slider_with_text(self.speed_slider, {'accuracy': 2})
        self.slider_names = ['num of rows', 'speed']
        self.back_btn = hapi.button(200, 400, 200, 50, "Back", btn_style)

        self.size = WIDTH//self.rows_slider.value()

        self.start_pos = 5, 5
        self.head = [x * self.size for x in self.start_pos]
        self.body = []
        self.food_pos = [random.randint(0, WIDTH) // self.size * self.size for x in range(2)]
        self.direction = [0, 0]
        self.move = hapi.timer(seconds=self.speed_slider.value())

    def Start(self):
        while hapi.is_running:
            self.current_screen()

            hapi.flip_display()
            hapi.event_loop()

    def Main(self):
        hapi.background(hapi.color['black'])
        hapi.fill(hapi.color['yellow'])
        hapi.rect(self.head[0]+1, self.head[1]+1, self.size-2, self.size-2)
        for x, y in self.body:
            hapi.rect(x+1, y+1, self.size-2, self.size-2)
        if self.move:
            for i in range(len(self.body) -1, 0, -1):
                self.body[i] = self.body[i - 1]
            if len(self.body) > 0:
                self.body[0] = self.head            
            self.head = [self.head[i] + self.direction[i] for i in range(2)]
            self.move = hapi.timer(seconds=self.speed_slider.value())
            if self.head in self.body:
                self.Died()
            elif self.head[0] >= WIDTH or self.head[0] < 0 or self.head[1] < 0 or self.head[1] >= HEIGHT:
                self.Died()            
            if self.head == self.food_pos:
                self.body.append(self.head[:])
                self.food_pos = [random.randint(0, WIDTH) // self.size * self.size for x in range(2)]
        hapi.fill(hapi.color['green'])
        hapi.rect(self.food_pos[0], self.food_pos[1], self.size-1, self.size-1)

    def Died(self):
        self.head = [x * self.size for x in self.start_pos]
        self.body = []
        self.food_pos = [random.randint(0, WIDTH) // self.size * self.size for x in range(2)]
        self.direction = [0, 0]
        self.current_screen = self.Main_menu

    def Settings(self):
        hapi.background(hapi.color['white'])
        self.speed_slider.update()
        self.rows_slider.update()
        hapi.fill(hapi.color['black'])
        hapi.font_size(30)
        hapi.text(self.slider_names[0], 100, 200)
        hapi.text(self.slider_names[1], 100, 300)
        if self.back_btn.update():
            self.current_screen = self.Main_menu

    def Main_menu(self):
        hapi.background(hapi.color['white'])
        hapi.fill(hapi.color['black'])
        hapi.font_size(50)
        hapi.text("Snake", 220, 30)
        if self.menu_btn_quit.update():
            hapi.is_running = False
        if self.menu_btn_start.update():
            self.current_screen = self.Main
        if self.settings_btn.update():
            self.current_screen = self.Settings

    def Events(self, event):
        if event.type == pygame.QUIT:
            hapi.is_running = False
        elif event.type == pygame.KEYDOWN:
            if event.unicode == "a" or event.key == 276:
                self.direction = [-self.size, 0]
            elif event.unicode == "d" or event.key == 275:
                self.direction = [self.size, 0]
            elif event.unicode == "w" or event.key == 273:
                self.direction = [0, -self.size]
            elif event.unicode == "s" or event.key == 274:
                self.direction = [0, self.size]

if __name__ == '__main__':
    game = Game()
    game.Start()

For about the same number of lines for a simple snake game, you get one complete with menus, interfaces and settings:

tutorials

demos

color change

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.no_stroke()
    mx = (hapi.mouseX() / hapi.WIDTH) * 255

    hapi.fill((0, mx, 0))
    for i in range(50 , 200, 60):
        hapi.rect(i, 50, 30, 30)

    hapi.fill((255, 0, 0))
    hapi.ellipse(hapi.mouseX(), hapi.mouseY(), 10, 10)

    hapi.stroke_size(1)
    hapi.stroke((255, 10, 10))
    hapi.line(0, hapi.mouseY(), hapi.mouseX()-10, hapi.mouseY())

    hapi.flip_display()
    hapi.event_loop()

pygame.quit()

lines

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.flip_display()
    hapi.event_loop()

pygame.quit()

squares

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

size = 50
while hapi.is_running:
    hapi.background((255, 255, 255))

    hapi.no_stroke()
    hapi.fill((0, 255, 0))
    hapi.rect(10, 10, size, size)
    hapi.fill((255, 255, 0))
    hapi.rect(100, 100, size, size)
    hapi.fill((255, 0, 0))
    hapi.rect(100, 10, size, size)
    hapi.fill((0, 0, 255))
    hapi.rect(10, 100, size, size)

    hapi.flip_display()
    hapi.event_loop()

pygame.quit()

buttons

from hooman import Hooman

import pygame

window_width, window_height = 500, 500
hapi = Hooman(window_width, window_height)

bg_col = (255, 255, 255)

# the function that gets called when the button is clicked on
def button_clicked(this):
    if this.y == 250:
        this.y = 300
    else:
        this.y = 250


grey_style = {
    "background_color": (200, 200, 200),
    "hover_background_color": (220, 220, 220),
    "curve": 0.1,
    "padding_x": 5,
    "padding_y": 5,
    "font_size": 15,
}


def button_hover_enter(this):
    hapi.set_background(hapi.color["green"])


def button_hover_exit(this):
    hapi.set_background(hapi.color["white"])


stylex = grey_style.copy()
stylex["on_hover_enter"] = button_hover_enter
stylex["on_hover_exit"] = button_hover_exit

button1 = hapi.button(150, 150, 100, 100, "Click Me", grey_style)

buttonx = hapi.button(150, 10, 100, 100, "Hover Me", stylex)

button2 = hapi.button(
    150,
    250, 100, 100,
    "No Click Me",
    {
        "background_color": (200, 200, 200),
        "hover_background_color": (220, 220, 220),
        "outline": True,
        "outline_color": (200, 200, 200),
        "outline_thickness": 5,
        "curve": 0.3,
        "on_click": button_clicked,
        "padding_x": 40,
        "padding_y": 10,
        "font_size": 15,
    },
)


def handle_events(event):
    if event.type == pygame.QUIT:
        hapi.is_running = False
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            hapi.is_running = False


hapi.handle_events = handle_events

clock = pygame.time.Clock()

hapi.set_background(hapi.colour["white"])

while hapi.is_running:

    if button1.update():  # if the button was clicked
        bg_col = (255, 0, 0) if bg_col == (255, 255, 255) else (255, 255, 255)
        hapi.set_background(bg_col)

    # for i in range(5):
    #     x = hapi.button(10+i*80, hapi.mouseY(), "Click Me",
    #         grey_style
    #     )
    # don't use it for ui elements in loop lile the above
    # each element can also be individually
    # updated
    hapi.update_ui()
    hapi.event_loop()

    hapi.flip_display()

    clock.tick(60)

pygame.quit()

transparent circles

import pygame
from hooman import Hooman
pygame.init()

hapi = Hooman(800, 600)


while hapi.is_running:
    hapi.background(hapi.color['white'])


    hapi.set_alpha(100)
    hapi.fill(hapi.color['red'])
    hapi.alpha_ellipse(100, 100, hapi.mouseX()//2, hapi.mouseX()//2)
    hapi.fill(hapi.color['yellow'])
    hapi.alpha_ellipse(100, 100, 100, 100)
    hapi.fill(hapi.color['green'])
    hapi.alpha_ellipse(150, 100, 100, 100)
    pygame.display.flip()

    hapi.event_loop()

pygame.quit()

supershapes

from hooman import Hooman
import numpy
from math import pow
from math import sqrt
import pygame

window_width, window_height = 500, 500
hapi = Hooman(window_width, window_height)

bg_col = (255, 255, 255)


def handle_events(event):
    if event.type == pygame.QUIT:
        hapi.is_running = False
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            hapi.is_running = False


hapi.handle_events = handle_events

clock = pygame.time.Clock()


while hapi.is_running:
    hapi.background(bg_col)

    hapi.fill(hapi.color['red'])

    #hapi.text(n1, 10+hapi.mouseX(), 10+hapi.mouseY())

    hapi.smooth_star(100, 100, 100, 100)
    hapi.oil_drop(100, 250, 100, 100)
    hapi.flowing_star(100, 350, 100, 100)

    hapi.flip_display()
    hapi.event_loop()

pygame.quit()

cross hair

from hooman import Hooman
from math import pow
from math import sqrt

window_width, window_height = 500, 500
hapi = Hooman(window_width, window_height)

bg_col = (255, 255, 255)


def handle_events(event):
    if event.type == pygame.QUIT:
        hapi.is_running = False
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            hapi.is_running = False

hapi.handle_events = handle_events

while hapi.is_running:
    hapi.background(bg_col)

    hapi.stroke(hapi.color['red'])
    hapi.stroke_size(2)

    mouse_coord = (hapi.mouseX(), hapi.mouseY())
    hapi.cross_hair(mouse_coord)

    hapi.flip_display()
    hapi.event_loop()

pygame.quit()

constrain

from hooman import Hooman
import numpy
from math import pow
from math import sqrt
import pygame

window_width, window_height = 500, 500
hapi = Hooman(window_width, window_height)

bg_col = (255, 255, 255)


def handle_events(event):
    if event.type == pygame.QUIT:
        hapi.is_running = False
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            hapi.is_running = False


hapi.handle_events = handle_events


def fake_slider(pos, x, y, width):
    size = 10
    hapi.fill(hapi.color['grey'])
    hapi.rect(x, y, width, size)
    hapi.fill((200, 200, 200))
    hapi.rect(pos, y, size, size)

while hapi.is_running:
    hapi.background(bg_col)

    fake_slider(hapi.mouseX(), 0, 450, hapi.WIDTH)

    reflected_val = hapi.constrain(hapi.mouseX(), 0, 500, 0, 255)
    reflected_col = (reflected_val, reflected_val, reflected_val)
    hapi.fill(reflected_col)
    hapi.rect(10, 10, 100, 100)

    reflected_val2 = hapi.constrain(hapi.mouseX(), 0, 500, 100, 200)
    fake_slider(reflected_val2, 100, 200, 100)

    hapi.flip_display()
    hapi.event_loop()

pygame.quit()

graphs

from hooman import Hooman
from collections import OrderedDict
import pygame
import random

window_width, window_height = 500, 500
hapi = Hooman(window_width, window_height)

bg_col = (255, 255, 255)



while hapi.is_running:
    hapi.background(bg_col)

    hapi.piechart(100, 100, 50, [
        ['a', 20, hapi.color['red']],
        ['b', 30, hapi.color['blue']],
        ['c', 40, hapi.color['yellow']],
        ['d', 60, hapi.color['green']],
        ['e', 30, hapi.color['black']]
    ], start_rad=20)

    hapi.barchart(
        190, 30, 200, 200, {
        "data": {"a": 10, "b": 20, "c": 90},
        "mouse_line": True
        }
    )

    hapi.linechart(
        30,
        270,
        200,
        100,
        {
            "data": [[0, 0], [100, 100], [200, 20], [300, 200]],
            "mouse_line": True,
            "range_y": [0, 200],
            "range_x": [0, 300],
        },
    )

    hapi.event_loop()
    hapi.flip_display()

pygame.quit()

All Demos

Docs

Attributes

.WIDTH

  • hapi.WIDTH is gives the width of the screen

.HEIGHT

  • hapi.HEIGHT is gives the height of the screen

.is_running

  • if loop is running

.screen

still exposes a screen to draw with any pygame shape

pygame.draw.arc(hapi.screen, (255, 0, 0), [80,10,200,200], hapi.PI, hapi.PI/2, 2)

Constants

.PI

The value of pi as provided by the maths module

pygame.draw.arc(hapi.screen, (255, 0, 0), [80,10,200,200], hapi.PI, hapi.PI/2, 2)

Colors, strokes & Fill

.fill

  • used for colouring next shapes
  • hapi.fill((100, 100, 100)) for r g b
  • hapi.fill(100) same as hapi.fill((100, 100, 100))

.stroke

  • used to set color of next shapes' outlines
  • hapi.stroke((100, 100, 100)) for r g b
  • hapi.stroke(100) same as hapi.stroke((100, 100, 100))

.background

  • used to set background color of screen
  • hapi.background((100, 100, 100)) for r g b
  • hapi.background(100) same as hapi.background((100, 100, 100))

.set_background

  • used to have the background drawn every frame automatically
  • hapi.set_background((100, 100, 100))
  • same as hapi.background((100, 100, 100))

.color

same as

{
    'red': (255, 0, 0),
    'green': (0, 255, 0),
    'blue': (0, 0, 255),
    'black': (0, 0, 0),
    'white': (255, 0, 0),
    'yellow': (255, 255, 0),
    'grey': (100, 100, 100)
}

also .colors, .colours, .colour same

Size

.stroke_size

  • used to control thickness of lines and outlines
  • hapi.stroke_size(size) where size is an int

.no_stroke

  • set lines and outlines thickness to 0
  • hapi.no_stroke()
  • same as hapi.stroke_size(0)

.font_size

  • sets font size of text
  • hapi.font_size(12)

Basic elements

.rect

hapi.rect(x, y, width, height)

  • x - x coordinate
  • y - y coordinate

.ellipse

hapi.ellipse(x, y, width, height)

  • x - x coordinate
  • y - y coordinate

.line

hapi.line(x1, y1, x2, y2)

  • x1 - x coordinate of first point
  • y1 - y coordinate of first point
  • x2 - x coordinate of second point
  • y2 - y coordinate of second point

.text

.text(letters, x, y)

  • letters - string of chars eg. 'abcd'
  • x - x coordinate
  • y - y coordinate
  • will convert any type passed to string
  • hapi.text(5, 10, 10) is valid
  • hapi.text(hapi.mouseX(), 10, 10) is valid out of the box

.polygon

.polygon(coords, fill=True)

  • coords is a 2d array [(0,0), (10, 10), (10, 100)]
  • if fill is False, only the outline will be drawn
  • adjust outline with .stroke_size

.begin_shape

hapi.begin_shape() starts drawing a polygon

.vertex

.vertex((100, 200))

.end_shape

hapi.end_shape(fill=True) draws polygon on closing

Minimal demo of .begin_shape, .vertex and .end_shape

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(hapi.color['white'])

    hapi.fill(hapi.color['blue'])
    hapi.stroke_size(4)

    hapi.begin_shape()
    hapi.vertex((0, 0))
    hapi.vertex((100, 0))
    hapi.vertex((hapi.mouseX(), hapi.mouseY()))
    hapi.end_shape()

    # same as hapi.polygon([(0, 0), (100, 0), (hapi.mouseX(), hapi.mouseY())])

    hapi.flip_display()
    hapi.event_loop()

pygame.quit()

.heart

.heart(x, y, w, h)

  • x - x coordinate
  • y - y coordinate
  • w - width
  • h - height

.star

.star(x, y, r1, r2, npoints)

  • x - x coordinate
  • y - y coordinate
  • r1 - radius on the x axis, same as half width
  • r2 - radius on the y axis, same as half height
  • npoints - the number of points of the star, this is a minimum of 2

.curve_rect

.curve_rect(x, y, w, h, curve)

  • x - x coordinate
  • y - y coordinate
  • w - width
  • h - height
  • curve - the percentage of curve with 0 being no curve and 100 being full curve

.arrow

.arrow(x, y, w, h)

  • x - x coordinate
  • y - y coordinate
  • w - width
  • h - height

.alpha_ellipse

.alpha_ellipse(x, y, w, h)

  • x - x coordinate
  • y - y coordinate
  • w - width
  • h - height

.regular_polygon

.regular_polygon(x, y, w, h, npoints, angle_offset)

  • x - x coordinate
  • y - y coordinate
  • w - width
  • h - height
  • npoints - the number of points/corners of the polygon eg. 4 is a square
  • angle_offset - the first point will be drawn from the top, this moves it to θ degrees anti-clockwise

.supershape

  • note see paulbourke.net/geometry/supershape/ on how to use supershape
  • there is also presets for this below

.supershape(x, y, w, h, options)

  • x - x coordinate
  • y - y coordinate
  • w - width
  • h - height
  • options - optional options for the shape

optional options

  • n1
  • n2
  • n3
  • m
  • a
  • b
  • phi

.smooth_star

note this is a preset for supershape

.smooth_star(x, y, w, h, n1=0.20, fill=False)

  • x - x coordinate
  • y - y coordinate
  • w - width
  • h - height
  • n1 - controls the smoothness of the star, this is between 0 and 1
  • fill - when set to False, only the outline will be drawn

.oil_drop

note this is a preset for supershape

.oil_drop(x, y, w, h, n1=0.3, fill=False)

  • x - x coordinate
  • y - y coordinate
  • w - width
  • h - height
  • n1 - controls the size of the drop, must be between 0 and 1
  • fill - when set to False, only the outline will be drawn

.flowing_star

note this is a preset for supershape

.flowing_star(x, y, w, h, n1=0.3, fill=False)

  • x - x coordinate
  • y - y coordinate
  • w - width
  • h - height
  • n1 - controls the inflation of the shape, must be between 0 and 1
  • fill - when set to False, only the outline will be drawn

.gradient_rect

.gradient_rect(x, y, w, h, start_col, end_col, direction=0)

  • x - x coordinate
  • y - y coordinate
  • w - width
  • h - height
  • start_col - this is the color it starts with
  • end_col - this is the color it ends with
  • direction - the direction of the gradient, 0 is horizontal where it starts on the left and 1 is vertical where is starts on the top

.cross_hair

.cross_hair(coord)

  • coord - the x and y position of the center of the cross_hairs

Interactivity

.mouseX

  • hapi.mouseX() gives the current x coordinate of the mouse

.mouseY

  • hapi.mouseY() gives the current y coordinate of the mouse

Pygame specifics

.flip_display

  • is just pygame.display.flip() behind the scene

.event_loop

requires

def handle_events(event):
    if event.type == pygame.QUIT:
        hapi.is_running = False

hapi.handle_events = handle_events
  • is put inside hapi.is_running loop

.set_caption

same as pygame.display.set_caption

Ui

.update_ui

  • no need to update each element if you call this
  • called inside hapi.is_running loop
  • here is when NOT to use it:
while hapi.is_running:
    for i in range(5):
            x = hapi.button(10+i*80, hapi.mouseY(), "Click Me",
                grey_style
            )
        hapi.update_ui()

.button

Create a button with hapi.button(x, y, text, [optional paramters])

  • x - x location of the button
  • y - y location of the button
  • text - the text on the button
  • [optional parameters] - a dictionary of any extra options you want for the button listed below

Optional Parameters

  • surface - the surface you want the button on, by default it is the main window
  • background_color - the color of the button background
  • hover_background_color - the color of the button background when the mouse is over the button
  • font - the font of the text, by default it is Calibri
  • font_size - the size of the text, by default it is 30
  • font_colour - the colour of the text, by default it is black
  • outline - when set to True, the button will have an outline when the mouse hovers over
  • outline_thickness - this is the thickness of the outline
  • outline_color - the colour of the outline
  • outline_half - when set to True, it will create an outline for the bottom and right side of the button
  • action - this is a function that gets called when the button is clicked
  • action_arg - if the function given in action requires a parameter, you can use this to send to the function
  • image - this should be a pygame.Surface() object that the button will show instead
  • hover_image - this should be a pygame.Surface() object that the button will show when the mouse is over the button
  • enlarge - this will resize the button when the mouse is over the button, this should be a bool
  • enlarge_amount - this is the percentage that you want the button to resize to when the mouse is over the button (1 = no change)
  • calculate_size - when set to True, this will calculate the width and height of the button from the size of the text
  • padding_x - an integer that is added on to the width on both sides of the text when calculate_size is set to True
  • padding_y - an integer that is added on to the height on both sides of the text when calulate_size is set to True
  • dont_generate - when set to True, the button will not generate the images to put on screen, this can be handy if you want to use calculate_size without supplying text, you will need to call button.update_text() to generate the images before drawing
  • curve - the amount of curve you want the button to have on the edges with 0 being no curve and 1 being full curve, by default it is 0
  • on_hover_enter - this is a function that gets called when the mouse enters the button, the first frame it hovers over
  • on_hover_exit - this is a function that gets called when the mouse exits the button, the frame once it stops hovering over
  • on_click - this is a function that gets called when the mouse clicks the button, this only gets called once, even if mouse i being held down
def on_hover_enter(this): # this refers to the button
    this.background_color = hapi.color['blue']

button = hapi.button(150, 250, "Click Me",
        {'on_hover_enter':on_hover_enter}
    )

Methods

  • update() - this updates the button and draws it on screen, this should be called every frame
  • Update_text(text) - this changes the text and recreates the button
  • get_rect() - this returns a pygame.Rect of the button
  • width() - this returns the width of the button
  • height() - this returns the height of the button
  • create_button() - this applies any changes to the button

.slider

.slider(x, y, w, h, [optional parameters])

  • x - x coordinate
  • y - y coordinate
  • w - width
  • h - height
  • optional parameters - a dictionary of optional options

optional parameters

  • background_color - the background color of the slider
  • slider_width - the width of slider, by default it is the same as the height
  • slider_color - the color of the slider
  • starting_value - the starting value of the slider, by default it is in the middle of the range
  • value_range - the range that the slider ranges between, by default it is between 0 and 1

Methods

  • update() - this updates the slider and draws it on screen, this should be called every frame
  • value() - this returns the current value of the slider
  • set_value(value) - given a integer or float, this sets the value and moves the slider

.slider_with_text

.slider_with_text(slider, [optianl parameters])

  • slider - a .slider widget
  • optional parameters - a dictionary of optional options

optional parameters

  • font - the font of the text, by default it is Calibri
  • font_size - the size of the text, by default it is 20
  • font_color - the color of the text, by defualt it is black
  • padding_x - how far off the horizontal sides of the slider should the text be drawn, in pixels, by default it is 0
  • padding_y - how far off the vertical sides of the slider should the text be drawn, in pixels, by default it is 0
  • pivot - where the text is located in relation to the slider, this can be "top", "top_left", "bottom_right" etc, default is "top_left"
  • accuracy - how many decimal points the value has, by default it is 0

Methods

  • update() - this updates the text and the given slider
  • value() - this returns the value of the given slider

.scroll

.scroll([optional parameters])

  • optional parameters - a dictionary of optional options

optional parameters

  • starting_x - the starting position of the horizontal scroll
  • starting_y - the starting position of the vertical scroll
  • range_x - the amount of extra pixels the scroll moves the screen by for the horizontal
  • range_y - the amount of extra pixel the scroll moves the screen by for the vertical
  • bar_color - the color of the bar, background of the slider
  • slider_color - the color of the slider, the forground of the slider

Methods

-update() - this updates the scroll widget

how to use the scroll

  • use scroll_widget[0] to get the amount of horizontal scroll and scroll_widget[1] to get the amount of vertical scroll

eg.

scroll_obj = hapi.scroll(params)

while hapi.running:
    hapi.rect(100 + scroll_obj[0], 100 + scroll_obj[1], 50, 50)
    scroll_obj.update()

.textbox

.textbox(x, y, w, h=0, [optional parameters])

  • x - x coordinate
  • y - y coordinate
  • w - width
  • h - height of each line
  • optional parameters - a dictionary of optional options

optional parameters

  • lines - the amount of lines the textbox has
  • text - the starting text it has
  • background_color - the color of the background
  • font_size - the size of the text font
  • font - the font of the text
  • text_color - the color of the text
  • surface - the surface to put the textbox on, by default it is the main window
  • margin - the offset from the left side that the text starts from
  • cursor - a cursor to show you where you are typing, by default it is on
  • Enter_action - a function that gets called when enter is pressed
  • calculateSize - when True, it will calculate the height based off the height of the text

Methods

  • update() - this updates the textbox and draws it on screen, this should be called every frame
  • get_lines(lines=1, return_as_string=False) - this returns the text in the textbox, lines can be the line number (starting from 1) or a range of lines eg. (1,4) gets lines 1 to 4, when return_as_string is False, it will return each line in a list
  • key_down(event) - when a KEYDOWN event happens, giving it to this method updates the textbox
def handle_events(event):
    if event.type == pygame.KEYDOWN:
        .keydown(event)

Contributing Notes

  • Demos: Include your name and Github URL in a docstring at the top of the demo file
You might also like...
OpenGL experiments with Pygame & ModernGL
OpenGL experiments with Pygame & ModernGL

pygame-opengl OpenGL experiments with Pygame & ModernGL TODO Skybox & Reflections Post-process effects (motion blur, color correction, etc..) Normal m

A short non 100% Accurate Solar System in pygame

solar-system-pygame Controls UP/DOWN for Emulation Speed Control ESC for Pause/Unpause q to Quit c or ESC again to Continue LEFT CLICK to Add an orbit

A Street Fighter game in Pygame
A Street Fighter game in Pygame

What is Street Fighter? Street Fighter, commonly abbreviated as SF or スト, is a Japanese competitive fighting video game franchise developed and publis

Simple car game written in PyGame

Welcome to CarGame 👋 Car Game written in PyGame! NOTE: This is still new and there may be stuff broken... 🏠 Homepage Install install pygame by typin

A Pygame application which generates mazes using randomized DFS (Depth-First-Search)
A Pygame application which generates mazes using randomized DFS (Depth-First-Search)

Maze-Generator-with-Randomized-DFS A Pygame application which generates mazes using randomized DFS (Depth-First-Search)-(Iterative implementation). Ra

FlappyBird game with python and pygame

FlappyBird game with python and pygame

An interactive pygame implementation of Conway's Game of Life
An interactive pygame implementation of Conway's Game of Life

Game of Life An interactive pygame implementation of Conway's Game of Life Installation Clone the repo and navigate into it. git clone https://github.

A Gomoku game GUI using pygame where the user can choose to play against another player or an AI using minimax with alpha-beta pruning

Gomoku A GUI based Gomoku game using pygame where the user can choose to play against another player or an AI using minimax with alpha-beta pruning. R

Made by Ashish and Avinash-sord12k. Powered by pygame
Made by Ashish and Avinash-sord12k. Powered by pygame

Spook_alle About -Made by Ashish (Github: Ashish-Github193) and Avinash-sord12k Version - BETA v_1.0 /1-11-2021/ (game is at its base version more ite

Comments
  • Error in Buttons.py running on Manjaro Linux

    Error in Buttons.py running on Manjaro Linux

    https://github.com/Abdur-rahmaanJ/hooman/blob/1ab113cd96111ab55ceacdc38ce996fbb71850a9/hooman/demos/Buttons.py#L45

    Error with Hooman 0.3.6:

    ❯ /home/jon/git/hooman_test/.venv/bin/python3.8 /home/jon/git/hooman_test/hooman_test/test1.py
    pygame 2.0.0 (SDL 2.0.12, python 3.8.6)
    Hello from the pygame community. https://www.pygame.org/contribute.html
    Traceback (most recent call last):
      File "/home/jon/git/hooman_test/hooman_test/test1.py", line 45, in <module>
        button1 = hapi.button(150, 150, "Click Me", grey_style)
      File "/home/jon/git/hooman_test/.venv/lib/python3.8/site-packages/hooman/hooman.py", line 334, in button
        b = Button(*args, **kwargs)
    TypeError: __init__() missing 1 required positional argument: 'text'
    
    opened by JFincher42 1
  • UI Overhaul

    UI Overhaul

    • create a base ui widget class which all other widgets are derived from
    • update docs (they are super out of date)
    • make the textbox more of an input field, very hard to use atm
    • fix all the bugs!!!!!
    bug documentation enhancement help wanted 
    opened by TheBigKahuna353 2
  • Tests and error catching

    Tests and error catching

    Thanks @JFincher42 for notifying

    We need to write tests to catch some cases like 2 params in .fill etc

    More about introducing the testing culture in the lib

    opened by Abdur-rahmaanJ 0
Releases(v0.9.3)
  • v0.9.3(Oct 14, 2022)

    What's Changed

    • Add default framerate=25 argument to save_record by @carlosguealv in https://github.com/Abdur-rahmaanJ/hooman/pull/35
    • Change "README.md", add "Examples of possibilities.md" by @Kirastel in https://github.com/Abdur-rahmaanJ/hooman/pull/36
    • UI overhaul by @TheBigKahuna353 in https://github.com/Abdur-rahmaanJ/hooman/pull/40
    • fixed demos by @TheBigKahuna353 in https://github.com/Abdur-rahmaanJ/hooman/pull/41
    • 1st rectangle of 1st face of rubix cube by @kareena14 in https://github.com/Abdur-rahmaanJ/hooman/pull/42
    • Fixed the setup by @Nythiennzo in https://github.com/Abdur-rahmaanJ/hooman/pull/44
    • Demo/adding sketch pad by @kherin in https://github.com/Abdur-rahmaanJ/hooman/pull/45

    New Contributors

    • @carlosguealv made their first contribution in https://github.com/Abdur-rahmaanJ/hooman/pull/35
    • @Kirastel made their first contribution in https://github.com/Abdur-rahmaanJ/hooman/pull/36
    • @kareena14 made their first contribution in https://github.com/Abdur-rahmaanJ/hooman/pull/42
    • @Nythiennzo made their first contribution in https://github.com/Abdur-rahmaanJ/hooman/pull/44
    • @kherin made their first contribution in https://github.com/Abdur-rahmaanJ/hooman/pull/45

    Full Changelog: https://github.com/Abdur-rahmaanJ/hooman/compare/v0.8.2...v0.9.3

    Source code(tar.gz)
    Source code(zip)
  • v0.8.2(May 5, 2022)

  • v0.8.0(May 4, 2022)

  • v0.7.1(May 1, 2022)

  • v0.7.0(Apr 27, 2022)

  • v0.4.0(Apr 18, 2022)

  • v0.3.7.1(Aug 23, 2021)

Owner
Abdur-Rahmaan Janhangeer
C++, Java & Python. FlaskCon organising member. Watch out for the next edition!
Abdur-Rahmaan Janhangeer
Lutris helps you install and play video games from all eras and from most gaming systems.

Lutris Lutris helps you install and play video games from all eras and from most gaming systems. By leveraging and combining existing emulators, engin

Pop!_OS 2 Nov 15, 2021
A script to install a Windows game through GOG on Linux

proton-gog-install A script to install a Windows game through GOG on Linux. Based on this gist. Tested on Arch, might work elsewhere. Requirements: Im

null 5 Dec 28, 2022
A Pygame game made in 48 hours

Flappuccino Flappuccino is a game created in 48 hours for the PyGame Community New Years Jam using Python with Pygame. Screenshots Background Informat

PolyMars 242 Jan 2, 2023
Quiz game made entirely with python and pygame for school work

Tabela de conteúdo Descrição Como instalar Linguagens usadas Contribuidores Créditos Problemas com o jogo? Contate-nos Descrição Quiz feito inteiramen

null 3 Apr 12, 2022
Recreation of HexGame in Pygame. More features will come soon !

Hex with Pygame Historical point of view What Are the rules of this game ? Some Strategies and tips The algorithm for the Win Other fonctionnalities W

null 4 Mar 26, 2022
This is a simple game made using pygame.

Ball breaker This is a simple game made using pygame game view The game view have been updated wait for the new view to be uploaded Game_show.mp4 Lear

Rishikesh Kumar 3 Nov 5, 2021
A simple matrix code rain created using Python with Pygame.

Matrix4_code_rain A simple matrix code rain created using Python with Pygame. To run the code you will need Pygame and MS Mincho font. Create a projec

null 7 Nov 6, 2022
null 3 Oct 22, 2021
This is an amazing game make using pygame.

This is an awesome balloon game. It is made in python using Pygame library. It is a project game while learning game development.

Rishikesh Kumar 2 Oct 10, 2021
Pendulum Simulation using Pygame

Pendulum project, built using pygame and math modules.

null 3 Nov 9, 2021