欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)簡(jiǎn)單飛機(jī)大戰(zhàn)小游戲

 更新時(shí)間:2022年05月08日 13:10:08   作者:HuCheng1997  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單飛機(jī)大戰(zhàn)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

實(shí)現(xiàn)步驟:

①下載64位對(duì)應(yīng)python版本的pygamepygame-1.9.6-cp38-cp38-win_amd64.whl

② 更新pippython -m pip install --upgrade pip

③ 安裝pygamepip 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è)公共類(lèi)
class Model:
? ? window = None ?# 主窗體window對(duì)象

? ? # 初始化函數(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):
? ? # 背景移動(dòng)
? ? 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 玩家類(lèi)
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)行碰撞檢測(cè)
? ? ? ? ? ? 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ī)銷(xiāo)毀并生成新敵機(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ī)類(lèi)
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 子彈類(lèi)
class Bullet(Model):
? ? def move(self):
? ? ? ? self.y -= 1


# TODO 游戲類(lèi)
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() ?# 背景向上移動(dòng),模擬環(huán)境移動(dòng)
? ? ? ? ? ? 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)聽(tīng)事件

? ? # 初始化窗口
? ? def frame_init(self):
? ? ? ? Model.window = self.window ?# 將window對(duì)象賦值給Model公有類(lèi)
? ? ? ? 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)聽(tīng)鼠標(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()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python基于win32ui模塊創(chuàng)建彈出式菜單示例

    Python基于win32ui模塊創(chuàng)建彈出式菜單示例

    這篇文章主要介紹了Python基于win32ui模塊創(chuàng)建彈出式菜單,結(jié)合實(shí)例形式分析了Python使用win32ui模塊創(chuàng)建彈出式菜單的具體步驟與相關(guān)操作技巧,并附帶說(shuō)明了win32ui模塊的安裝命令,需要的朋友可以參考下
    2018-05-05
  • 詳解python使用pip安裝第三方庫(kù)(工具包)速度慢、超時(shí)、失敗的解決方案

    詳解python使用pip安裝第三方庫(kù)(工具包)速度慢、超時(shí)、失敗的解決方案

    這篇文章主要介紹了詳解python使用pip安裝第三方庫(kù)(工具包)速度慢、超時(shí)、失敗的解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Python 二進(jìn)制字節(jié)流數(shù)據(jù)的讀取操作(bytes與bitstring)

    Python 二進(jìn)制字節(jié)流數(shù)據(jù)的讀取操作(bytes與bitstring)

    本文主要介紹了Python 二進(jìn)制字節(jié)流數(shù)據(jù)的讀取操作(bytes與bitstring),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python與人工神經(jīng)網(wǎng)絡(luò):使用神經(jīng)網(wǎng)絡(luò)識(shí)別手寫(xiě)圖像介紹

    Python與人工神經(jīng)網(wǎng)絡(luò):使用神經(jīng)網(wǎng)絡(luò)識(shí)別手寫(xiě)圖像介紹

    文章主要介紹什么是神經(jīng)網(wǎng)絡(luò),感知機(jī)的概念和模型,以及新一代的S曲線神經(jīng)元系統(tǒng)等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Python爬蟲(chóng)基礎(chǔ)之XPath語(yǔ)法與lxml庫(kù)的用法詳解

    Python爬蟲(chóng)基礎(chǔ)之XPath語(yǔ)法與lxml庫(kù)的用法詳解

    這篇文章主要給大家介紹了關(guān)于Python爬蟲(chóng)基礎(chǔ)之XPath語(yǔ)法與lxml庫(kù)用法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • python實(shí)現(xiàn)門(mén)限回歸方式

    python實(shí)現(xiàn)門(mén)限回歸方式

    今天小編就為大家分享一篇python實(shí)現(xiàn)門(mén)限回歸方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • Python 無(wú)限級(jí)分類(lèi)樹(shù)狀結(jié)構(gòu)生成算法的實(shí)現(xiàn)

    Python 無(wú)限級(jí)分類(lèi)樹(shù)狀結(jié)構(gòu)生成算法的實(shí)現(xiàn)

    這篇文章主要介紹了Python 無(wú)限級(jí)分類(lèi)樹(shù)狀結(jié)構(gòu)生成算法的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • python實(shí)現(xiàn)學(xué)員管理系統(tǒng)

    python實(shí)現(xiàn)學(xué)員管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)學(xué)員管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • YOLOV5代碼詳解之損失函數(shù)的計(jì)算

    YOLOV5代碼詳解之損失函數(shù)的計(jì)算

    YOLOV4出現(xiàn)之后不久,YOLOv5橫空出世,YOLOv5在YOLOv4算法的基礎(chǔ)上做了進(jìn)一步的改進(jìn),檢測(cè)性能得到進(jìn)一步的提升,這篇文章主要給大家介紹了關(guān)于YOLOV5代碼詳解之損失函數(shù)計(jì)算的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • 如何用python合并多個(gè)有規(guī)則命名的nc文件

    如何用python合并多個(gè)有規(guī)則命名的nc文件

    在地學(xué)領(lǐng)域,nc格式的文件可謂隨處可見(jiàn),這種文件可以存儲(chǔ)多維數(shù)字矩陣,下面這篇文章主要給大家介紹了關(guān)于如何用python合并多個(gè)有規(guī)則命名的nc文件的相關(guān)資料,需要的朋友可以參考下
    2022-03-03

最新評(píng)論