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

《Python编程:从入门到实践》第十二章:武装飞船

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

Python编程:从入门到实践

这篇文章主要知识点是关于Python编程,从入门到实践,武装飞船,Python编程:从入门到实践,python编程写代码时几个坏习惯总结 《Python编程:从入门到实践》第八章:函数 《Python编程:从入门到实践》第九章:类 自学python编程的方法路线 18天学习《python编程:从入门到实践》心得笔记 的内容,如果大家想对相关知识点有系统深入的学习,可以参阅以下电子书

零基础Python编程入门与实战
  • 类型:Python大小:30.6 MB格式:PDF出版:化学工业出版社作者:吴晶晶
立即下载
疯狂Python讲义
疯狂Python讲义含源码工具版
  • 类型:Python编程大小:124.3 MB格式:PDF出版:电子工业出版社作者:李刚
立即下载
Python编程无师自通
  • 类型:Python编程大小:122.4 MB格式:PDF出版:人民邮电出版社作者:科里·奥尔索夫
立即下载
Python编程入门指南(上下册)
  • 类型:Python编程大小:15.4 MB格式:PDF出版:电子工业出版社作者:明日科技
立即下载
我的Python世界(Minecraft我的世界)
  • 类型:Python编程大小:73 MB格式:PDF出版:人民邮电出版社作者:程晨
立即下载
自学Python编程基础学习笔记
  • 类型:Python笔记大小:16.1 MB格式:PDF出版:python学习笔记作者:python程序员
立即下载
Head First Python(第2版)
  • 类型:Python入门大小:86.9 MB格式:PDF出版:中国电力出版社作者:Paul Barry
立即下载
树莓派Python编程入门与实战(第2版)
  • 类型:Python大小:94.6 MB格式:PDF出版:人民邮电出版社作者:勃鲁姆
立即下载
Python编程快速上手:让繁琐工作自动化(第2版)
  • 类型:Python编程大小:145 MB格式:PDF出版:人民邮电出版社作者:阿尔•斯维加特
立即下载

12-1 蓝色天空

创建一个背景为蓝色的Pygame窗口。

import sys
import pygame
def run():
	pygame.init()
	screen = pygame.display.set_mode((1200,600))
	pygame.display.set_caption("Blue Sky")

	bg_color = (0,124,195)

	while  True:
		screen.fill(bg_color)
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				sys.exit()
		pygame.display.flip()
run()

结果:


 

12-2 游戏角色

找一幅你喜欢的游戏角色位图图像或将一幅图像转换为位图。创建一个类,将该角色绘制到屏幕中央,并将该图像的背景色设置为屏幕背景色,或将 屏幕背景色设置为该图像的背景色

ship.py

import pygame

class Ship():
	"""docstring for Ship"""
	def __init__(self, screen):
		"""初始化飞船并设置其初始位置"""
		self.screen = screen
		# 加载飞船图像并获取其外接矩形
		self.image = pygame.image.load('images/ship.bmp')
		self.rect = self.image.get_rect()
		self.screen_rect = screen.get_rect()

		# 将每搜新飞船放在屏幕底部中央
		self.rect.centerx = self.screen_rect.centerx
		self.rect.centery = self.screen_rect.centery

	def biltme(self):
		self.screen.blit(self.image,self.rect)

run.py

import sys
import pygame
from ship import Ship
def run():
	pygame.init()
	screen = pygame.display.set_mode((1200,600))
	pygame.display.set_caption("Blue Sky")

	bg_color = (0,124,195)
	s = Ship(screen)
	while  True:
		screen.fill(bg_color)
		s.biltme()
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				sys.exit()
		pygame.display.flip()
run()

结果:


 

12-3 火箭

编写一个游戏,开始时屏幕中央有一个火箭,而玩家可使用四个方向键上下左右移动火箭。请务必确保火箭不会移到屏幕外面。

setting.py

class Setting():

	def __init__(self):
		"""初始化游戏的设置"""
		#屏幕设置
		self.screen_width = 1200
		self.screen_height = 600
		self.bg_color = (255,255,255)
		self.ship_speed_factor = 1.5

rocket.py

