Python從入門到實踐習題13 1 13 2

2021-08-19 13:47:01 字數 4583 閱讀 1660

星星:找一副星星影象,並在螢幕上顯示一系列排列整齊的星星。

又一次模仿書上的**,又出現了新問題,同樣粗心。希望明天會更好。

主程式:display

import pygame

import sys

from settings import settings

from pygame.sprite import group

import func as f

def display():

"""顯示"""

pygame.init()

settings = settings()

screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))

pygame.display.set_caption("display stars")

stars = group()

f.create_stars(settings, screen, stars)

while true:

f.check_keys()

f.update_screen(settings, screen, stars)

display()

設定類:settings

import pygame

class settings():

"""存放所有設定的類"""

def __init__(self):

self.screen_width = 1200

self.screen_height = 700

self.bg_color = (248, 2, 3)

星星類:star

import pygame

from pygame.sprite import sprite

class star(sprite):

"""星星的類"""

def __init__(self, screen):

super().__init__()

self.screen = screen

self.image = pygame.image.load('star.bmp')

self.rect = self.image.get_rect()

self.screen_rect = screen.get_rect()

self.rect.x = self.rect.width

self.rect.y = self.rect.height

def blitme(self):

"""繪製星星"""

self.screen.blit(self.image, self.rect)

函式func

import sys 

import pygame

from star import star

def check_keys():

for event in pygame.event.get():

if event.type == pygame.quit:

sys.exit()

def update_screen(settings, screen, stars):

"""重繪螢幕"""

screen.fill(settings.bg_color)

stars.draw(screen)

pygame.display.flip()

def get_number_stars_x(settings, star_width):

space_x = settings.screen_width - star_width

number_stars_x = int(space_x / (2 * star_width))

return number_stars_x

def get_number_stars_y(settings, star_height):

space_y = settings.screen_height - star_height

number_stars_y = int(space_y / (2 * star_height))

return number_stars_y

def create_star(screen, stars, star_number, row_number):

star = star(screen)

star_width = star.rect.width

star.x = star_width + 2 * star_width * star_number

star.rect.x = star.x

star.rect.y = star.rect.height + 2 * star.rect.height * row_number

stars.add(star)

def create_stars(settings, screen, stars):

star = star(screen)

number_stars_x = get_number_stars_x(settings, star.rect.width)

number_stars_y = get_number_stars_y(settings, star.rect.height)

for row_number in range(number_stars_y):

for star_number in range(number_stars_x):

create_star(screen, stars, star_number, row_number)

實際效果圖:

總結:1.

今天遇到了新問題:unboundlocalerror:local variable

在測試顯示乙個星星的過程中:

在例項化star = star(screen)時,把這句話寫在了screen = .......前面

2.值得注意的地方:

在計算行和列能放置星星的數量時,將number_stars_x = int(space_x / (2 * star_width))和number_stars_y = int(space_y / (2 * star_height)),寫成number_stars_x = int(space_x / 2 )和number_stars_y = int(space_y / 2 ),結果就是bug不好找,程式執行時慢的不行,顯示的效果還有些混亂,和預期相差較大,甚至會出現記憶體溢位錯誤。看看這兩種情況:

很恐怖!值得注意!

13-2 更逼真的星星:隨機放置星星

from random import randint

random_number = randint(-10, 10)

還是用上面的程式,需要更改的地方如下(只需更改func內的函式即可):

def create_star(screen, stars, star_number):

star = star(screen)

star_width = star.rect.width

star.x = randint(0, 1700) + star_width

star.rect.x = star.x

star.rect.y = randint(0, 700) + star.rect.height

stars.add(star)

def create_stars(settings, screen, stars):

star = star(screen)

number_stars_x = get_number_stars_x(settings, star.rect.width)

number_stars_y = get_number_stars_y(settings, star.rect.height)

#for row_number in range(number_stars_y):

for star_number in range(number_stars_x):

create_star(screen, stars, star_number)

效果圖(根據圖案的選擇,可能出現重疊的效果):

Python從入門到實踐 習題15 6

15 6 自動生成標籤 請修改die.py和dice visual.py,將用來設定hist.x labels 值的列表替換為乙個自動生成這種列表的迴圈。如果你熟悉列表解析,可嘗試將 die visual.py和dice visual.py中的其他for 迴圈也替換為列表解析。import pyga...

python程式設計從入門到實踐 習題11 3

11 3 雇員 編寫乙個名為employee 的類,其方法init 接受名 姓和年薪,並將它們都儲存在屬性中。編寫乙個名為give raise 的方法,它預設將 年薪增加5000美元,但也能夠接受其他的年薪增加量。為employee 編寫乙個測試用例,其中包含兩個測試方法 test give def...

Python程式設計從入門到實踐 習題3 4 3 7

3 4 嘉賓名單 如果你可以邀請任何人一起共進晚餐 無論是在世的還是故去的 你會邀請哪些人?請建立乙個列表,其中包含至少3個你想邀請的人 然後,使用這個列表列印訊息,邀請這些人來與你共進晚餐。person 白起 太乙 周瑜 for n in person print 尊敬的 n,誠摯邀請您於2017...