Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Purple Rain Simulator in Python 3 Using Turtle Graphics
- #Reddit Challenge: https://www.reddit.com/r/learnprogramming/comments/5hvvfj/great_coding_challenge_for_beginners_purple_rain/
- import os
- os.system("clear")
- import random
- import turtle
- turtle.fd(0)
- #Set the screensize
- turtle.setup(width=800, height=600)
- turtle.ht()
- turtle.tracer(0)
- turtle.bgcolor("black")
- turtle.title("Purple Rain...Purple Rain!")
- class Drop(turtle.Turtle):
- def __init__(self):
- turtle.Turtle.__init__(self)
- self.speed(0)
- self.penup()
- self.goto(random.randint(-300, 300), random.randint(400, 2000))
- self.shape("square")
- self.setheading(270)
- self.type = random.randint(1,3)
- if self.type == 1:
- self.color("#FF0080")
- self.shapesize(stretch_wid=0.3, stretch_len=2, outline=None)
- self.myspeed = random.randint(15, 20)
- elif self.type == 2:
- self.color("#8A2BE2")
- self.shapesize(stretch_wid=0.2, stretch_len=1, outline=None)
- self.myspeed = random.randint(10, 15)
- elif self.type == 3:
- self.color("#DDA0DD")
- self.shapesize(stretch_wid=0.1, stretch_len=0.8, outline=None)
- self.myspeed = random.randint(5, 10)
- class Particle(turtle.Turtle):
- def __init__(self, spriteshape, color, startx, starty):
- turtle.Turtle.__init__(self)
- self.speed(0)
- self.penup()
- self.shapesize(stretch_wid=0.3, stretch_len=0.3, outline=None)
- self.goto(-1000,-1000)
- self.frame = 0
- self.color(color)
- self.shape(spriteshape)
- self.gravity = 1
- self.dx = 0
- self.dy = 0
- self.dz = 0
- self.size = 0.3
- def explode(self, startx, starty):
- self.goto(startx,starty)
- self.dx = random.randint(-10, 10)
- self.dy = random.randint(10, 20)
- self.dz = random.randint(-10, 10)
- self.frame = 1
- def move(self):
- if self.frame > 0:
- self.dy -= self.gravity
- self.goto(self.xcor() + self.dx, self.ycor() + self.dy)
- self.frame += 1
- self.size += self.dz / 400
- if self.size == 0:
- self.size = 0.005
- self.shapesize(stretch_wid=self.size, stretch_len=self.size, outline=None)
- # if self.frame > 20:
- # self.frame = 0
- # self.goto(-1000, -1000)
- #Create a list of raindrops
- drops =[]
- for i in range(0, 30):
- drop = Drop()
- drops.append(drop)
- #Create particles
- particles = []
- for i in range(30):
- particles.append(Particle("circle", "purple", 0, 0))
- while True:
- for drop in drops:
- drop.fd(drop.myspeed)
- if drop.ycor() < -290:
- for particle in particles:
- if random.randint(0, 100) >70 or particle.ycor() < -300 or particle.size < 0.05:
- particle.frame = 0
- particle.color(drop.pencolor())
- if drop.type == 1:
- particle.shapesize(stretch_wid=0.3, stretch_len=0.3, outline=None)
- particle.size = 0.3
- particle.gravity = 1
- elif drop.type == 2:
- particle.size = 0.2
- particle.shapesize(stretch_wid=0.2, stretch_len=0.2, outline=None)
- particle.gravity = 2
- if drop.type == 3:
- particle.size = 0.1
- particle.shapesize(stretch_wid=0.1, stretch_len=0.1, outline=None)
- particle.gravity = 3
- particle.explode(drop.xcor(), drop.ycor())
- drop.goto(random.randint(-400, 400), random.randint(400, 3000))
- for particle in particles:
- particle.move()
- turtle.update()
- delay = input("Press enter to finish. > ")
Add Comment
Please, Sign In to add comment