运行图片
代码
import pygame
import random
pygame. init( )
width, height = 800 , 600
screen = pygame. display. set_mode( ( width, height) )
pygame. display. set_caption( 'Christmas Tree' )
GREEN = ( 34 , 139 , 34 )
RED = ( 255 , 0 , 0 )
WHITE = ( 255 , 255 , 255 )
def draw_christmas_tree ( ) : triangle_height = 300 triangle_width = 200 pygame. draw. rect( screen, pygame. Color( 'brown' ) , ( width // 2 - 25 , height - 200 , 50 , 200 ) ) for i in range ( triangle_height) : pygame. draw. polygon( screen, GREEN, [ ( width // 2 - triangle_width // 2 - i, height - 50 - i) , ( width // 2 + triangle_width // 2 + i, height - 50 - i) , ( width // 2 , height - 50 - i - triangle_height) ] )
def generate_snowflakes ( ) : snowflakes = [ ] for _ in range ( 100 ) : x = random. randint( 0 , width) y = random. randint( 0 , height) size = random. randint( 1 , 5 ) snowflakes. append( ( x, y, size) ) return snowflakes
def draw_snowflakes ( snowflakes) : for flake in snowflakes: pygame. draw. circle( screen, WHITE, ( flake[ 0 ] , flake[ 1 ] ) , flake[ 2 ] )
def draw_santa ( ) : santa_image = pygame. Surface( ( 120 , 120 ) , pygame. SRCALPHA) pygame. draw. circle( santa_image, RED, ( 60 , 60 ) , 50 ) pygame. draw. polygon( santa_image, RED, [ ( 0 , 60 ) , ( 120 , 60 ) , ( 60 , 120 ) ] ) pygame. draw. circle( santa_image, WHITE, ( 45 , 45 ) , 10 ) pygame. draw. circle( santa_image, WHITE, ( 75 , 45 ) , 10 ) pygame. draw. arc( santa_image, WHITE, ( 30 , 45 , 60 , 60 ) , 3.14 / 2 , 3 * 3.14 / 2 , 3 ) pygame. draw. polygon( santa_image, pygame. Color( 'orange' ) , [ ( 60 , 50 ) , ( 70 , 70 ) , ( 50 , 70 ) ] ) pygame. draw. line( santa_image, WHITE, ( 55 , 85 ) , ( 20 , 90 ) , 2 ) pygame. draw. line( santa_image, WHITE, ( 65 , 85 ) , ( 100 , 90 ) , 2 ) pygame. draw. polygon( santa_image, RED, [ ( 30 , 0 ) , ( 90 , 0 ) , ( 60 , 60 ) ] ) screen. blit( santa_image, ( width // 2 - 60 , height - 200 - 120 ) )
running = True
clock = pygame. time. Clock( )
snowflakes = generate_snowflakes( ) while running: for event in pygame. event. get( ) : if event. type == pygame. QUIT: running = False screen. fill( ( 0 , 0 , 0 ) ) draw_christmas_tree( ) draw_santa( ) draw_snowflakes( snowflakes) for i in range ( len ( snowflakes) ) : x, y, size = snowflakes[ i] snowflakes[ i] = ( x, y + size, size) if y + size > height: snowflakes[ i] = ( random. randint( 0 , width) , 0 , size) pygame. display. flip( ) clock. tick( 30 )
pygame. quit( )