1. 程式人生 > >Python程式設計從入門到實踐練習13-3、13-4

Python程式設計從入門到實踐練習13-3、13-4

13-3 雨滴:尋找一幅雨滴影象,並建立一系列整齊排列的雨滴。讓這些雨滴往下落,直到到達屏幕後消失。

先說今天的問題:

1.在更改函式形參時,呼叫的時候也要修改,否則會出現AttributeError

    值得注意的是:每當新增進新的程式碼,要檢查函式傳遞的引數是否對應一致,函式本身與呼叫它的時候都要檢查

2.愚蠢的錯誤:把建立整齊雨滴的函式寫到迴圈體中去了,結果一直出現重疊,達不到預期效果,搞了半天眼睛快瞎了才發現,    唉!


主程式 display

import pygame


from settings import Settings

from rain import Rain

import func as f

from pygame.sprite import Group#####

def display():
	"""主函式----顯示雨滴效果"""
	pygame.init()
	settings = Settings()
	screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
	pygame.display.set_caption('Raining')
	rains = Group()####
	f.create_rains(settings, screen, rains)#####
	while True:
		f.check_events()
		screen.fill(settings.bg_color)
		f.change_direction(settings, rains)
		rains.draw(screen)
		pygame.display.flip()

display()

函式模組func

import pygame

import sys

from rain import Rain


def check_events():
	"""按鍵檢查"""
	for event in pygame.event.get():
			if event.type == pygame.QUIT:
				sys.exit()

def get_col_numbers(settings, rain):#對於每一行來說,變化的是x座標,列在變化
	"""一行可以存放的雨滴數"""
	col_numbers = int(settings.screen_width / (2 * rain.rect.width))
	return col_numbers

def get_row_numbers(settings, rain):
	"""可存放雨滴的行數"""
	row_numbers = int(settings.screen_height / (1 * rain.rect.height))
	return row_numbers

def create_rain(settings, screen, rains, col_number, row_number):
	rain = Rain(settings, screen)
	rain_width = rain.rect.width
	rain.x = rain_width + 2 * rain_width * col_number
	rain.rect.x = rain.x#rect存放整數
	rain.rect.y = rain.rect.y + 2 * rain.rect.height * row_number
	rains.add(rain)

def create_rains(settings, screen, rains):
	rain = Rain(settings, screen)#這句是要寫的,不寫的話get_col_numbers()中的rain將出現NameError
	col_numbers = get_col_numbers(settings, rain)
	row_numbers = get_row_numbers(settings, rain)
	for row_number in range(row_numbers):
		for col_number in range(col_numbers):
			create_rain(settings, screen, rains, col_number, row_number)

def change_direction(settings, rains):
	for rain in rains.sprites():
		rain.rect.y += settings.drop_speed

		

		

rain類:

import pygame

from pygame.sprite import Sprite

class Rain(Sprite):
	"""雨滴"""
	def __init__(self, settings, screen):
		super().__init__()
		self.screen = screen
		self.settings = settings
		self.image = pygame.image.load('rain.bmp')
		self.rect = self.image.get_rect()
		
		self.y = float(self.rect.y)
		
	def blitme(self):
		self.screen.blit(self.image, self.rect)
	

settings類:

class Settings():
	"""設定所有設定的類"""
	def __init__(self):
		self.screen_width = 1200
		self.screen_height = 600
		self.bg_color = (255, 255, 255)
		self.drop_speed = 1

效果圖:

初始:


下落:




13-4 連綿細雨:使得一行雨滴消失在螢幕低端後,螢幕頂端又出現一行新的雨滴,並開始往下落

新增函式:

def update_rains(settings, screen, rains):
	for rain in rains.sprites():
		if rain.check_edges():
			new_rain = Rain(settings, screen)
			new_rain.rect.x = rain.rect.x
			rains.remove(rain)
			rains.add(new_rain)
		print(len(rains))	

主程式改動:

while True:
		f.check_events()
		screen.fill(settings.bg_color)
		f.change_direction(settings, rains)
		f.update_rains(settings, screen, rains)
		rains.draw(screen)
		pygame.display.flip()

效果:

初始:


執行中:


rains編組的長度(始終不變):



未解決的疑惑:

1.程式剛開始執行時較慢,下落速度較慢,幾秒後變正常

2.rains編組中數值為什麼是35?螢幕大小存放的三行rain,應該是21,執行時為什麼變成35?