python實(shí)現(xiàn)簡單飛機(jī)大戰(zhàn)小游戲
為了熟悉Python基礎(chǔ)語法,學(xué)習(xí)了一個(gè)經(jīng)典的案例:飛機(jī)大戰(zhàn),最后實(shí)現(xiàn)效果如下:

實(shí)現(xiàn)步驟:
①下載64位對應(yīng)python版本的pygame:pygame-1.9.6-cp38-cp38-win_amd64.whl
② 更新pip:python -m pip install --upgrade pip
③ 安裝pygame:pip install pygame-1.9.6-cp38-cp38-win_amd64.whl
④ 實(shí)現(xiàn)代碼如下:
import sys ?# 導(dǎo)入系統(tǒng)模塊
import random ?# 隨機(jī)數(shù)模塊
import pygame ?# 導(dǎo)入pygame模塊
import pygame.locals ?# 導(dǎo)入pygame本地策略
ICO_PATH = "images/app.ico" ?# APP_ICO
APP_NAME = "飛機(jī)大戰(zhàn)V1.0" ?# APP_NAME
Enemy_IMGS = ("images/enemy1.png", "images/enemy2.png") ?# ENEMY_IMGS
# TODO 定義一個(gè)公共類
class Model:
? ? window = None ?# 主窗體window對象
? ? # 初始化函數(shù)
? ? def __init__(self, img_path, x, y):
? ? ? ? self.img = pygame.image.load(img_path)
? ? ? ? self.x = x
? ? ? ? self.y = y
? ? # 將圖片填充到背景中
? ? def display(self):
? ? ? ? Model.window.blit(self.img, (self.x, self.y))
# TODO 背景
class Background(Model):
? ? # 背景移動
? ? def move(self):
? ? ? ? if self.y <= Game.WINDOW_HEIGHT:
? ? ? ? ? ? self.y += 1
? ? ? ? else:
? ? ? ? ? ? self.y = 0
? ? # 背景圖片展示
? ? def display(self):
? ? ? ? Model.window.blit(self.img, (self.x, self.y)) ?# 填充背景
? ? ? ? Model.window.blit(self.img, (self.x, self.y - Game.WINDOW_HEIGHT)) ?# 填充輔助背景
# TODO 玩家類
class PlayerPlane(Model):
? ? def __init__(self, img_path, x, y):
? ? ? ? super().__init__(img_path, x, y)
? ? ? ? # 子彈
? ? ? ? self.bullets = []
? ? def display(self, enemys):
? ? ? ? super().display()
? ? ? ? remove_bullets = []
? ? ? ? for bullet in self.bullets:
? ? ? ? ? ? bullet.move()
? ? ? ? ? ? bullet.display()
? ? ? ? ? ? # 如果子彈小于消失于屏幕,刪除子彈
? ? ? ? ? ? if bullet.y < -11:
? ? ? ? ? ? ? ? remove_bullets.append(bullet)
? ? ? ? ? ? # 子彈的矩陣
? ? ? ? ? ? bullet_rect = pygame.locals.Rect(bullet.x, bullet.y, 5, 11)
? ? ? ? ? ? # TODO 進(jìn)行碰撞檢測
? ? ? ? ? ? for enemy in enemys:
? ? ? ? ? ? ? ? # 敵機(jī)的矩陣
? ? ? ? ? ? ? ? enemy_rect = pygame.locals.Rect(enemy.x, enemy.y, 57, 43)
? ? ? ? ? ? ? ? # 如果碰撞
? ? ? ? ? ? ? ? if pygame.Rect.colliderect(bullet_rect, enemy_rect):
? ? ? ? ? ? ? ? ? ? # 隨機(jī)修改敵機(jī)的位置模擬敵機(jī)銷毀并生成新敵機(jī)并刪除子彈
? ? ? ? ? ? ? ? ? ? enemy.img = pygame.image.load(Enemy_IMGS[random.randint(0, 1)])
? ? ? ? ? ? ? ? ? ? enemy.x = random.randint(0, Game.WINDOW_WIDTH - 57)
? ? ? ? ? ? ? ? ? ? enemy.y = random.randint(-Game.WINDOW_WIDTH, -43)
? ? ? ? ? ? ? ? ? ? remove_bullets.append(bullet)
? ? ? ? ? ? ? ? ? ? break
? ? ? ? for bullet in remove_bullets:
? ? ? ? ? ? self.bullets.remove(bullet)
# TODO 敵機(jī)類
class EnemyPlane(Model):
? ? def __init__(self):
? ? ? ? self.img = pygame.image.load(Enemy_IMGS[random.randint(0, 1)])
? ? ? ? self.x = random.randint(0, Game.WINDOW_WIDTH - 57)
? ? ? ? self.y = random.randint(-Game.WINDOW_WIDTH, -43)
? ? def move(self):
? ? ? ? if self.y > Game.WINDOW_HEIGHT:
? ? ? ? ? ? self.y = -43 ?# 返回初始位置
? ? ? ? else:
? ? ? ? ? ? self.y += 1
# TODO 子彈類
class Bullet(Model):
? ? def move(self):
? ? ? ? self.y -= 1
# TODO 游戲類
class Game:
? ? WINDOW_WIDTH = 410
? ? WINDOW_HEIGHT = 614
? ? # 初始化操作
? ? def __init__(self):
? ? ? ? self.window = pygame.display.set_mode([Game.WINDOW_WIDTH, Game.WINDOW_HEIGHT]) ?# 窗口的設(shè)置
? ? ? ? self.background = Background("images/background.png", 0, 0) ?# 窗體模型的設(shè)置
? ? ? ? self.player = PlayerPlane("images/me1.png", 180, 400)
? ? ? ? self.enemyPlanes = []
? ? ? ? for _ in range(5):
? ? ? ? ? ? self.enemyPlanes.append(EnemyPlane())
? ? # 游戲的入口函數(shù)
? ? def run(self):
? ? ? ? self.frame_init()
? ? ? ? while True:
? ? ? ? ? ? self.background.move() ?# 背景向上移動,模擬環(huán)境移動
? ? ? ? ? ? self.background.display() ?# 將圖片塞入到窗體中
? ? ? ? ? ? self.player.display(self.enemyPlanes)
? ? ? ? ? ? for enemy in self.enemyPlanes:
? ? ? ? ? ? ? ? enemy.move()
? ? ? ? ? ? ? ? enemy.display()
? ? ? ? ? ? pygame.display.update() ?# 刷新窗體
? ? ? ? ? ? self.event_init() ?# 監(jiān)聽事件
? ? # 初始化窗口
? ? def frame_init(self):
? ? ? ? Model.window = self.window ?# 將window對象賦值給Model公有類
? ? ? ? ico = pygame.image.load(ICO_PATH) ?# 加載圖片
? ? ? ? pygame.display.set_icon(ico) ?# 設(shè)置icon
? ? ? ? pygame.display.set_caption(APP_NAME) ?# 設(shè)置app_name
? ? # 事件初始化方法
? ? def event_init(self):
? ? ? ? for event in pygame.event.get():
? ? ? ? ? ? # 關(guān)閉事件
? ? ? ? ? ? if event.type == pygame.locals.QUIT:
? ? ? ? ? ? ? ? sys.exit()
? ? ? ? ? ? # 監(jiān)聽鼠標(biāo)事件
? ? ? ? ? ? if event.type == pygame.locals.MOUSEMOTION:
? ? ? ? ? ? ? ? pos = pygame.mouse.get_pos()
? ? ? ? ? ? ? ? self.player.x = pos[0] - 51
? ? ? ? ? ? ? ? self.player.y = pos[1] - 63
? ? ? ? focus_state = pygame.mouse.get_pressed()
? ? ? ? if focus_state[0] == 1:
? ? ? ? ? ? pos = pygame.mouse.get_pos()
? ? ? ? ? ? self.player.bullets.append(Bullet("images/bullet1.png", pos[0], pos[1] - 75))
if __name__ == "__main__":
? ? Game().run()以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)的Excel文件讀寫類
這篇文章主要介紹了Python實(shí)現(xiàn)的Excel文件讀寫類,涉及Python針對Excel常見的讀寫、打印等操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Python 新建文件夾與復(fù)制文件夾內(nèi)所有內(nèi)容的方法
今天小編就為大家分享一篇Python 新建文件夾與復(fù)制文件夾內(nèi)所有內(nèi)容的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
淺談python數(shù)據(jù)類型及類型轉(zhuǎn)換
這篇文章主要介紹了淺談python數(shù)據(jù)類型及類型轉(zhuǎn)換,介紹了python中的數(shù)據(jù)類型,以及數(shù)據(jù)的不可變性,還有字符串,列表等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
使用Python腳本對GiteePages進(jìn)行一鍵部署的使用說明
剛好之前有了解過python的自動化,就想著自動化腳本,百度一搜還真有類似的文章。今天就給大家分享下使用Python腳本對GiteePages進(jìn)行一鍵部署的使用說明,感興趣的朋友一起看看吧2021-05-05
Python中關(guān)于字典的常規(guī)操作范例以及介紹
今天小編幫大家簡單介紹下Python的一種數(shù)據(jù)結(jié)構(gòu): 字典,字典是 Python 提供的一種常用的數(shù)據(jù)結(jié)構(gòu),它用于存放具有映射關(guān)系的數(shù)據(jù),通讀本篇對大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-09-09
Python報(bào)錯(cuò):PermissionError:?[Errno?13]?Permission?denied的解
這篇文章主要給大家介紹了關(guān)于Python報(bào)錯(cuò):PermissionError:?[Errno?13]?Permission?denied的解決辦法,文中給出了詳細(xì)的解決辦法,需要的朋友可以參考下2022-02-02
pandas中關(guān)于apply+lambda的應(yīng)用
本文主要介紹了pandas中關(guān)于apply+lambda的應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

