第十三章
13-1 星星 : 找一幅星星图像, 并在屏幕上显示一系列整齐排列的星星。
13-2 更逼真的星星 : 为让星星的分布更逼真, 可随机地放置星星。 本书前面说过, 可像下面这样来生成随机数:
from random import randint
random_number = randint(-10,10)
13-1
start.py
importpygamefrom pygame.sprite importSpriteclassStart(Sprite):"""docstring for Start"""
def __init__(self, screen):
super(Start, self).__init__()
self.screen=screen
self.image= pygame.image.load('images/start.bmp')
self.rect=self.image.get_rect()#设置位置
self.rect.x =self.rect.width
self.rect.y=self.rect.height
self.x=float(self.rect.x)defblitme(self):
self.screen.blit(self.image,self.rect)
screen.py
importpygameimportsysfrom start importStartfrom pygame.sprite importGroupdefscreen():
pygame.init()
screen= pygame.display.set_mode((1200,800))
bg_color= (255,255,255)
pygame.display.set_caption("all start")
start=Group()whileTrue:for event inpygame.event.get():if event.type ==pygame.QUIT:
sys.exit()
create_start(start,screen)
screen.fill(bg_color)
start.draw(screen)
pygame.display.flip()defcreate_start(start,screen):
start1=Start(screen)
start_width=start1.rect.width
avaliable_x= 1200 - 2*start_width
number_x= int(avaliable_x / (2 *start_width))
start_height=start1.rect.height
avaliable_y= 800 - 2*start_height
number_y= int (avaliable_y / (2 *start_height))for n_y inrange(number_y):for n_x inrange(number_x):
st=Start(screen)
st.x= start_width + 2 * start_width *n_x
st.y= start_height + 2 * start_height *n_y
st.rect.x=st.x
st.rect.y=st.y
start.add(st)
screen()
输出:
13-2
start.py
同上
screen.py
importpygameimportsysfrom start importStartfrom pygame.sprite importGroupfrom random importrandintdefscreen():
pygame.init()
screen= pygame.display.set_mode((1200,800))
bg_color= (255,255,255)
pygame.display.set_caption("all start")
start=Group()whileTrue:for event inpygame.event.get():if event.type ==pygame.QUIT:
sys.exit()
create_start(start,screen)
screen.fill(bg_color)
start.draw(screen)
pygame.display.flip()defcreate_start(start,screen):
start1=Start(screen)
start_width=start1.rect.width
avaliable_x= 1200 - 2*start_width
number_x= int(avaliable_x / (2 *start_width))
start_height=start1.rect.height
avaliable_y= 800 - 2*start_height
number_y= int (avaliable_y / (2 *start_height))for n_y inrange(number_y):for n_x inrange(number_x):
st=Start(screen)
st.x= randint(-30,30) + 2 * start_width *n_x
st.y= randint(-30,30) + 2 * start_height *n_y
st.rect.x=st.x
st.rect.y=st.y
start.add(st)
screen()
输出:
13-3 雨滴 : 寻找一幅雨滴图像, 并创建一系列整齐排列的雨滴。 让这些雨滴往下落, 直到到达屏幕底端后消失。
13-4 连绵细雨 : 修改为完成练习13-3而编写的代码, 使得一行雨滴消失在屏幕底端后, 屏幕顶端又出现一行新雨滴, 并开始往下落。
13-3
rain.py
importpygamefrom pygame.sprite importSpritefrom random importrandintclassRain(Sprite):"""docstring for Start"""
def __init__(self, screen):
super(Rain, self).__init__()
self.screen=screen
self.image= pygame.image.load('images/rain.bmp')
self.rect=self.image.get_rect()#设置位置
self.rect.x =self.rect.width
self.rect.y=self.rect.height
self.x=float(self.rect.x)
self.y=float(self.rect.y)
self.speed= 1
defblitme(self):
self.screen.blit(self.image,self.rect)defupdate(self):
self.y+=self.speed
self.rect.y= self.y
screen.py
importpygameimportsysfrom rain importRainfrom pygame.sprite importGroupfrom random importrandintdefscreen():
pygame.init()
screen= pygame.display.set_mode((1200,600))
bg_color= (255,255,255)
pygame.display.set_caption("all Rain")
rains=Group()
create_rain(rains,screen)whileTrue:for event inpygame.event.get():if event.type ==pygame.QUIT:
sys.exit()
screen.fill(bg_color)for r inrains:
r.update()if r.rect.y > 1200:
rains.remove(r)
rains.draw(screen)
pygame.display.flip()defcreate_rain(rains,screen):
rain1=Rain(screen)
rain_width=rain1.rect.width
avaliable_x= 1200 - 2*rain_width
number_x= int(avaliable_x / (2 *rain_width))
rain_height=rain1.rect.height
avaliable_y= 800 - 2*rain_height
number_y= int (avaliable_y / (2 *rain_height))for n_x inrange(number_x):
r=Rain(screen)
r.x= rain_width + 2 * rain_width *n_x
r.rect.x=r.x
rains.add(r)
screen()
输出:
13-4
rain.py同上
screen.py
importpygameimportsysfrom rain importRainfrom pygame.sprite importGroupfrom random importrandintdefscreen():
pygame.init()
screen= pygame.display.set_mode((1200,600))
bg_color= (255,255,255)
pygame.display.set_caption("all Rain")
rains=Group()
create_rain(rains,screen)whileTrue:for event inpygame.event.get():if event.type ==pygame.QUIT:
sys.exit()
screen.fill(bg_color)
flag=Falsefor r inrains:
r.update()if r.rect.y > 1200:
rains.remove(r)
flag=Trueifflag:
create_rain(rains,screen)
flag=False
rains.draw(screen)
pygame.display.flip()defcreate_rain(rains,screen):
rain1=Rain(screen)
rain_width=rain1.rect.width
avaliable_x= 1200 - 2*rain_width
number_x= int(avaliable_x / (2 *rain_width))
rain_height=rain1.rect.height
avaliable_y= 800 - 2*rain_height
number_y= int (avaliable_y / (2 *rain_height))for n_x inrange(number_x):
r=Rain(screen)
r.x= rain_width + 2 * rain_width *n_x
r.rect.x=r.x
rains.add(r)
screen()
输出:
13-5 抓球 : 创建一个游戏, 在屏幕底端放置一个玩家可左右移动的角色。 让一个球出现在屏幕顶端, 且水平位置是随机的, 并让这个球以固定的速度往下落。 如果角
色与球发生碰撞(表示将球抓住了) , 就让球消失。 每当角色抓住球或球因抵达屏幕底端而消失后, 都创建一个新球。
ball.py
from pygame.sprite importSpritefrom random importrandintimportpygameclassBall(Sprite):"""docstring for Ball"""
def __init__(self, screen):
super(Ball, self).__init__()
self.screen=screen
self.screen_rect=self.screen.get_rect()
self.image= pygame.image.load('images/ball.bmp')
self.rect=self.image.get_rect()#设置位置
self.rect.x = randint(0,self.screen_rect.right-self.rect.width)
self.rect.y=0
self.x=float(self.rect.x)
self.y=float(self.rect.y)
self.speed= 1
defblitme(self):
self.screen.blit(self.image,self.rect)
human.py
importpygamefrom pygame.sprite importSpriteclassHuman(Sprite):"""docstring for Human"""
def __init__(self, screen):
super(Human, self).__init__()
self.screen=screen
self.image= pygame.image.load('images/human.bmp')
self.rect=self.image.get_rect()
self.screen_rect=screen.get_rect()
self.rect.centerx=self.screen_rect.centerx
self.rect.bottom=self.screen_rect.bottom
self.moving_left=False
self.moving_right=Falsedefupdate_human(self):if self.moving_left and self.rect.x >0:
self.rect.x-=1
ifself.moving_right :
self.rect.x+=1
defbliteme(self):
self.screen.blit(self.image,self.rect)
update_functions.py
importpygameimportsysfrom ball importBallfrom human importHumanclassU_Functions():"""docstring for U_Functions"""
def __init__(self):pass
defcheck_event(self,human):for event inpygame.event.get():if event.type ==pygame.QUIT:
sys.exit()elif event.type ==pygame.KEYDOWN:if event.key ==pygame.K_LEFT:
human.moving_left=Trueelif event.key ==pygame.K_RIGHT:
human.moving_right=Trueelif event.type ==pygame.KEYUP:if event.key ==pygame.K_LEFT:
human.moving_left=Falseelif event.key ==pygame.K_RIGHT:
human.moving_right=Falsedefcreate_ball(self,ball,screen):if len(ball) ==0:
b=Ball(screen)
ball.add(b)else:pass
defupdate_ball(self,ball,screen,human):for b inball:
b.rect.y+=b.speedif b.rect.y >b.screen_rect.bottom:
ball.remove(b)
collisions=pygame.sprite.groupcollide(ball,human,True,False)defupdate_screen(self,screen,human,bg_color,ball):
screen.fill(bg_color)if len(human) ==0:
human.add(Human(screen))for h inhuman:
self.check_event(h)
h.update_human()
human.draw(screen)
self.create_ball(ball,screen)
self.update_ball(ball,screen,human)
ball.draw(screen)
pygame.display.flip()
play.py
importpygameimportsysfrom human importHumanfrom update_fuction importU_Functionsfrom ball importBallfrom pygame.sprite importGroupdefrun():
pygame.init()
screen= pygame.display.set_mode((800,600))
pygame.display.set_caption("catch ball")
bg_color=(255,255,255)
human=Human(screen)
function=U_Functions()
b=Group()
human=Group()whileTrue:
function.update_screen(screen,human,bg_color,b)
run()
输出:
13-6 游戏结束 : 在为完成练习13-5而编写的代码中, 跟踪玩家有多少次未将球接着。 在未接着球的次数到达三次后, 结束游戏。
ball.py 和 human.py 同上
game_status.py
classGameStatus(object):"""docstring for GameStatus"""
def __init__(self):
self.game_active=True
self.total=0
self.catched=0
self.loss=0defcheck_active(self):if self.loss == 3:
self.game_active=False
update_function.py
importpygameimportsysfrom ball importBallfrom human importHumanfrom time importsleepclassU_Functions():"""docstring for U_Functions"""
def __init__(self):pass
defcheck_event(self,human):for event inpygame.event.get():if event.type ==pygame.QUIT:
sys.exit()elif event.type ==pygame.KEYDOWN:if event.key ==pygame.K_LEFT:
human.moving_left=Trueelif event.key ==pygame.K_RIGHT:
human.moving_right=Trueelif event.type ==pygame.KEYUP:if event.key ==pygame.K_LEFT:
human.moving_left=Falseelif event.key ==pygame.K_RIGHT:
human.moving_right=Falsedefcreate_ball(self,ball,screen):if len(ball) ==0:
b=Ball(screen)
ball.add(b)else:pass
defupdate_ball(self,ball,screen,human,game_status):for b inball:
b.rect.y+=b.speedif b.rect.y >b.screen_rect.bottom:
ball.remove(b)
game_status.loss+=1
ifpygame.sprite.groupcollide(ball,human,True,False):
sleep(0.5)defupdate_screen(self,screen,human,bg_color,ball,game_status):
screen.fill(bg_color)if len(human) ==0:
human.add(Human(screen))for h inhuman:
self.check_event(h)
h.update_human()
human.draw(screen)
self.create_ball(ball,screen)
self.update_ball(ball,screen,human,game_status)
ball.draw(screen)
pygame.display.flip()
play_game.py
importpygameimportsysfrom human importHumanfrom update_fuction importU_Functionsfrom ball importBallfrom pygame.sprite importGroupfrom game_status importGameStatusdefrun():
pygame.init()
screen= pygame.display.set_mode((800,600))
pygame.display.set_caption("catch ball")
bg_color=(255,255,255)
human=Human(screen)
function=U_Functions()
b=Group()
human=Group()
game_status=GameStatus()whileTrue:
game_status.check_active()ifgame_status.game_active:
function.update_screen(screen,human,bg_color,b,game_status)else:
sys.exit()
run()