欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python实践到入门,外星人项目12章的习题的自我练习

程序员文章站 2024-02-19 18:37:10
...

类似于外星人项目第一部分的拓展:


刚开始自学python,外星人项目12章,完成了一架飞船可以在屏幕上左右移动,并且可以开火。因为后面的几道习题是单独的,为了简单,就将其勉强凑一起,忽略一些细节问题。然后综合几道习题,完成火箭的上下左右移动。在此基础上,假设火箭向右端开火的情况。当然要实现向其他方向开火,只需要在相应位置更改就行。

1、主程序文件exercise_test.py(类似于外星人项目中的alien_invasion.py)

import sys
import pygame
from pygame.sprite import Group
import game_functions as gf
from settings import Settings
from rocket import Rocket


def run_game():
	
	#初始化背景设置
	pygame.init()
	#创建一个名为screen的显示窗口
	ai_settings = Settings()
	screen = pygame.display.set_mode((ai_settings.screen_width,
		ai_settings.screen_height))
	
	pygame.display.set_caption("Blue Sky")
	
	#创建火箭
	rocket = Rocket(ai_settings, screen)
	
	#创建一个用于存储子弹的编组
	bullets = Group()
	while True:
		gf.check_events(ai_settings, screen, rocket, bullets)
		rocket.update()
		gf.update_bullets(bullets, ai_settings)

		gf.update_screen(ai_settings, screen, rocket, bullets)
		

run_game()

2、game_function.py

import sys
import pygame
from bullet import Bullet

def check_keydown_events(event, ai_settings, screen, rocket, bullets):
	"""响应按键"""
	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
	elif event.key == pygame.K_SPACE:
		#创建一颗子弹,并将其加入到编组bullets中
		if len(bullets) < ai_settings.bullets_allowed:
			new_bullet = Bullet(ai_settings, screen, rocket)
			bullets.add(new_bullet)
		
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(ai_settings, screen, rocket, bullets):
	"""响应案件和鼠标事件"""
	for event in pygame.event.get():
		if event.type == pygame.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_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()

def update_bullets(bullets, ai_settings):
	"""更新子弹的位置,并删除已消失的子弹"""
	bullets.update()
	#删除已消失的子弹,利用子弹右边界不能超过屏幕宽度来设定
	for bullet in bullets.copy():
		if bullet.rect.right >= ai_settings.screen_width:
			bullets.remove(bullet)
	#可通过下面代码来检测是否成功
	"""print(len(bullets))"""

3、rocket.py

import pygame

class Rocket():
	def __init__(self, ai_settings, screen):
		#初始化火箭并且设置其初始位置
		self.screen = screen
		self.ai_settings = ai_settings
		#加载火箭图像并获取其外接矩形
		self.image = pygame.image.load('images/rocket.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
		
		#在飞船的属性center中存储小数值
		self.center = float(self.rect.centerx)
		self.center_up_down = 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_settings.rocket_speed_factor
		if self.moving_left and self.rect.left > 0:
			self.center -= self.ai_settings.rocket_speed_factor
		if self.moving_up and self.rect.top > 0:
			self.center_up_down -= self.ai_settings.rocket_speed_factor
		if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
			self.center_up_down += self.ai_settings.rocket_speed_factor
			
		#根据self.center更新rect对象
		self.rect.centerx = self.center
		self.rect.centery = self.center_up_down

	def blitme(self):
		"""在指定位置绘制火箭"""
		self.screen.blit(self.image, self.rect)

4、setting.py

class Settings():
	"""创建包含所有设置的类"""
	
	def __init__(self):
		self.screen_width = 800
		self.screen_height = 600
		self.bg_color = (0, 191, 255)
		
		#设置飞船的速度
		self.rocket_speed_factor = 1.5
		#子弹设置
		self.bullet_speed_factor = 1
		self.bullet_width = 15
		self.bullet_height = 3
		self.bullet_color = 60, 60, 60
		self.bullets_allowed = 3

5、bullet.py

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
	"""一个对飞船发射的子弹进行管理的类"""
	
	def __init__(self, ai_settings, screen, rocket):
		"""在飞船所在的位置创建一个子弹对象"""
		super().__init__()
		self.screen = screen
		
		#在(0,0)处创建一个表示子弹的矩形,再设置正确的位置
		self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height)
		self.rect.centerx = rocket.rect.centerx
		self.rect.centery = rocket.rect.centery
		
		#存储用小数点表示的子弹位置
		self.y = float(self.rect.y)
		self.x = float(self.rect.x)
		
		self.color = ai_settings.bullet_color
		self.speed_factor = ai_settings.bullet_speed_factor
		
	def update(self):
		"""向有移动子弹"""
		#更新表示子弹位置的小数值,通过换x、y来决定子弹的方向
		self.x += self.speed_factor
		#更新表示子弹的rect的位置
		self.rect.x = self.x
			
	def draw_bullet(self):
		"""在屏幕上绘制子弹"""
		pygame.draw.rect(self.screen, self.color, self.rect)