标签分类
技术文章
当前位置:首页 > ▲▲▲【编程教程】▲▲▲ > python > Python编程:从入门到实践

《Python编程:从入门到实践》第十三章:外星人

  • 发布时间:
  • 作者:码农之家原创
  • 点击:429

Python编程:从入门到实践

这篇文章主要知识点是关于Python编程,从入门到实践,外星人,Python编程:从入门到实践,《Python编程:从入门到实践》DEMO实例代码 Python编程实现输入某年某月某日计算出这一天是该年第几天的方法 《Python编程:从入门到实践》第六章:字典 python编程学习np.float 被删除的问题解析 python编程的习惯整理 的内容,如果大家想对相关知识点有系统深入的学习,可以参阅以下电子书

Python编程入门(第3版)
  • 类型:Python大小:3285 MB MB格式:PDF出版:人民邮电出版社作者:TobyDonaldson,袁国忠
立即下载
Python编程:从入门到实践
  • 类型:Python入门大小:9.85M格式:PDF出版:中国工信出版集团作者:埃里克·马瑟斯
立即下载
Python高性能编程
Python高性能编程中文完整版
  • 类型:Python编程大小:16.9 MB格式:PDF出版:人民邮电出版社作者:戈雷利克
立即下载
Python股票量化交易从入门到实践
  • 类型:Python大小:96 MB格式:PDF出版:人民邮电出版社作者:袁霄
立即下载
Python编程轻松进阶
  • 类型:Python编程大小:2.1 MB格式:PDF出版:人民邮电出版社作者:阿尔·斯维加特
立即下载
Python编程快速上手:让繁琐工作自动化(第2版)
  • 类型:Python编程大小:145 MB格式:PDF出版:人民邮电出版社作者:阿尔•斯维加特
立即下载
树莓派Python编程入门与实战(第2版)
  • 类型:Python大小:94.6 MB格式:PDF出版:人民邮电出版社作者:勃鲁姆
立即下载
Python编程(第四版) 上册
  • 类型:Python编程大小:234.3 MB格式:PDF出版:中国电力出版社作者:Mark Lutz
立即下载
GO语言编程从入门到实践
  • 类型:GO语言大小:43.8 MB格式:PDF出版:清华大学出版社作者:黄永祥
立即下载

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 更逼真的星星

为让星星的分布更逼真,可随机地放置星星。本书前面说过,可像下面这样来生成随机数:

from random import randint

random_number = randint(-10,10)

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 雨滴

寻找一幅雨滴图像,并创建一系列整齐排列的雨滴。让这些雨滴往下落,直到到达屏幕底端后消失。

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 连绵细雨

修改为完成练习13-3而编写的代码,使得一行雨滴消失在屏幕底端后,屏幕顶端又出现一行新雨滴,并开始往下落。

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()

 

实例详解Python编程实现生成特定范围内不重复多个随机数的2种方法

《Python编程:从入门到实践》第五章:if语句

《Python编程:从入门到实践》学习笔记

《Python编程:从入门到实践》第九章:类

18天学习《python编程:从入门到实践》心得笔记

以上就是本次给大家分享的全部知识点内容总结,大家还可以在下方相关文章里找到spring+springmvc+mybatis整合注、 解决axios.interceptors.respon、 儿童python编程入门书籍推、 等python文章进一步学习,感谢大家的阅读和支持。

  • 上一篇:《Python编程:从入门到实践》第十二章:武装飞船
  • 下一篇:《Python编程:从入门到实践》第十五章:生成数据
  • 展开 +

    收起 -

  • 《Python编程:从入门到实践》第九章:类
  • 《Python编程:从入门到实践》第十二章:武装飞船
  • 《Python编程:从入门到实践》第五章:if语句
  • Python编程必学书单
  • python编程写代码时几个坏习惯总结
  • 《Python编程:从入门到实践》课后习题及答案
  • python编程的习惯整理
  • 《Python编程:从入门到实践》第十章:文件和异常
  • 学习笔记

    Copyright 2018-2019 xz577.com 码农之家

    版权责任说明