tokyoedtech

Purple Rain 2.py

Jan 3rd, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | None | 0 0
  1. #Purple Rain Simulator in Python 3 Using Turtle Graphics
  2. #Reddit Challenge: https://www.reddit.com/r/learnprogramming/comments/5hvvfj/great_coding_challenge_for_beginners_purple_rain/
  3.  
  4. import os
  5. os.system("clear")
  6.  
  7. import random
  8.  
  9. import turtle
  10. turtle.fd(0)
  11. #Set the screensize
  12. turtle.setup(width=800, height=600)
  13. turtle.ht()
  14. turtle.tracer(0)
  15. turtle.bgcolor("black")
  16. turtle.title("Purple Rain...Purple Rain!")
  17.  
  18.  
  19.  
  20. class Drop(turtle.Turtle):
  21.     def __init__(self):
  22.         turtle.Turtle.__init__(self)
  23.         self.speed(0)
  24.         self.penup()
  25.         self.goto(random.randint(-300, 300), random.randint(400, 2000))
  26.         self.shape("square")
  27.         self.setheading(270)
  28.        
  29.         self.type = random.randint(1,3)
  30.         if self.type == 1:
  31.             self.color("#FF0080")
  32.             self.shapesize(stretch_wid=0.3, stretch_len=2, outline=None)
  33.             self.myspeed = random.randint(15, 20)
  34.         elif self.type == 2:
  35.             self.color("#8A2BE2")
  36.             self.shapesize(stretch_wid=0.2, stretch_len=1, outline=None)
  37.             self.myspeed = random.randint(10, 15)
  38.         elif self.type == 3:
  39.             self.color("#DDA0DD")
  40.             self.shapesize(stretch_wid=0.1, stretch_len=0.8, outline=None)
  41.             self.myspeed = random.randint(5, 10)
  42.  
  43. class Particle(turtle.Turtle):
  44.     def __init__(self, spriteshape, color, startx, starty):
  45.         turtle.Turtle.__init__(self)
  46.         self.speed(0)
  47.         self.penup()
  48.         self.shapesize(stretch_wid=0.3, stretch_len=0.3, outline=None)
  49.         self.goto(-1000,-1000)
  50.         self.frame = 0
  51.         self.color(color)
  52.         self.shape(spriteshape)
  53.         self.gravity = 1
  54.         self.dx = 0
  55.         self.dy = 0
  56.         self.dz = 0
  57.         self.size = 0.3
  58.        
  59.     def explode(self, startx, starty):
  60.         self.goto(startx,starty)
  61.         self.dx = random.randint(-10, 10)
  62.         self.dy = random.randint(10, 20)
  63.         self.dz = random.randint(-10, 10)
  64.         self.frame = 1
  65.  
  66.     def move(self):
  67.         if self.frame > 0:
  68.             self.dy -= self.gravity
  69.             self.goto(self.xcor() + self.dx, self.ycor() + self.dy)
  70.             self.frame += 1
  71.             self.size += self.dz / 400
  72.             if self.size == 0:
  73.                 self.size = 0.005
  74.             self.shapesize(stretch_wid=self.size, stretch_len=self.size, outline=None)
  75.  
  76. #       if self.frame > 20:
  77. #           self.frame = 0
  78. #           self.goto(-1000, -1000)
  79.  
  80.  
  81. #Create a list of raindrops
  82. drops =[]  
  83.  
  84. for i in range(0, 30):
  85.     drop = Drop()
  86.     drops.append(drop)
  87.  
  88. #Create particles
  89. particles = []
  90. for i in range(30):
  91.     particles.append(Particle("circle", "purple", 0, 0))
  92.  
  93. while True:
  94.     for drop in drops:
  95.         drop.fd(drop.myspeed)
  96.         if drop.ycor() < -290:
  97.  
  98.             for particle in particles:
  99.                 if random.randint(0, 100) >70 or particle.ycor() < -300 or particle.size < 0.05:
  100.                     particle.frame = 0
  101.                     particle.color(drop.pencolor())
  102.                     if drop.type == 1:
  103.                         particle.shapesize(stretch_wid=0.3, stretch_len=0.3, outline=None)
  104.                         particle.size = 0.3
  105.                         particle.gravity = 1
  106.                     elif drop.type == 2:
  107.                         particle.size = 0.2
  108.                         particle.shapesize(stretch_wid=0.2, stretch_len=0.2, outline=None)
  109.                         particle.gravity = 2
  110.                     if drop.type == 3:
  111.                         particle.size = 0.1
  112.                         particle.shapesize(stretch_wid=0.1, stretch_len=0.1, outline=None)
  113.                         particle.gravity = 3
  114.  
  115.  
  116.                     particle.explode(drop.xcor(), drop.ycor())
  117.  
  118.  
  119.             drop.goto(random.randint(-400, 400), random.randint(400, 3000))
  120.  
  121.     for particle in particles:
  122.         particle.move()
  123.  
  124.     turtle.update()
  125.  
  126. delay = input("Press enter to finish. > ")
Add Comment
Please, Sign In to add comment