【Python编程:从入门到实践】第十三章:外星人
来源:互联网 发布:南宁市扶贫数据大平台 编辑:程序博客网 时间:2024/05/23 01:59
13-1 星星 :找一幅星星图像,并在屏幕上显示一系列整齐排列的星星。
start.py
import pygamefrom pygame.sprite import Spriteclass Start(Sprite):"""docstring for Start"""def __init__(self, screen):super(Start, self).__init__()self.screen = screenself.image = pygame.image.load('images/start.bmp')self.rect = self.image.get_rect()#设置位置self.rect.x = self.rect.widthself.rect.y = self.rect.heightself.x = float(self.rect.x)def blitme(self):self.screen.blit(self.image,self.rect)screen.py
import pygameimport sysfrom start import Startfrom pygame.sprite import Groupdef 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.widthavaliable_x = 1200 - 2*start_widthnumber_x = int(avaliable_x / (2 * start_width))start_height = start1.rect.heightavaliable_y = 800 - 2* start_heightnumber_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_xst.y = start_height + 2 * start_height * n_yst.rect.x = st.xst.rect.y = st.ystart.add(st)screen()结果:
13-2 更逼真的星星 :为让星星的分布更逼真,可随机地放置星星。本书前面说过,可像下面这样来生成随机数:
from random import randint
random_number = randint(-10,10)
start.py
同上
screen.py
import pygameimport sysfrom start import Startfrom pygame.sprite import Groupfrom random import randintdef 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.widthavaliable_x = 1200 - 2*start_widthnumber_x = int(avaliable_x / (2 * start_width))start_height = start1.rect.heightavaliable_y = 800 - 2* start_heightnumber_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_xst.y = randint(-30,30) + 2 * start_height * n_yst.rect.x = st.xst.rect.y = st.ystart.add(st)screen()结果:
13-3 雨滴 :寻找一幅雨滴图像,并创建一系列整齐排列的雨滴。让这些雨滴往下落,直到到达屏幕底端后消失。
rain.py
import pygamefrom pygame.sprite import Spritefrom random import randintclass Rain(Sprite):"""docstring for Start"""def __init__(self, screen):super(Rain, self).__init__()self.screen = screenself.image = pygame.image.load('images/rain.bmp')self.rect = self.image.get_rect()#设置位置self.rect.x = self.rect.widthself.rect.y = self.rect.heightself.x = float(self.rect.x)self.y = float(self.rect.y)self.speed = 1def blitme(self):self.screen.blit(self.image,self.rect)def update(self):self.y +=self.speedself.rect.y = self.yscreen.py
import pygameimport sysfrom rain import Rainfrom pygame.sprite import Groupfrom random import randintdef 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.widthavaliable_x = 1200 - 2*rain_widthnumber_x = int(avaliable_x / (2 * rain_width))rain_height = rain1.rect.heightavaliable_y = 800 - 2* rain_heightnumber_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_xr.rect.x = r.xrains.add(r)screen()
结果:
13-4 连绵细雨 :修改为完成练习13-3而编写的代码,使得一行雨滴消失在屏幕底端后,屏幕顶端又出现一行新雨滴,并开始往下落。
rain.py
同上
screen.py
import pygameimport sysfrom rain import Rainfrom pygame.sprite import Groupfrom random import randintdef 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 = Falsefor r in rains:r.update()if r.rect.y > 1200:rains.remove(r)flag = Trueif flag:create_rain(rains,screen)flag = Falserains.draw(screen)pygame.display.flip()def create_rain(rains,screen):rain1 = Rain(screen)rain_width = rain1.rect.widthavaliable_x = 1200 - 2*rain_widthnumber_x = int(avaliable_x / (2 * rain_width))rain_height = rain1.rect.heightavaliable_y = 800 - 2* rain_heightnumber_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_xr.rect.x = r.xrains.add(r)screen()结果:
13-5 抓球:创建一个游戏,在屏幕地段放置一个玩家可左右移动的角色。让一个球出现在屏幕顶端,且水平位置是随机的,并让这个球以固定的速度往下落。如果角色与球发生碰撞(表示将球抓住了),就让球消失。每当角色抓住球或引球抵达屏幕低端而消失后,都创建一个新球。
ball.py
from pygame.sprite import Spritefrom random import randintimport pygameclass Ball(Sprite):"""docstring for Ball"""def __init__(self, screen):super(Ball, self).__init__()self.screen = screenself.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 = 0self.x = float(self.rect.x)self.y = float(self.rect.y)self.speed = 1def blitme(self):self.screen.blit(self.image,self.rect)human.py
import pygamefrom pygame.sprite import Spriteclass Human(Sprite):"""docstring for Human"""def __init__(self, screen):super(Human, self).__init__()self.screen = screenself.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.centerxself.rect.bottom = self.screen_rect.bottomself.moving_left = Falseself.moving_right =Falsedef update_human(self):if self.moving_left and self.rect.x > 0:self.rect.x -=1if self.moving_right :self.rect.x +=1def bliteme(self):self.screen.blit(self.image,self.rect)update_functions.py
import pygameimport sysfrom ball import Ballfrom human import Humanclass U_Functions():"""docstring for U_Functions"""def __init__(self):passdef 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 = 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 = Falsedef create_ball(self,ball,screen):if len(ball) ==0:b = Ball(screen)ball.add(b)else:passdef update_ball(self,ball,screen,human):for b in ball:b.rect.y +=b.speedif 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 pygameimport sysfrom human import Humanfrom update_fuction import U_Functionsfrom ball import Ballfrom pygame.sprite import Groupdef 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 = Trueself.total = 0self.catched = 0self.loss = 0def check_active(self):if self.loss == 3:self.game_active = Falseupdate_function.py
import pygameimport sysfrom ball import Ballfrom human import Humanfrom time import sleepclass U_Functions():"""docstring for U_Functions"""def __init__(self):passdef 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 = 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 = Falsedef create_ball(self,ball,screen):if len(ball) ==0:b = Ball(screen)ball.add(b)else:passdef update_ball(self,ball,screen,human,game_status):for b in ball:b.rect.y +=b.speedif b.rect.y > b.screen_rect.bottom:ball.remove(b)game_status.loss +=1if 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 pygameimport sysfrom human import Humanfrom update_fuction import U_Functionsfrom ball import Ballfrom pygame.sprite import Groupfrom game_status import GameStatusdef 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()
阅读全文
0 0
- 【Python编程:从入门到实践】第十三章:外星人
- [Python]《Python编程:从入门到实践》外星人入侵(一)
- 《Python编程:从入门到实践》中外星人项目pygame安装(Ubuntu)
- 20170118Python编程:从入门到实践
- Python编程:从入门到实践1
- Python编程:从入门到实践2
- Python编程:从入门到实践
- 《Python编程:从入门到实践》2-9章 笔记
- 《Python编程:从入门到实践》第10章 笔记
- 《Python编程:从入门到实践》第11章 笔记
- Python编程 从入门到实践 第六章 字典
- python编程:从入门到实践 第2章
- python编程:从入门到实践第3章
- python编程:从入门到实践 第4章
- python编程:从入门到实践 第5章
- 【Python编程:从入门到实践】第三章:列表简介
- 【Python编程:从入门到实践】第四章:操作列表
- 【Python编程:从入门到实践】第五章:if语句
- 老夫创造
- 百度图片爬虫
- 4.7(2)——输出等腰三角形
- tomcat与请求
- Android studio里Cannot resolve symbol 'R'
- 【Python编程:从入门到实践】第十三章:外星人
- oracle一个创建用户、创建表空间、授权、建表的完整过程
- 基于内容的推荐算法
- 9.输入DStream之Kafka数据源实战(基于Direct的方式)
- Java/11.12日/笔记
- LeetCode解题-#14-Longest Common Prefix
- Tomcat热部署
- Apache commons-pool2-2.4.2源码学习笔记
- tensorflow安装