1. 程式人生 > >【Python程式設計:從入門到實踐】第十二章:武裝飛船

【Python程式設計:從入門到實踐】第十二章:武裝飛船

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

結果: