第十三章
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()