1. 程式人生 > >外星人大戰-----------------------------課後習題(從入門到實踐)第十二章

外星人大戰-----------------------------課後習題(從入門到實踐)第十二章

這是完成的一些課後習題。首先最原始的設定類,主程式

設定類:

#儲存作業例子的設定類
class Setting():
    def __init__(self):
        self.screen_width=800      #設定螢幕高度寬度,背景顏色
        self.screen_height=800
        self.bg_color=(230,230,230)
#-*-coding:GBK-*-
#-*-coding:utf-8-*-
import sys     #退出時需要這個模組
import pygame  #匯入遊戲模組
from setting import Setting    #匯入設定類

def run_game():
	pygame.init()  #初始化,建立螢幕物件
	ai_settings=Setting()    #建立一個800*800的螢幕
	screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))  
	pygame.display.set_caption("12-1")   #對於這個視窗的命名為    
      #開始遊戲的主程式碼迴圈
	while True:
		for event in pygame.event.get():     #監聽鍵盤和滑鼠,規定退出的情況
			if event.type==pygame.QUIT:
				sys.exit()  
		screen.fill(ai_settings.bg_color)
		pygame.display.flip()    #讓螢幕可見,重新整理螢幕
run_game()

 

1.建立一個背景為藍色的pygame視窗:只需要修改設定類就OK,修改顏色取值為(0,0,255)

#儲存外星人遊戲開發的設定
class Setting():
    def __init__(self):
        self.screen_width=800      #設定螢幕高度寬度,背景顏色
        self.screen_height=600
        self.bg_color=(0,0,255)

輸出為:

2.找一幅喜歡的遊戲角色點陣圖繪製到螢幕中央

先建立picture類:

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
#建立圖片類
import pygame

class Picture(): 
	def __init__(self,screen):
		self.screen=screen

     #圖片存在images資料夾下,名字是picture.bmp    
		self.image=pygame.image.load('images/picture.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 blitme(self):
		self.screen.blit(self.image,self.rect)   

主程式:

#-*-coding:GBK-*-
#-*-coding:utf-8-*-


import sys     #退出時需要這個模組
import pygame  #匯入遊戲模組
from setting import Setting    #匯入設定類
from picture import Picture   #匯入圖片類

def run_game():
	pygame.init()  #初始化,建立螢幕物件
	ai_settings=Setting()    #建立一個800*800的螢幕
	screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))  
	pygame.display.set_caption("12-2")   #對於這個視窗的命名

	picture=Picture(screen)   #建立圖片例項
    
   # "開始遊戲的主程式碼迴圈"
	while True:
		for event in pygame.event.get():     #監聽鍵盤和滑鼠,規定退出的情況
			if event.type==pygame.QUIT:
				sys.exit()  
		screen.fill(ai_settings.bg_color)
		picture.blitme()   
		pygame.display.flip()    #讓螢幕可見,重新整理螢幕
run_game()

輸出如下:

3.火箭,編寫一個遊戲,開始螢幕的時候處於中央,玩家可以使用四個箭頭上下左右移動,不會移出螢幕外。

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
#儲存作業例子的設定類
class Setting():
    def __init__(self):
        self.screen_width=800      #設定螢幕高度寬度,背景顏色
        self.screen_height=600
        self.bg_color=(230,230,230)

設定圖片類:

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
#建立圖片類
#建立圖片類
import pygame

class Picture(): 
	def __init__(self,screen):
		self.screen=screen

     #圖片存在images資料夾下,名字是picture.bmp    
		self.image=pygame.image.load('images/picture.bmp')  
		self.rect=self.image.get_rect()
		self.screen_rect=screen.get_rect()
		self.moving_up=False
		self.moving_down=False
		self.moving_right=False
		self.moving_left=False
     #設定螢幕中央
		self.rect.centerx=self.screen_rect.centerx
		self.rect.centery=self.screen_rect.centery

	def update(self):
		if self.moving_up and self.rect.top>0:
			self.rect.centery-=1
		if self.moving_down and self.rect.bottom<self.screen_rect.bottom:
			self.rect.centery+=1
		if self.moving_right and self.rect.right<self.screen_rect.right:
			self.rect.centerx+=1
		if self.moving_left and self.rect.left>0:
			self.rect.centerx-=1
   
	def blitme(self):
		self.screen.blit(self.image,self.rect)

設定功能模組,檢查按鍵等功能:

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
#功能模組,包括按鍵的設定檢測,退出遊戲設定
import pygame
import sys

