項目代碼 | planepython
main.py
主程序文件,啓動文件git
# -*- 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
基類github
# -*- 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
飛機基類bash
# -*- 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
敵機對象app
# -*- 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
子彈基類dom
# -*- 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
敵機子彈對象ui
# -*- 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
窗口對象spa
# -*- 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))
複製代碼
python開發等相關IT技術q羣: 887934385 提供資料,部分相關源碼 共同探討
不經常使用q能夠掃如下二維碼添加助理客服小姐姐微信,經過後第一時間回覆
關注微信公衆號「 python社區營 」 第一時間學習更多知識