import pygame
class Rocket():
	"""docstring for Rocket"""
	def __init__(self,ai_setting, screen):
		self.screen = screen
		self.image = pygame.image.load('images/ship.jpg')
		self.rect = self.image.get_rect()
		self.screen_rect = screen.get_rect()
		self.ai_setting = ai_setting
		self.rect.centerx = self.screen_rect.centerx
		self.rect.centery = self.screen_rect.centery
		self.center = float(self.rect.centerx)
		self.centery = float(self.rect.centery)
		self.moving_right = False
		self.moving_left = False
		self.moving_up = False
		self.moving_down = False
	def update(self):
		if self.moving_right and self.rect.right < self.screen_rect.right:
			self.center +=self.ai_setting.ship_speed_factor
		if self.moving_left and self.rect.left > 0:
			self.center -=self.ai_setting.ship_speed_factor
		if self.moving_up and self.rect.top > 0:
			self.centery -= self.ai_setting.ship_speed_factor
		if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
			self.centery +=self.ai_setting.ship_speed_factor
		self.rect.centerx = self.center
		self.rect.centery = self.centery
	def blitme(self):
		self.screen.blit(self.image,self.rect)

game_functions.py

import sys

import pygame
def check_keydown_events(event,rocket):
	if event.key == pygame.K_RIGHT:
		rocket.moving_right = True
	elif event.key == pygame.K_LEFT:
		rocket.moving_left = True
	elif event.key == pygame.K_UP:
		rocket.moving_up = True
	elif event.key ==pygame.K_DOWN:
		rocket.moving_down = True
def check_keyup_events(event,rocket):
	if event.key == pygame.K_RIGHT:
		rocket.moving_right = False
	elif event.key == pygame.K_LEFT:
		rocket.moving_left = False
	elif event.key == pygame.K_UP:
		rocket.moving_up = False
	elif event.key == pygame.K_DOWN:
		rocket.moving_down = False
def check_events(rocket):
	"""响应按键和鼠标事件"""
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			print("Quit")
			sys.exit()
		elif event.type == pygame.KEYDOWN:
			check_keydown_events(event,rocket)
		elif event.type == pygame.KEYUP:
			check_keyup_events(event,rocket)
def update_screen(ai_settings,screen,rocket):
	screen.fill(ai_settings.bg_color)
	rocket.blitme()
	pygame.display.flip()

run.py

import sys
import pygame
from rocket import Rocket
from setting import Setting
import game_functions as gf
def run():
	pygame.init()
	ai_setting = Setting()
	screen =pygame.display.set_mode((ai_setting.screen_width,ai_setting.screen_height))
	pygame.display.set_caption("Rocket")
	r = Rocket(ai_setting,screen)
	while True:
		gf.check_events(r)
		r.update()
		gf.update_screen(ai_setting,screen,r)
run()

结果:

12-4 按键

创建一个程序,显示一个空屏幕。在事件循环中,每当检测到pygame.KEYDOWN 事件时都打印属性event.key 。运行这个程序,并按各种键,看看 Pygame如何响应。

import sys
import pygame
def test():
	pygame.init()
	screen = pygame.display.set_mode((900,600))
	pygame.display.set_caption("12-4")
	bg_color = (255,255,255)
	while True:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				sys.exit()
			elif event.type == pygame.KEYDOWN:
				if event.key == pygame.K_UP:
					bg_color=(255,0,255)
				elif event.key == pygame.K_DOWN:
					bg_color=(0,255,255)
				elif event.key == pygame.K_LEFT:
					bg_color=(255,255,0)
				elif event.key == pygame.K_RIGHT:
					bg_color = (0,0,255)
		screen.fill(bg_color)
		pygame.display.flip()
test()		

结果:

K_UP:

K_DOWN:

K_LEFT:

K_RIGHT:

12-5 侧面射击

编写一个游戏,将一艘飞船放在屏幕左边,并允许玩家上下移动飞船。在玩家按空格键时,让飞船发射一颗在屏幕中向右穿行的子弹,并在子弹离开 屏幕而消失后将其删除。

game_functions.py

import sys

import pygame
from bullet import Bullets
def check_keydown_events(event,ai_setting,screen,rocket,bullets):
	if event.key == pygame.K_UP:
		rocket.moving_up = True
	elif event.key == pygame.K_DOWN:
		rocket.moving_down = True
	elif event.key == pygame.K_SPACE:
		new_bullet = Bullets(ai_setting,screen,rocket)
		bullets.add(new_bullet)
def check_keyup_events(event,rocket):
	if event.key == pygame.K_UP:
		rocket.moving_up = False
	elif event.key == pygame.K_DOWN:
		rocket.moving_down = False