#對按鍵的響應
def check_keydown_events(event,picture):
	if event.key==pygame.K_UP:
		picture.moving_up=True
	elif event.key==pygame.K_DOWN:
		picture.moving_down=True
	elif event.key==pygame.K_RIGHT:
		picture.moving_right=True
	elif event.key==pygame.K_LEFT:
		picture.moving_left=True
 
#鬆開鍵盤的響應
def check_keyup_events(event,picture):
    if event.key==pygame.K_UP:
        picture.moving_up=False
    elif event.key==pygame.K_DOWN:
        picture.moving_down=False
    elif event.key==pygame.K_RIGHT:
        picture.moving_right=False
    elif event.key==pygame.K_LEFT:
        picture.moving_left=False

#檢測滑鼠鍵盤事件
def check_events(picture):
	for event in pygame.event.get():
		if event.type==pygame.QUIT:
			sys.exit()
		elif event.type==pygame.KEYDOWN:
			check_keydown_events(event,picture)
		elif event.type==pygame.KEYUP:
			check_keyup_events(event,picture)

#更新螢幕:
def update_screen(ai_settings,screen,picture):
    screen.fill(ai_settings.bg_color)
    picture.blitme()
    pygame.display.flip()

主程式:

#-*-coding:GBK-*-
#-*-coding:utf-8-*-

import sys     #退出時需要這個模組
import pygame  #匯入遊戲模組
from setting import Setting    #匯入設定類
from picture import Picture   #匯入圖片類
import game_functions as gf  #匯入功能模組

def run_game():
    pygame.init()  #初始化,建立螢幕物件
    ai_settings=Setting()    #建立一個800*800的螢幕
    screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))  
    pygame.display.set_caption("12-3")   #對於這個視窗的命名

    picture=Picture(screen)   #建立圖片例項
    
    "開始遊戲的主程式碼迴圈"
    while True:
        gf.check_events(picture)
        picture.update()
        gf.update_screen(ai_settings,screen,picture)


run_game()

執行主程式:

4.建立一個程式,顯示一個空的螢幕,在事件迴圈裡,每當檢測到pygame.KEYDOWN事件都列印屬性event.key,執行這個程式,按下其他鍵,看看響應結果:

 

5.編寫一個遊戲,將一艘飛船放在螢幕左邊,並允許使用者上下移動飛船,在玩家按空格鍵時,讓飛船發射一顆在螢幕中向右穿行的子彈,在子彈離開螢幕而消失後將其刪除。

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
#儲存作業例子的設定類
#儲存作業例子的設定類
class Setting():
    def __init__(self):
        self.screen_width=800      #設定螢幕高度寬度,背景顏色
        self.screen_height=600
        self.bg_color=(230,230,230)
        #設定飛船移動速度
        self.ship_speed_factor=1.3
        #設定子彈引數
        self.bullet_width=3
        self.bullet_height=10
        self.bullet_color=(255,0,0)
        self.bullet_speed_factor=1.1

修改圖片類:放入飛船圖片,調整位置

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
#建立圖片類
#建立圖片類
import pygame
class Picture(): 
	def __init__(self,screen,ai_settings):
		self.screen=screen
		self.ai_settings=ai_settings

     #圖片存在images資料夾下,名字是ship.bmp    
		self.image=pygame.image.load('images/ship.bmp')  
		self.rect=self.image.get_rect()
		self.screen_rect=screen.get_rect()
		self.moving_up=False
		self.moving_down=False   
		
     
     #設定螢幕左邊中央
		self.rect.x=0
		self.rect.centery=self.screen_rect.centery	
		
		self.ship_place=float(self.rect.centery)

	def update(self):
		if self.moving_up and self.rect.top>0:
			self.ship_place-=self.ai_settings.ship_speed_factor
		if self.moving_down and self.rect.bottom<self.screen_rect.bottom:
			self.ship_place+=self.ai_settings.ship_speed_factor
		self.rect.centery=self.ship_place
   
	def blitme(self):
		self.screen.blit(self.image,self.rect)

新增一個子彈類bullet.py

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
#子彈設定類
import pygame
from pygame.sprite import Sprite
 
class Bullet(Sprite):
    def __init__(self,ai_settings,screen,picture):
        super().__init__()
        self.screen=screen
        self.ai_settings=ai_settings
        self.picture=picture
       #建立子彈
        self.rect=pygame.Rect(0,0,self.ai_settings.bullet_width,self.ai_settings.bullet_height)
        self.rect.centery=picture.rect.centery
        self.rect.right=picture.rect.right
        self.color=self.ai_settings.bullet_color
        self.speed_factor=self.ai_settings.bullet_speed_factor
        self.bullet_place=float(self.rect.x)

 #更新子彈位置
    def update(self):
        self.bullet_place+=self.speed_factor
        self.rect.x=self.bullet_place

