pygame.examples.liquid

Se hai installato pygame

pip install pygame

puoi eseguire il codice originale scrivendo le istruzioni

import pygame.examples.liquid

pygame.examples.liquid.main()

Versione riarrangiata

Salva l’immagine nella sottocartella data

import pygame as pg
import os           # path.split(), path.join()
import math         # sin()
import time         # sleep()

def main():
    pg.init()
    screen    = pg.display.set_mode((640, 480), pg.HWSURFACE | pg.DOUBLEBUF)
    main_dir  = os.path.split(os.path.abspath(__file__))[0]
    imagename = os.path.join(main_dir, "data", "liquid.bmp")
    bitmap    = pg.image.load(imagename)
    bitmap    = pg.transform.scale2x(bitmap)
    bitmap    = pg.transform.scale2x(bitmap)

    # get the image and screen in the same format
    if screen.get_bitsize() == 8: screen.set_palette(bitmap.get_palette())
    else:                         bitmap = bitmap.convert()

    anim    = 0.0
    xblocks = range(0, 640, 20)   # 0, 20, 40,..., 620
    yblocks = range(0, 480, 20)   # 0, 20, 40,..., 460

    while True:
        for e in pg.event.get():
            if e.type in (pg.QUIT, pg.KEYDOWN, pg.MOUSEBUTTONDOWN):
                return

        anim = anim+0.02
        for x in xblocks:
            xpos = 20+(x+15*(math.sin(anim+x*0.01)))
            for y in yblocks:
                ypos = 20+(y+15*(math.sin(anim+y*0.01)))
                screen.blit(bitmap, (x,y), (xpos,ypos, 20,20))

        pg.display.flip()
        time.sleep(0.005)

if __name__ == "__main__":
    main()
    pg.quit()

Lascia un commento