Advertisement
HauntJemimah

Untitled

Feb 14th, 2014
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.95 KB | None | 0 0
  1. import random
  2. import math
  3.  
  4. # Requirements:
  5.  
  6. # Modify the prior Create-a-Picture lab, or start a new one.
  7.  
  8. # Animate the image. Try one or more of the following:
  9.  
  10.     # Move an item across the screen.
  11.     # Move an item back and forth.
  12.     # Move up/down/diagonally.
  13.     # Move in circles.
  14.     # Have a person wave his/her arms.
  15.     # Create a stoplight that changes colors.
  16.  
  17. # import statement
  18. import pygame
  19.  
  20. # Define colors:
  21. BLACK = (0, 0, 0)
  22. WHITE = (255, 255, 255)
  23. RED = (255, 0, 0)
  24. GREEN = (0, 255, 0)
  25. BLUE = (0, 0, 255)
  26. LIGHTBLUE = (103, 255, 246)
  27.  
  28. # initialize pygame:
  29. pygame.init()
  30.  
  31. # screen:
  32. size = (700, 500)
  33. screen = pygame.display.set_mode(size)
  34.  
  35. # Set the caption:
  36. pygame.display.set_caption("Chapter 8 Lab")
  37.  
  38. # Clock:
  39. clock = pygame.time.Clock()
  40.  
  41. # FPS:
  42. FPS = 60
  43.  
  44. # boolean variable for game:
  45. done = False
  46.  
  47. # Expanding Rectangle List:
  48. rectList = []
  49.  
  50. for i in range(5):
  51.     rectX = random.randrange(0, 20)
  52.     rectY = random.randrange(0, 20)
  53.     rectList.append([rectX, rectY])
  54.  
  55. rectXChange = 10
  56. rectYChange = 10
  57.  
  58. # For loop for white circles:
  59. whiteCircleList = []
  60.  
  61. for i in range(25):
  62.     x = random.randrange(0, 700)
  63.     y = random.randrange(0, 50)
  64.     whiteCircleList.append([x,y])
  65.  
  66. # For Loop for Blue Circles:
  67. blueCircleList = []
  68.  
  69. for i in range(100):
  70.     circleX = random.randrange(0, 500)
  71.     circleY = random.randrange(0, 700)
  72.     blueCircleList.append([circleX, circleY])
  73.  
  74. # Light Blue Circle For Loop:
  75. lightBlueCircleList = []
  76.  
  77. for i in range(100):
  78.     circleX = random.randrange(0, 500)
  79.     circleY = random.randrange(0, 700)
  80.     lightBlueCircleList.append([circleX, circleY])
  81.  
  82. # Surfboard's Rectangle (x-pos, y-pos, x-length, y-length):
  83. surfboardRect = pygame.Rect(325, 125, 50, 150)
  84.  
  85. boardY = 255.0
  86.  
  87. # Surfboard's First Line (starting x-pos, starting y-pos, ending x-pos, ending y-pos):
  88. firstLineCoordinatePair = [330, 275, 350, 350]
  89.  
  90. # Surfboard's Second Line (starting x-pos, starting y-pos, ending x-pos, ending y-pos):
  91. secondLineCoordinatePair = [370, 275, 350, 350]
  92.  
  93. rectYChange = -5
  94. phase = 0
  95.  
  96. while not done:
  97.     for event in pygame.event.get():
  98.         if event.type == pygame.QUIT:
  99.             done = True
  100.  
  101.     # Game Logic:
  102.     phase += 1
  103.  
  104.     if phase > 180:
  105.         phase = phase * -1
  106.  
  107.     # Save exact position as a float to avoid floating point errors:
  108.     boardY = math.cos(math.radians(phase))*2
  109.  
  110.     surfboardRect.y = int(boardY)
  111.  
  112.     # Clear the screen:
  113.     screen.fill(RED)
  114.  
  115.     # Drawing Code:
  116.  
  117.     # Cool Expanding Rectangle Code (Commented Out Because I Want Falling Rectangles):
  118. ##    for i in range(len(rectList)):
  119. ##        pygame.draw.rect(screen, WHITE, [rectList[i][0],rectList[i][1], 10 + rectXChange, 10 + rectYChange], 2)
  120. ##        rectXChange += 1
  121. ##        rectYChange += 1
  122.  
  123.     # Falling Down circles:
  124.     for i in range(len(whiteCircleList)):
  125.         pygame.draw.circle(screen, WHITE, [whiteCircleList[i][0], whiteCircleList[i][1]], 3)
  126.         whiteCircleList[i][1] += 5
  127.         # If the rectangles have hit the bottom of the screen, make them appear 10 pixels above the top:
  128.         if whiteCircleList[i][1] > 450:
  129.             x = random.randrange(0, 700)
  130.             whiteCircleList[i][0] = x
  131.             y = random.randrange(-50, -10)
  132.             whiteCircleList[i][1] = y
  133.  
  134.     # Red Falling Up Circles:
  135.     for i in range(len(blueCircleList)):
  136.         pygame.draw.circle(screen, BLUE, [blueCircleList[i][0], blueCircleList[i][1]], 5, 5)
  137.         blueCircleList[i][1] -= 5
  138.         if blueCircleList[i][1] < 50:
  139.             circleX = random.randrange(0,700)
  140.             circleY = random.randrange(400, 500)
  141.             blueCircleList[i][0] = circleX
  142.             blueCircleList[i][1] = circleY
  143.  
  144.     # Light Blue Falling Up Circles:
  145.     for i in range(len(lightBlueCircleList)):
  146.         pygame.draw.circle(screen, LIGHTBLUE, [lightBlueCircleList[i][0], lightBlueCircleList[i][1]], 3, 3)
  147.         lightBlueCircleList[i][1] -= 5
  148.         if lightBlueCircleList[i][1] < 50:
  149.             circleX = random.randrange(0, 700)
  150.             circleY = random.randrange(400, 450)
  151.             lightBlueCircleList[i][0] = circleX
  152.             lightBlueCircleList[i][1] = circleY
  153.  
  154.     # Old Rectangle Code:
  155. ##    # Surfboard's Rectangle (x-pos, y-pos, x-length, y-length):
  156. ##    for i in range(len(surfboardRectangleList)):
  157. ##        pygame.draw.rect(screen, BLACK, [surfboardRectangleList[0], surfboardRectangleList[1] + rectYChange, surfboardRectangleList[2], surfboardRectangleList[3]], 2)
  158. ##        # Change the y-pos:
  159. ##        surfboardRectangleList[1] += 1
  160. ##        # If statement to control the y-position gets to (500 - 150 = 350)
  161. ##        if surfboardRectangleList[1] > 350:
  162. ##            # Reverse direction since you're at the bottom of the wave:
  163. ##            surfboardRectangleList[1] = -surfboardRectangleList[1]
  164.  
  165.     # Revised Rectangle Code:
  166.     pygame.draw.rect(screen, BLACK, surfboardRect, 0)
  167.  
  168.     # Surfboard's Line 1 (starting x-pos, starting y-pos, ending x-pos, ending y-pos):
  169.     for i in range(len(firstLineCoordinatePair)):
  170.         pygame.draw.line(screen, BLACK, [firstLineCoordinatePair[0], firstLineCoordinatePair[1]],[firstLineCoordinatePair[2], firstLineCoordinatePair[3]], 10)
  171.         # Change the starting y-pos:
  172.         firstLineCoordinatePair[1] += 1
  173.         # Change the ending y-pos:
  174.         firstLineCoordinatePair[3] += 1
  175.  
  176.     # Surfboard's Line 2 (starting x-pos, starting y-pos, ending x-pos, ending y-pos):
  177.     for i in range(len(secondLineCoordinatePair)):
  178.         pygame.draw.line(screen, BLACK, [secondLineCoordinatePair[0], secondLineCoordinatePair[1]],[secondLineCoordinatePair[2], secondLineCoordinatePair[3]], 10)
  179.         # Change the starting y-pos:
  180.         secondLineCoordinatePair[1] += 1
  181.         # Change the ending y-pos:
  182.         secondLineCoordinatePair[3] += 1
  183.  
  184.     # Update the screen:
  185.     pygame.display.flip()
  186.  
  187.     # Clock FPS:
  188.     clock.tick(FPS)
  189.  
  190. # Quit after main game loop ends:
  191. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement