Last active
January 12, 2019 13:38
-
-
Save jsbueno/f749b00321ec23b176ee7f0b2fbc4f5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import pygame | |
LARGURA, ALTURA = TAMANHO = 800, 600 | |
DROP_SIZE = 50 | |
def init(): | |
global screen, raindrop | |
screen = pygame.display.set_mode(TAMANHO) | |
def le_teclado(): | |
pygame.event.pump() | |
teclas = pygame.key.get_pressed() | |
if teclas[pygame.K_ESCAPE]: | |
raise RuntimeError | |
class Gota: | |
VEL_MIN, VEL_MAX = 5, 15 | |
def __init__(self): | |
self.x = random.randint(0, LARGURA) | |
self.y = 0 | |
self.vel = random.randint(self.VEL_MIN, self.VEL_MAX) | |
self.x_velho = 0 | |
self.y_velho = 0 | |
self.load_image() | |
def load_image(self): | |
raindrop = pygame.image.load('raindrop.png') | |
self.raindrop = pygame.transform.rotozoom(raindrop, 0, DROP_SIZE / raindrop.get_width()) | |
def atualiza(self): | |
screen.blit(self.raindrop, (self.x, self.y)) | |
self.x_velho = self.x | |
self.y_velho = self.y | |
self.y += self.vel | |
self.x += 2 | |
if self.y > ALTURA: | |
self.y = 0 | |
self.x = random.randint(0, LARGURA) | |
self.vel = random.randint(10, 40) | |
def apaga(self): | |
pygame.draw.rect(screen, (0,0,0), (self.x_velho, self.y_velho, DROP_SIZE, DROP_SIZE)) | |
class GotaPequena(Gota): | |
VEL_MIN, VEL_MAX = 20, 40 | |
def load_image(self): | |
raindrop = pygame.image.load('raindrop.png') | |
self.raindrop = pygame.transform.rotozoom(raindrop, 0, 30 / raindrop.get_width()) | |
def main(): | |
gotas = [Gota() for i in range(20)] + [GotaPequena() for i in range(20)] | |
while True: | |
for gota in gotas: | |
gota.apaga() | |
for gota in gotas: | |
gota.atualiza() | |
le_teclado() | |
pygame.display.flip() | |
pygame.time.delay(30) | |
print(__name__) | |
if __name__ == "__main__": | |
try: | |
init() | |
main() | |
except RuntimeError: | |
print("Programa encerrado normalmente") | |
finally: | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment