《python编程从入门到实践》 第十二章课程代码

**

《python编程从入门到实践》 第十二章课程代码

**

模块1 alien_invasion

#coding=utf-8
import sys        
import pygame
import game_function as gf
from settings import Settings
from ship import Ship 
from pygame.sprite import Group

def run_game():
	#初始化
	pygame.init()
	ai_settings=Settings()
	screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
	pygame.display.set_caption('外星人入侵')
	ship=Ship(ai_settings,screen)
	#创建储存子弹的编组
	bullets=Group()
	#主循环
	while True:
		gf.check_events(ai_settings,screen,ship,bullets)
		ship.update()
		gf.update_bullet(bullets)
		print(len(bullets))
		gf.update_screen(ai_settings,screen,ship,bullets)
#主函数
run_game()


模块2 ship

#coding=utf-8
import pygame
class Ship():
	#除了自身参数外,增加screen以此获得坐标
	def __init__(self,ai_settings,screen):
		self.screen=screen
		self.ai_settings=ai_settings
		self.image=pygame.image.load('image/imageship.bmp')
		#图片太大了将其缩小
		self.image=pygame.transform.scale(self.image,(50,50))
		#image和screen的外接矩形 把图片规则化
		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.center=float(self.rect.centerx)
		self.moving_right=False
		self.moving_left=False
	def update(self):
		if self.moving_right and self.rect.right<self.screen_rect.right:
			self.rect.centerx+=self.ai_settings.ship_speed_factor
		if self.moving_left and self.rect.left>self.screen_rect.left:
			self.rect.centerx-=self.ai_settings.ship_speed_factor
	#绘制飞船
	def blitme(self):
		self.screen.blit(self.image,self.rect)

模块3 settings

class Settings():
	def __init__(self):
		self.screen_width=800
		self.screen_height=600
		self.bg_color=(230,230,230)
		self.ship_speed_factor=1.5
		self.bullet_speed=1
		self.bullet_width=3
		self.bullet_height=15
		self.bullet_color=60,60,60
		self.bullet_allowed=3

模块4 game_function

#coding=utf-8
import sys
import pygame
from bullet import Bullet

def check_keydown(event,ai_settings,screen,ship,bullets):
	if event.key==pygame.K_RIGHT:
		ship.moving_right=True
	elif event.key==pygame.K_LEFT:
		ship.moving_left=True
	elif event.key==pygame.K_SPACE:
		fire_bullet(ai_settings,screen,ship,bullets)
			
def fire_bullet(ai_settings,screen,ship,bullets):
	if len(bullets)<=ai_settings.bullet_allowed:
			new_bullet=Bullet(ai_settings,screen,ship)
			bullets.add(new_bullet)
			
def check_keyup(event,ship):
	if event.key==pygame.K_RIGHT:
		ship.moving_right=False	
	elif event.key==pygame.K_LEFT:
		ship.moving_left=False		
		
def check_events(ai_settings,screen,ship,bullets):
	for event in pygame.event.get():
		if event.type==pygame.QUIT:
			sys.exit()
		elif event.type==pygame.KEYDOWN:
			check_keydown(event,ai_settings,screen,ship,bullets)
		elif event.type==pygame.KEYUP:
			check_keyup(event,ship)			

def update_screen(ai_settings,screen,ship,bullets):
	screen.fill(ai_settings.bg_color)
	for bullet in bullets.sprites():
		bullet.draw_bullet()
	ship.blitme()
	pygame.display.flip()
	
def update_bullet(bullets):
	bullets.update()
	for bullet in bullets.copy():
			if bullet.rect.bottom<=0:
				bullets.remove(bullet)
	

模块4 bullet

#coding=utf-8
import pygame
from pygame.sprite import Sprite

#创建一个bullet的类并继承sprite所有属性
class Bullet(Sprite):
	def __init__(self,ai_settings,screen,ship):
		super().__init__()#继承sprite的构造函数属性和函数
		self.screen=screen
		#在(0,0)处创建子弹,在放置于正确位置
		self.rect=pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
		self.rect.centerx=ship.rect.centerx
		self.rect.top=ship.rect.top
		self.color=ai_settings.bullet_color
		self.speed=ai_settings.bullet_speed
		self.y=float(self.rect.y)
	def update(self):
		self.y-=self.speed
		self.rect.y=self.y
	def draw_bullet(self):
		pygame.draw.rect(self.screen,self.color,self.rect)

飞船图片
飞船图片

你可能感兴趣的:(《python编程从入门到实践》 第十二章课程代码)