第十三章
13-1 星星 : 找一幅星星图像, 并在屏幕上显示一系列整齐排列的星星。
13-2 更逼真的星星 : 为让星星的分布更逼真, 可随机地放置星星。 本书前面说过, 可像下面这样来生成随机数:
from random import randint random_number = randint(-10,10)
13-1
start.py
import pygame from pygame.sprite import Sprite class Start(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) def blitme(self): self.screen.blit(self.image,self.rect)
screen.py
import pygame import sys from start import Start from pygame.sprite import Group def screen(): pygame.init() screen = pygame.display.set_mode((1200,800)) bg_color = (255,255,255) pygame.display.set_caption("all start") start = Group() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() create_start(start,screen) screen.fill(bg_color) start.draw(screen) pygame.display.flip() def create_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 in range(number_y): for n_x in range(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
import pygame import sys from start import Start from pygame.sprite import Group from random import randint def screen(): pygame.init() screen = pygame.display.set_mode((1200,800)) bg_color = (255,255,255) pygame.display.set_caption("all start") start = Group() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() create_start(start,screen) screen.fill(bg_color) start.draw(screen) pygame.display.flip() def create_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 in range(number_y): for n_x in range(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
import pygame from pygame.sprite import Sprite from random import randint class Rain(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 def blitme(self): self.screen.blit(self.image,self.rect) def update(self): self.y +=self.speed self.rect.y = self.y
screen.py
import pygame import sys from rain import Rain from pygame.sprite import Group from random import randint def screen(): 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) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(bg_color) for r in rains: r.update() if r.rect.y > 1200: rains.remove(r) rains.draw(screen) pygame.display.flip() def create_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 in range(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
import pygame import sys from rain import Rain from pygame.sprite import Group from random import randint def screen(): 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) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(bg_color) flag = False for r in rains: r.update() if r.rect.y > 1200: rains.remove(r) flag = True if flag: create_rain(rains,screen) flag = False rains.draw(screen) pygame.display.flip() def create_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 in range(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 import Sprite from random import randint import pygame class Ball(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 def blitme(self): self.screen.blit(self.image,self.rect)
human.py
import pygame from pygame.sprite import Sprite class Human(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 =False def update_human(self): if self.moving_left and self.rect.x > 0: self.rect.x -=1 if self.moving_right : self.rect.x +=1 def bliteme(self): self.screen.blit(self.image,self.rect)
update_functions.py
import pygame import sys from ball import Ball from human import Human class U_Functions(): """docstring for U_Functions""" def __init__(self): pass def check_event(self,human): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: human.moving_left = True elif event.key == pygame.K_RIGHT: human.moving_right = True elif event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: human.moving_left = False elif event.key == pygame.K_RIGHT: human.moving_right = False def create_ball(self,ball,screen): if len(ball) ==0: b = Ball(screen) ball.add(b) else: pass def update_ball(self,ball,screen,human): for b in ball: b.rect.y +=b.speed if b.rect.y > b.screen_rect.bottom: ball.remove(b) collisions = pygame.sprite.groupcollide(ball,human,True,False) def update_screen(self,screen,human,bg_color,ball): screen.fill(bg_color) if len(human) == 0: human.add(Human(screen)) for h in human: 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
import pygame import sys from human import Human from update_fuction import U_Functions from ball import Ball from pygame.sprite import Group def run(): 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() while True: function.update_screen(screen,human,bg_color,b) run()
输出:
13-6 游戏结束 : 在为完成练习13-5而编写的代码中, 跟踪玩家有多少次未将球接着。 在未接着球的次数到达三次后, 结束游戏。
ball.py 和 human.py 同上
game_status.py
class GameStatus(object): """docstring for GameStatus""" def __init__(self): self.game_active = True self.total = 0 self.catched = 0 self.loss = 0 def check_active(self): if self.loss == 3: self.game_active = False
update_function.py
import pygame import sys from ball import Ball from human import Human from time import sleep class U_Functions(): """docstring for U_Functions""" def __init__(self): pass def check_event(self,human): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: human.moving_left = True elif event.key == pygame.K_RIGHT: human.moving_right = True elif event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: human.moving_left = False elif event.key == pygame.K_RIGHT: human.moving_right = False def create_ball(self,ball,screen): if len(ball) ==0: b = Ball(screen) ball.add(b) else: pass def update_ball(self,ball,screen,human,game_status): for b in ball: b.rect.y +=b.speed if b.rect.y > b.screen_rect.bottom: ball.remove(b) game_status.loss +=1 if pygame.sprite.groupcollide(ball,human,True,False): sleep(0.5) def update_screen(self,screen,human,bg_color,ball,game_status): screen.fill(bg_color) if len(human) == 0: human.add(Human(screen)) for h in human: 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
import pygame import sys from human import Human from update_fuction import U_Functions from ball import Ball from pygame.sprite import Group from game_status import GameStatus def run(): 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() while True: game_status.check_active() if game_status.game_active: function.update_screen(screen,human,bg_color,b,game_status) else: sys.exit() run()