專案程式碼 | plane
main.py
主程式檔案,啟動檔案
# -*- coding:utf-8 -*-
import pygame, time
from Plane import Plane
from HeroPlane import HeroPlane
from Screen import Screen
from pygame.locals import *
def key_control(plane_temp):
# 獲取事件,比如按鍵等
for event in pygame.event.get():
# 判斷是否是點選了退出按鈕
if event.type == QUIT:
print("exit")
exit()
# 判斷是否是按下了鍵
elif event.type == KEYDOWN:
# 檢測按鍵是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
plane_temp.move_left()
# 檢測按鍵是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
plane_temp.move_right()
# 檢測按鍵是否是空格鍵
elif event.key == K_SPACE:
print('space')
plane_temp.fire()
def main():
screen = pygame.display.set_mode((480, 852), 0, 32)
# 建立視窗物件
screen_temp = Screen(screen)
# 建立一個飛機物件
plane = Plane(screen)
# 建立敵機物件
enemyPlane = HeroPlane(screen)
while True:
screen_temp.display() # 顯示視窗
plane.display(plane.bullet_list) # 顯示飛機
enemyPlane.display(enemyPlane.bullet_list) # 敵機顯示
enemyPlane.move() # 敵機移動
enemyPlane.fire() # 敵機開火
key_control(plane) # 鍵盤事件監聽
pygame.display.update() # 更新視窗
time.sleep(0.01) # 延時0.01秒,防止程式記憶體溢位
if __name__ == '__main__':
main()
Base.py
基類
# -*- coding:utf-8 -*-
import pygame
class Base(object):
# 背景圖片
image = None
def __init__(self, screen_temp, x, y, image_path):
self.x = x
self.y = y
self.screen = screen_temp
self.image_load(image_path)
# 飛機賦值圖片物件
def image_load(self, image_path):
self.image = pygame.image.load(image_path)
BasePlane.py
飛機基類
# -*- coding:utf-8 -*-
from Base import Base
class BasePlane(Base):
def __init__(self, screen_temp, x, y, image_path):
Base.__init__(self, screen_temp, x, y, image_path)
# 顯示飛機
def display(self, bullet_list):
self.screen.blit(self.image, (self.x, self.y)) # 顯示飛機
for bullet in bullet_list: # 迴圈取出子彈物件
# 判斷子彈是否越界
if bullet.judge():
bullet_list.remove(bullet) # 如果子彈越界就刪除子彈
bullet.display() # 顯示子彈
bullet.move() # 子彈移動步長
Plane.py
飛機物件
# -*- coding:utf-8 -*-
from Bullet import Bullet
from BasePlane import BasePlane
class Plane(BasePlane):
# 儲存子彈物件
bullet_list = []
def __init__(self, screen_temp):
BasePlane.__init__(self, screen_temp, 210, 700, "./resource/hero1.png")
# 飛機向左移動偏移量
def move_left(self):
self.x -= 10
# 飛機向右移動偏移量
def move_right(self):
self.x += 10
# 將飛機建立的子彈物件儲存進 bullet_list 列表中
def fire(self):
self.bullet_list.append(Bullet(self.screen, self.x, self.y))
print(self.bullet_list)
HeroPlane.py
敵機物件
# -*- coding:utf-8 -*-
import random
from BasePlane import BasePlane
from EnemyBullet import EnemyBullet
class HeroPlane(BasePlane):
# 定義一個類屬性用來儲存
direction = 'right'
# 儲存子彈物件
bullet_list = []
def __init__(self, screen_temp):
BasePlane.__init__(self, screen_temp, 0, 0, "./resource/enemy-1.gif")
# 敵機移動軌跡
def move(self):
# 敵機建立的子彈預設向右移動
if self.direction == 'right':
self.x += 5 # 每次向右移動增加 5px 的步長
elif self.direction == 'left': # 向左移動
self.x -= 5 # 每次向左移動減少 5px 的步長
# 當敵機向右移動到了邊界就向左移動
if self.x > 480 - 50: # 480 是介面總寬度; 50 是飛機寬度. 所以敵機移動的距離應該是介面寬度-敵機寬度 ( 移動距離 = 介面寬度 - 敵機寬度 )
self.direction = 'left'
elif self.x <= 0: # 當敵機移動到了最左邊就會繼續向右移動
self.direction = 'right'
# 開火
def fire(self):
random_temp = random.randint(1, 100) # 隨機生成 1 - 100的隨機數
if (random_temp == 20) or (random_temp == 78): # 隨機數概率
self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y)) # 建立敵機子彈物件
BaseBullet.py
子彈基類
# -*- coding:utf-8 -*-
from Base import Base
class BaseBullet(Base):
def __init__(self, screen_temp, x, y, image_path):
Base.__init__(self, screen_temp, x, y, image_path)
# 子彈背景
def display(self):
self.screen.blit(self.image, (self.x, self.y))
Bullet.py
子彈物件
# -*- coding:utf-8 -*-
from BaseBullet import BaseBullet
class Bullet(BaseBullet):
def __init__(self, screen_temp, x, y):
BaseBullet.__init__(self, screen_temp, x + 40, y - 20, "./resource/bullet.png")
# 子彈步長
def move(self):
self.y -= 10
# 判斷子彈y軸是否已經越界
def judge(self):
if self.y < 0:
return True
else:
return False
EnemyBullet.py
敵機子彈物件
# -*- coding:utf-8 -*-
from BaseBullet import BaseBullet
class EnemyBullet(BaseBullet):
def __init__(self, screen_temp, x, y):
BaseBullet.__init__(self, screen_temp, x + 30, y + 30, "./resource/bullet-1.gif")
# 子彈移動步長
def move(self):
self.y += 20
# 判斷子彈y軸是否已經越界
def judge(self):
if self.y > 890: # 890 介面總高度
return True
else:
return False
Screen.py
視窗物件
# -*- coding:utf-8 -*-
from Base import Base
class Screen(Base):
def __init__(self, screen_temp):
Base.__init__(self, screen_temp, 0, 0, "./resource/background.png")
def display(self):
self.screen.blit(self.image, (self.x, self.y))