【Python编程从⼊门到实践】第⼗三章课后习题13-1 星星:⼀幅星星图像,并在屏幕上显⽰⼀系列整齐排列的星星。
import pygame
import sys
from pygame.sprite import Group
from pygame.sprite import Sprite
class Settings():
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (0, 0, 51)
class Star(Sprite):
def __init__(self, settings, screen):
super().__init__()
self.settings = settings
self.screen = screen
self.image = pygame.image.load('images/star.png')
< = _rect()
< = width
< = height
def blitme(self):
self.screen.blit(self.image, )
def get_number_star_x(settings, star_width):
available_space_x = settings.screen_width - 2 * star_width
number_stars_x = int(available_space_x / (star_width * 2))
return number_stars_x
def get_number_rows(settings, star_height):
"""计算屏幕可容纳多少⾏外星⼈"""
available_space_y = (settings.screen_height - 2 * star_height)
oracle11g怎么修改表的密码number_rows = int(available_space_y / (star_height * 2))
return number_rows
def create_star(settings, screen, stars, star_number, row_number):
star = Star(settings, screen)
< = width + 2 * width * star_number
< = height + 2 * height * row_number
stars.add(star)
def create_star_group(settgings, screen, stars):
star = Star(settgings, screen)
number_stars_x = get_number_star_x(settgings, width)
number_rows = get_number_rows(settgings, height)
for row_number in range(number_rows):
for star_number in range(number_stars_x):
create_star(settgings, screen, stars, star_number, row_number)
def run_star():
pygame.init()
settings = Settings()
settings = Settings()
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height)) pygame.display.set_caption('Stars')
stars = Group()
create_star_group(settings, screen, stars)
while True:
for event in ():
pe == pygame.QUIT:
viewfresh车厘子screen.fill(settings.bg_color)
stars.draw(screen)
pygame.display.flip()
run_star()
运⾏结果:
13-2 更逼真的星星:为让星星的分布更逼真,可随机地放置星星。
import pygame
import sys
from pygame.sprite import Group
from pygame.sprite import Sprite
from random import randint
class Settings():
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.screen_height = 800
self.bg_color = (0, 0, 51)
class Star(Sprite):
def __init__(self, settings, screen):
super().__init__()
self.settings = settings
self.screen = screen
self.random_number = randint(-10, 10)
self.image = pygame.image.load('images/star.png')
< = _rect()
< = width + self.random_number
< = height + self.random_number
def blitme(self):
self.screen.blit(self.image, )
def get_number_star_x(settings, star_width):
available_space_x = settings.screen_width - 2 * star_width
number_stars_x = int(available_space_x / (star_width * 2))
return number_stars_x学python编程入门
def get_number_rows(settings, star_height):
"""计算屏幕可容纳多少⾏外星⼈"""
available_space_y = (settings.screen_height - 2 * star_height)
number_rows = int(available_space_y / (star_height * 2))
return number_rows
def create_star(settings, screen, stars, star_number, row_number):
star = Star(settings, screen)
< = + 2 * * star_number
十进制 78的原码反码补码< = + 2 * * row_number
stars.add(star)
def create_star_group(settgings, screen, stars):
star = Star(settgings, screen)
number_stars_x = get_number_star_x(settgings, width)
number_rows = get_number_rows(settgings, height)
for row_number in range(number_rows):
for star_number in range(number_stars_x):
create_star(settgings, screen, stars, star_number, row_number)
def run_star():
pygame.init()
settings = Settings()
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height)) pygame.display.set_caption('Stars')
java代码加密混淆工具stars = Group()
create_star_group(settings, screen, stars)
while True:
for event in ():
pe == pygame.QUIT:
screen.fill(settings.bg_color)
stars.draw(screen)
pygame.display.flip()
run_star()
运⾏结果:
13-3 ⾬滴:寻⼀幅⾬滴图像,并创建⼀系列整齐排列的⾬滴。让这些⾬滴往下落,直到到达屏幕底端后消失。
import sys
import pygame
from pygame.sprite import Group
from pygame.sprite import Sprite
class Settings():
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (255, 255, 255)
class Rain(Sprite):
def __init__(self, settings, screen):
super().__init__()
self.settings = settings
self.screen = screen
self.image = pygame.image.load('images/rain.png')
< = _rect()
self.drop_speed = 1
def update(self):
< += self.drop_speed
< += self.drop_speed
def blitme(self):
self.screen.blit(self.image, )
def get_number_rains_x(settings, rain_width):
available_space_x = settings.screen_width - rain_width * 2
number_rains_x = int(available_space_x / (rain_width * 2))
return number_rains_x
def create_rain(settings, screen, rains, number_rain):
rain = Rain(settings, screen)
< = width + 2 * width * number_rain
rains.add(rain)
def create_rain_group(settings, screen, rains):
rain = Rain(settings, screen)
number_rains_x = get_number_rains_x(settings, width)
for rain_number in range(number_rains_x):
create_rain(settings, screen, rains, rain_number)
def update_rains(settings, rains):
"""更新⾬滴的位置,并删除已经消失的⾬滴"""
# 更新⾬滴的位置
rains.update()
# 删除已消失的⾬滴
for rain py():
bottom >= settings.screen_height:
def run_rain():
pygame.init()
settings = Settings()
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height)) pygame.display.set_caption('Raining')
rains = Group()
create_rain_group(settings, screen, rains)
while True:
for event in ():
pe == pygame.QUIT:
screen.fill(settings.bg_color)
rains.draw(screen)
update_rains(settings, rains)
pygame.display.flip()
run_rain()
运⾏结果:
软件架构师是用什么软件
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论