python 製作下雪的情景
阿新 • • 發佈:2018-12-11
# -*- coding: utf-8 -*- import pygame import random # 初始化pygame pygame.init() # 根據背景圖片的大小,設定螢幕長寬 SIZE = (1364, 569) screen = pygame.display.set_mode(SIZE) pygame.display.set_caption("Snow Animation") bg = pygame.image.load('snow.jpg') # 雪花列表 snow_list = [] # 初始化雪花:[x座標, y座標, x軸速度, y軸速度] for i in range(200): x = random.randrange(0, SIZE[0]) y = random.randrange(0, SIZE[1]) sx = random.randint(-1, 1) sy = random.randint(3, 6) snow_list.append([x, y, sx, sy]) clock = pygame.time.Clock() # 遊戲主迴圈 done = False while not done: # 訊息事件迴圈,判斷退出 for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # 黑背景/圖片背景 # screen.fill((0, 0, 0)) screen.blit(bg, (0, 0)) # 雪花列表迴圈 for i in range(len(snow_list)): # 繪製雪花,顏色、位置、大小 pygame.draw.circle(screen, (255, 255, 255), snow_list[i][:2], snow_list[i][3]-3) # 移動雪花位置(下一次迴圈起效) snow_list[i][0] += snow_list[i][2] snow_list[i][1] += snow_list[i][3] # 如果雪花落出螢幕,重設位置 if snow_list[i][1] > SIZE[1]: snow_list[i][1] = random.randrange(-50, -10) snow_list[i][0] = random.randrange(0, SIZE[0]) # 重新整理螢幕 pygame.display.flip() clock.tick(20) # 退出 pygame.quit()