Pages

Se afișează postările cu eticheta rain effect. Afișați toate postările
Se afișează postările cu eticheta rain effect. Afișați toate postările

joi, 22 septembrie 2016

PyGame : Effects - part 002.

This is another tutorial about pygame and python 2.7 is very simple.
I make this tutorial for educational purposes for the children versus python language.
I used for most common variables the Romanian language and this will allow you to understand well the variables versus python language script.
I used this version of python:
C:\Python27>python.exe
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
This python script comes with few functions:
Imagine_hasurata - fill the image 
ImaginePicatura - draw drop
Pregatire - setup drawing
I let rect variable for python pygame sprite.py ( need to be set the name to rect from rectangle).
Also, I have two python class to make the drop and trail drop into the screen.

This is the effect result:
pygame python
This is the python script:
#! /usr/bin/env python
import pygame, random
from pygame.locals import *

aleator = 1.5
ecran_x = 640
ecran_y = 480
acceleratie = 1.0
marime_picatura = (3, 3)
culoare_start = 255
culoare_stop = 0
culoare_intre = 2

def Imagine_hasurata(f, i):
    lungime = f - i + 1
    interval = (culoare_start - culoare_stop) / (culoare_intre + 1)
    imagini = []
    for x in range(culoare_intre):
        image = pygame.Surface((1, lungime)).convert()
        color = culoare_start - (x + 1)*interval
        image.fill((color, color, color))
        imagini.append(image)
    return imagini

def ImaginePicatura():
    image = pygame.Surface(marime_picatura).convert()
    image.fill((culoare_start, culoare_start, culoare_start))
    return image

def Pregatire(Picatura, trasa):
    y = 0.0
    v = 0.0
    ylist = []
    while int(y) <  ecran_y:
        ylist.insert(0, int(y))
        v = v + acceleratie
        y = y + v
    Picatura.ylist = ylist[:]
    ylist.insert(0, ecran_y)
    trasa.imageset = []
    for i in range(len(ylist) - 1):
        trasa.imageset.insert(0, Imagine_hasurata(ylist[i], ylist[i + 1]))

class Picatura(pygame.sprite.Sprite):
    def __init__(self, x):
        pygame.sprite.Sprite.__init__(self, self.updategroup, self.displaygroup)
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.trasaindex = 0
        self.ynum = len(self.ylist)
    def update(self):
        self.ynum = self.ynum - 1
        if self.ynum < 0:
            self.kill()
        else:
            self.rect.centery = self.ylist[self.ynum]
            Trasare(self, self.trasaindex)
            self.trasaindex = self.trasaindex + 1

class Trasare(pygame.sprite.Sprite):
    def __init__(self, Picatura, trasaindex):
        pygame.sprite.Sprite.__init__(self, self.updategroup)
        self.imagini = self.imageset[trasaindex]
        self.rect = self.imagini[0].get_rect()
        self.rect.midtop = Picatura.rect.center
        self.update = self.start
    def start(self):
        self.add(self.displaygroup)
        self.update = self.fade
        self.imagenum = 0
        self.fade()
    def fade(self):
        if self.imagenum == len(self.imagini):
            self.kill()
        else:
            self.image = self.imagini[self.imagenum]
            self.imagenum = self.imagenum + 1

def main():
    pygame.init()
    ecran = pygame.display.set_mode((ecran_x, ecran_y))
    fundal = pygame.Surface(ecran.get_rect().size)

    updategroup = pygame.sprite.Group()
    displaygroup = pygame.sprite.RenderUpdates()

    Picatura.image = ImaginePicatura()
    Pregatire(Picatura, Trasare)

    for thing in [Picatura, Trasare]:
        thing.updategroup = updategroup
        thing.displaygroup = displaygroup

    ceas = pygame.time.Clock()

    while 1:

        for event in pygame.event.get():
            if event.type == QUIT:
                return

        displaygroup.clear(ecran, fundal)
        updategroup.update()

        if random.random() < aleator:
            Picatura(random.randrange(ecran_x))

        pygame.display.update(displaygroup.draw(ecran))
        ceas.tick(10)

if __name__ == '__main__':
    main()