#繪製子彈
    def draw_bullet(self):
        pygame.draw.rect(self.screen,self.color,self.rect)



修改功能模組:

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
#功能模組,包括按鍵的設定檢測,退出遊戲設定
#功能模組,包括按鍵的設定檢測,退出遊戲設定
import pygame
import sys
from bullet import Bullet

#對按鍵的響應
def check_keydown_events(event,ai_settings,screen,picture,bullets):
    if event.key==pygame.K_UP:
        picture.moving_up=True
    elif event.key==pygame.K_DOWN:
        picture.moving_down=True
    elif event.key==pygame.K_SPACE:
        fire_bullet(ai_settings,screen,picture,bullets)  
 
#鬆開鍵盤的響應
def check_keyup_events(event,picture):
    if event.key==pygame.K_UP:
        picture.moving_up=False
    elif event.key==pygame.K_DOWN:
        picture.moving_down=False


#檢測滑鼠鍵盤事件
def check_events(ai_settings,screen,picture,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,picture,bullets)
        elif event.type==pygame.KEYUP:
            check_keyup_events(event,picture)

#發射子彈
def fire_bullet(ai_settings,screen,picture,bullets):
    new_bullet=Bullet(ai_settings,screen,picture)
    bullets.add(new_bullet)

#更新子彈位置並刪除消失的子彈
def update_bullets(bullets,screen):
	bullets.update()
	screen_rect=screen.get_rect()
	for bullet in bullets.copy():
		if bullet.rect.left>screen_rect.right:
			bullets.remove(bullet)

#更新螢幕:
def update_screen(ai_settings,screen,picture,bullets):
    screen.fill(ai_settings.bg_color)
    picture.blitme()
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    pygame.display.flip()

主程式:

#-*-coding:GBK-*-
#-*-coding:utf-8-*-

import sys     #退出時需要這個模組
import pygame  #匯入遊戲模組
from setting import Setting    #匯入設定類
from picture import Picture   #匯入圖片類
from pygame.sprite import Group
import game_functions as gf  #匯入功能模組

def run_game():
    pygame.init()  #初始化,建立螢幕物件
    ai_settings=Setting()    #建立一個800*800的螢幕
    screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))  
    pygame.display.set_caption("12-5")   #對於這個視窗的命名

    picture=Picture(screen,ai_settings)   #建立圖片例項
    bullets=Group()
    
    "開始遊戲的主程式碼迴圈"
    while True:
        gf.check_events(ai_settings,screen,picture,bullets)
        picture.update()
        gf.update_bullets(bullets,screen)
        gf.update_screen(ai_settings,screen,picture,bullets)


run_game()

執行主程式:可以實現功能

注意:這裡有一個問題:這幾天一直在困擾我:

我的picture類之前的程式碼是這樣的:

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
#建立圖片類
#建立圖片類
import pygame
class Picture(): 
	def __init__(self,screen,ai_settings):
		self.screen=screen
		self.ai_settings=ai_settings

     #圖片存在images資料夾下,名字是ship.bmp    
		self.image=pygame.image.load('images/ship.bmp')  
		self.rect=self.image.get_rect()
		self.screen_rect=screen.get_rect()
		self.moving_up=False
		self.moving_down=False   
		self.ship_place=float(self.rect.centery)
     
     #設定螢幕左邊中央
		self.rect.x=0
		self.rect.centery=self.screen_rect.centery	
		
		

	def update(self):
		if self.moving_up and self.rect.top>0:
			self.ship_place-=self.ai_settings.ship_speed_factor
		if self.moving_down and self.rect.bottom<self.screen_rect.bottom:
			self.ship_place+=self.ai_settings.ship_speed_factor
		self.rect.centery=self.ship_place
   
	def blitme(self):
		self.screen.blit(self.image,self.rect)

輸出介面在左上角,我檢查了很多遍的程式碼我都覺得沒有問題;所以這周我是特別的崩潰的,知道週六我才打起精神,準備再從頭到尾做一遍,我發現無論我怎麼改動位置y都沒有用,後來把self.ship_place=float(self.rect.centery)這行程式碼放到設定位置的後面就OK了,我覺得應該是先設定位置,再來設定移動的介面,但我又想不通,前面我只是設定引數,這應該沒有問題啊~~~~~想不通,如果有大佬看到了這個文章,能給我答覆嘛!我也會去找到理由的。