Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- # Requirements:
- # Modify the prior Create-a-Picture lab, or start a new one.
- # Animate the image. Try one or more of the following:
- # Move an item across the screen.
- # Move an item back and forth.
- # Move up/down/diagonally.
- # Move in circles.
- # Have a person wave his/her arms.
- # Create a stoplight that changes colors.
- # import statement
- import pygame
- # Define colors:
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- LIGHTBLUE = (103, 255, 246)
- # initialize pygame:
- pygame.init()
- # screen:
- size = (700, 500)
- screen = pygame.display.set_mode(size)
- # Set the caption:
- pygame.display.set_caption("Chapter 8 Lab")
- # Clock:
- clock = pygame.time.Clock()
- # FPS:
- FPS = 60
- # boolean variable for game:
- done = False
- # Expanding Rectangle List:
- rectList = []
- for i in range(5):
- rectX = random.randrange(0, 20)
- rectY = random.randrange(0, 20)
- rectList.append([rectX, rectY])
- rectXChange = 10
- rectYChange = 10
- # For loop for white circles:
- whiteCircleList = []
- for i in range(25):
- x = random.randrange(0, 700)
- y = random.randrange(0, 50)
- whiteCircleList.append([x,y])
- # For Loop for Blue Circles:
- blueCircleList = []
- for i in range(100):
- circleX = random.randrange(0, 500)
- circleY = random.randrange(0, 700)
- blueCircleList.append([circleX, circleY])
- # Light Blue Circle For Loop:
- lightBlueCircleList = []
- for i in range(100):
- circleX = random.randrange(0, 500)
- circleY = random.randrange(0, 700)
- lightBlueCircleList.append([circleX, circleY])
- # Surfboard's Rectangle (x-pos, y-pos, x-length, y-length):
- surfboardRect = pygame.Rect(325, 125, 50, 150)
- boardY = 255.0
- # Surfboard's First Line (starting x-pos, starting y-pos, ending x-pos, ending y-pos):
- firstLineCoordinatePair = [330, 275, 350, 350]
- # Surfboard's Second Line (starting x-pos, starting y-pos, ending x-pos, ending y-pos):
- secondLineCoordinatePair = [370, 275, 350, 350]
- rectYChange = -5
- phase = 0
- while not done:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- # Game Logic:
- phase += 1
- if phase > 180:
- phase = phase * -1
- # Save exact position as a float to avoid floating point errors:
- boardY = math.cos(math.radians(phase))*2
- surfboardRect.y = int(boardY)
- # Clear the screen:
- screen.fill(RED)
- # Drawing Code:
- # Cool Expanding Rectangle Code (Commented Out Because I Want Falling Rectangles):
- ## for i in range(len(rectList)):
- ## pygame.draw.rect(screen, WHITE, [rectList[i][0],rectList[i][1], 10 + rectXChange, 10 + rectYChange], 2)
- ## rectXChange += 1
- ## rectYChange += 1
- # Falling Down circles:
- for i in range(len(whiteCircleList)):
- pygame.draw.circle(screen, WHITE, [whiteCircleList[i][0], whiteCircleList[i][1]], 3)
- whiteCircleList[i][1] += 5
- # If the rectangles have hit the bottom of the screen, make them appear 10 pixels above the top:
- if whiteCircleList[i][1] > 450:
- x = random.randrange(0, 700)
- whiteCircleList[i][0] = x
- y = random.randrange(-50, -10)
- whiteCircleList[i][1] = y
- # Red Falling Up Circles:
- for i in range(len(blueCircleList)):
- pygame.draw.circle(screen, BLUE, [blueCircleList[i][0], blueCircleList[i][1]], 5, 5)
- blueCircleList[i][1] -= 5
- if blueCircleList[i][1] < 50:
- circleX = random.randrange(0,700)
- circleY = random.randrange(400, 500)
- blueCircleList[i][0] = circleX
- blueCircleList[i][1] = circleY
- # Light Blue Falling Up Circles:
- for i in range(len(lightBlueCircleList)):
- pygame.draw.circle(screen, LIGHTBLUE, [lightBlueCircleList[i][0], lightBlueCircleList[i][1]], 3, 3)
- lightBlueCircleList[i][1] -= 5
- if lightBlueCircleList[i][1] < 50:
- circleX = random.randrange(0, 700)
- circleY = random.randrange(400, 450)
- lightBlueCircleList[i][0] = circleX
- lightBlueCircleList[i][1] = circleY
- # Old Rectangle Code:
- ## # Surfboard's Rectangle (x-pos, y-pos, x-length, y-length):
- ## for i in range(len(surfboardRectangleList)):
- ## pygame.draw.rect(screen, BLACK, [surfboardRectangleList[0], surfboardRectangleList[1] + rectYChange, surfboardRectangleList[2], surfboardRectangleList[3]], 2)
- ## # Change the y-pos:
- ## surfboardRectangleList[1] += 1
- ## # If statement to control the y-position gets to (500 - 150 = 350)
- ## if surfboardRectangleList[1] > 350:
- ## # Reverse direction since you're at the bottom of the wave:
- ## surfboardRectangleList[1] = -surfboardRectangleList[1]
- # Revised Rectangle Code:
- pygame.draw.rect(screen, BLACK, surfboardRect, 0)
- # Surfboard's Line 1 (starting x-pos, starting y-pos, ending x-pos, ending y-pos):
- for i in range(len(firstLineCoordinatePair)):
- pygame.draw.line(screen, BLACK, [firstLineCoordinatePair[0], firstLineCoordinatePair[1]],[firstLineCoordinatePair[2], firstLineCoordinatePair[3]], 10)
- # Change the starting y-pos:
- firstLineCoordinatePair[1] += 1
- # Change the ending y-pos:
- firstLineCoordinatePair[3] += 1
- # Surfboard's Line 2 (starting x-pos, starting y-pos, ending x-pos, ending y-pos):
- for i in range(len(secondLineCoordinatePair)):
- pygame.draw.line(screen, BLACK, [secondLineCoordinatePair[0], secondLineCoordinatePair[1]],[secondLineCoordinatePair[2], secondLineCoordinatePair[3]], 10)
- # Change the starting y-pos:
- secondLineCoordinatePair[1] += 1
- # Change the ending y-pos:
- secondLineCoordinatePair[3] += 1
- # Update the screen:
- pygame.display.flip()
- # Clock FPS:
- clock.tick(FPS)
- # Quit after main game loop ends:
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement