Skip to content

Instantly share code, notes, and snippets.

@jsbueno
Last active January 12, 2019 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbueno/f749b00321ec23b176ee7f0b2fbc4f5b to your computer and use it in GitHub Desktop.
Save jsbueno/f749b00321ec23b176ee7f0b2fbc4f5b to your computer and use it in GitHub Desktop.
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