阅读使用函数绘制对象的文本,修改以前实验中的代码,以便定义一个函数来绘制对象。该函数应该采用一对x,y参数作为对象位置的基础。
调整主代码中的动画循环以使用函数来绘制对象的多个实例。 我遇到的问题是尝试使用函数重新编码,我不知道如何做到这一点。 这是我的代码:
#-------------------------------------------------------------------------------
# Name: Animations.py
# Purpose: Draws a picture of a farm and shooting star that moves and makes it rain while the song Raining Men
#
# Author:
#
# Created: 12/10/2013
#-------------------------------------------------------------------------------
import os
os.environ['SDL_VIDEODRIVER']= 'windib'
#Imports OS
import random
import pygame
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load("The_Weather_Girls_It_39_s_Raining_Men.wav")
pygame.mixer.music.play()
#Initiates music mixer and plays music
black = ( 0, 0, 0)
white = ( 255, 255, 255)
dsblue = ( 0, 191, 255)
red = ( 255, 0, 0)
green = ( 0, 255, 0)
white = ( 255, 255, 255)
sbrown = ( 139, 69, 19)
fbrick = ( 178, 34, 34)
yellow = ( 255, 255, 0)
hgrey = ( 30, 30, 30)
hyellow = ( 255, 255, 102)
#Defines colours
done = False
shooting_star_x = 0
shooting_star_y = 50
#Starting position of shooting star
shooting_star_change_x = 10
shooting_star_change_y = 10
#Speed and direction of shooting star
size = [700,500]
screen = pygame.display.set_mode(size)
#Sets the screen
pygame.display.set_caption("The Big Red Farm!")
#Sets header for pygame
rain_list = []
for i in range(100):
x = random.randrange(0,700)
y = random.randrange(0,700)
rain_list.append([x,y])
clock = pygame.time.Clock()
#Sets clock to manage how fast screen updates
#Program Loop ----------------------------------------------------------
while done == False:
for event in pygame.event.get():
if event.type == pygame.QUIT: #If clicked close
print ("User asked to quit.")
done = True
#Terminates loop
screen.fill(hgrey)
#Sets background colour to a dark grey
pygame.draw.circle(screen,hyellow, (130, 80),55)
#Draws the moon
pygame.draw.ellipse(screen,white,[shooting_star_x,shooting_star_y,150,5])
print shooting_star_y
shooting_star_x += shooting_star_change_x
if shooting_star_x <= 699:
shooting_star_x += shooting_star_change_x
elif shooting_star_x >= 700:
shooting_star_x = 25
shooting_star_x += shooting_star_change_x
elif shooting_star_x >= 300:
screen.fill(red)
#Draws Shooting star
pygame.draw.rect (screen,red,[300,200,400,300])
#Draws farm
pygame.draw.ellipse(screen,red,[300,100,400,200])
#Draws roof
pygame.draw.rect (screen, sbrown, [475,350,75,150])
#Draws Door
pygame.draw.rect (screen, yellow, [350,200,20,20])
pygame.draw.rect (screen, yellow, [375,200,20,20])
pygame.draw.rect (screen, yellow, [350,225,20,20])
pygame.draw.rect (screen, yellow, [375,225,20,20])
#Draws the window on the top left side of the big red farm
pygame.draw.rect (screen, yellow, [610,200,20,20])
pygame.draw.rect (screen, yellow, [635,200,20,20])
pygame.draw.rect (screen, yellow, [610,225,20,20])
pygame.draw.rect (screen, yellow, [635,225,20,20])
#Draws the window on the top right side of the big red farm
pygame.draw.rect (screen, yellow, [350,350,60,100])
#Draws the window on the left bottom part of the big red farm
pygame.draw.rect (screen, yellow, [600,350,60,100])
#Draws the window on the right bottom part of the big red farm
pygame.draw.rect (screen,red, [0,295,300,500])
#Draws the garage that is attached to the big red barn
pygame.draw.rect (screen, sbrown, [65,400,190,400])
#Draws the garage door
for i in range(len(rain_list)):
pygame.draw.circle(screen,dsblue, rain_list[i],2)
rain_list[i][1] += 3
if rain_list[i][1] > 700:
y = random.randrange(-50,-10)
rain_list[i][1] = y
x = random.randrange(0,700)
rain_list[1][0] = x
pygame.display.flip()
clock.tick(165)
pygame.quit()
#Terminates program