def check_events(ai_settings,screen,rocket,bullets):
	"""响应按键和鼠标事件"""
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			print("Quit")
			sys.exit()
		elif event.type == pygame.KEYDOWN:
			check_keydown_events(event,ai_settings,screen,rocket,bullets)
		elif event.type == pygame.KEYUP:
			check_keyup_events(event,rocket)
def update_bullet(bullets):
	for bullet in bullets:
		if bullet.rect_left<=0:
			bullets.remove(bullet)
def update_screen(ai_settings,screen,rocket,bullets):
	screen.fill(ai_settings.bg_color)
	for bullet in bullets.sprites():
		bullet.draw_bullet()
	rocket.blitme()
	pygame.display.flip()

bullet.py

import pygame
from pygame.sprite import Sprite
class Bullets(Sprite):
	"""docstring for Bullets"""
	def __init__(self, ai_settings,screen,rocket):
		super( Bullets, self).__init__()
		self.screen = screen

		self.rect = pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
		self.rect.centery = rocket.rect.centery
		self.rect.right = rocket.rect.right

		#存储小数表示的子弹位置
		self.x = float(self.rect.x)
		self.color = ai_settings.bullet_color
		self.speed_factor = ai_settings.bullet_speed_factor
	def update(self):
		self.x +=self.speed_factor
		self.rect.x = self.x
	def draw_bullet(self):
		pygame.draw.rect(self.screen,self.color,self.rect)


setting.py

class Setting():

	def __init__(self):
		"""初始化游戏的设置"""
		#屏幕设置
		self.screen_width = 1200
		self.screen_height = 600
		self.bg_color = (255,255,255)
		self.ship_speed_factor = 1.5

		self.bullet_speed_factor = 1
		self.bullet_width = 15
		self.bullet_height = 3
		self.bullet_color =(60,60,60)

rocket.py

import pygame
class Rocket():
	"""docstring for Rocket"""
	def __init__(self,ai_setting, screen):
		self.screen = screen
		self.image = pygame.image.load('images/rocket.jpg')
		self.rect = self.image.get_rect()
		self.screen_rect = screen.get_rect()
		self.ai_setting = ai_setting
		self.rect.x = 0
		self.rect.centery = self.screen_rect.centery
		self.center = float(self.rect.centerx)
		self.centery = float(self.rect.centery)
		self.moving_up = False
		self.moving_down = False
	def update(self):
		if self.moving_up and self.rect.top > 0:
			self.centery -= self.ai_setting.ship_speed_factor
		if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
			self.centery +=self.ai_setting.ship_speed_factor
		self.rect.centery = self.centery
	def blitme(self):
		self.screen.blit(self.image,self.rect)

run.py

import sys
import pygame
from rocket import Rocket
from setting import Setting
from pygame.sprite import Group
import game_functions as gf
def run():
	pygame.init()
	ai_setting = Setting()
	screen =pygame.display.set_mode((ai_setting.screen_width,ai_setting.screen_height))
	pygame.display.set_caption("Rocket")
	r = Rocket(ai_setting,screen)
	bullets = Group()
	while True:
		gf.check_events(ai_setting,screen,r,bullets)
		r.update()
		bullets.update()
		gf.update_screen(ai_setting,screen,r,bullets)
run()

结果:

《Python编程:从入门到实践》课后习题及答案

python编程学习np.float 被删除的问题解析

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

《Python编程:从入门到实践》第六章:字典

《Python编程:从入门到实践》第十章:文件和异常

以上就是本次给大家分享的全部知识点内容总结,大家还可以在下方相关文章里找到vue项目中使用md5加密以及、 spring+springmvc+mybatis整合注、 解决axios.interceptors.respon、 等python文章进一步学习,感谢大家的阅读和支持。

  • 上一篇:《Python编程:从入门到实践》第十一章:测试代码
  • 下一篇:《Python编程:从入门到实践》第十三章:外星人
  • 展开 +

    收起 -

  • 自学python编程的方法路线
  • 《Python编程:从入门到实践》学习笔记
  • 《Python编程:从入门到实践》课后习题及答案
  • Python编程实现输入某年某月某日计算出这一天是该年第几天的方法
  • python编程学习np.float 被删除的问题解析
  • Python小程序编程实现一键自动整理文件解压文件
  • 《Python编程:从入门到实践》第九章:类
  • 《Python编程:入门到实践》第七章:用户输入和while循环
  • 学习笔记

    Copyright 2018-2019 xz577.com 码农之家

    版权责任说明