最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)簡單飛機大戰(zhàn)小游戲

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

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

實現(xiàn)步驟:

①下載64位對應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

④ 實現(xiàn)代碼如下:

import sys ?# 導入系統(tǒng)模塊
import random ?# 隨機數(shù)模塊
import pygame ?# 導入pygame模塊
import pygame.locals ?# 導入pygame本地策略

ICO_PATH = "images/app.ico" ?# APP_ICO
APP_NAME = "飛機大戰(zhàn)V1.0" ?# APP_NAME
Enemy_IMGS = ("images/enemy1.png", "images/enemy2.png") ?# ENEMY_IMGS


# TODO 定義一個公共類
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 進行碰撞檢測
? ? ? ? ? ? for enemy in enemys:
? ? ? ? ? ? ? ? # 敵機的矩陣
? ? ? ? ? ? ? ? enemy_rect = pygame.locals.Rect(enemy.x, enemy.y, 57, 43)
? ? ? ? ? ? ? ? # 如果碰撞
? ? ? ? ? ? ? ? if pygame.Rect.colliderect(bullet_rect, enemy_rect):
? ? ? ? ? ? ? ? ? ? # 隨機修改敵機的位置模擬敵機銷毀并生成新敵機并刪除子彈
? ? ? ? ? ? ? ? ? ? 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 敵機類
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]) ?# 窗口的設置
? ? ? ? self.background = Background("images/background.png", 0, 0) ?# 窗體模型的設置

? ? ? ? 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) ?# 設置icon
? ? ? ? pygame.display.set_caption(APP_NAME) ?# 設置app_name

? ? # 事件初始化方法
? ? def event_init(self):
? ? ? ? for event in pygame.event.get():
? ? ? ? ? ? # 關(guān)閉事件
? ? ? ? ? ? if event.type == pygame.locals.QUIT:
? ? ? ? ? ? ? ? sys.exit()
? ? ? ? ? ? # 監(jiān)聽鼠標事件
? ? ? ? ? ? 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決keras backend 越跑越慢問題

    解決keras backend 越跑越慢問題

    這篇文章主要介紹了解決keras backend 越跑越慢問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • tensorflow常用函數(shù)API介紹

    tensorflow常用函數(shù)API介紹

    這篇文章主要介紹了tensorflow常用函數(shù)API介紹,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • Linux系統(tǒng)下升級pip的完整步驟

    Linux系統(tǒng)下升級pip的完整步驟

    這篇文章主要給大家介紹了關(guān)于Linux系統(tǒng)下升級pip的完整步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • Python實現(xiàn)的Excel文件讀寫類

    Python實現(xiàn)的Excel文件讀寫類

    這篇文章主要介紹了Python實現(xiàn)的Excel文件讀寫類,涉及Python針對Excel常見的讀寫、打印等操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • Python 新建文件夾與復制文件夾內(nèi)所有內(nèi)容的方法

    Python 新建文件夾與復制文件夾內(nèi)所有內(nèi)容的方法

    今天小編就為大家分享一篇Python 新建文件夾與復制文件夾內(nèi)所有內(nèi)容的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 淺談python數(shù)據(jù)類型及類型轉(zhuǎn)換

    淺談python數(shù)據(jù)類型及類型轉(zhuǎn)換

    這篇文章主要介紹了淺談python數(shù)據(jù)類型及類型轉(zhuǎn)換,介紹了python中的數(shù)據(jù)類型,以及數(shù)據(jù)的不可變性,還有字符串,列表等相關(guān)內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • 使用Python腳本對GiteePages進行一鍵部署的使用說明

    使用Python腳本對GiteePages進行一鍵部署的使用說明

    剛好之前有了解過python的自動化,就想著自動化腳本,百度一搜還真有類似的文章。今天就給大家分享下使用Python腳本對GiteePages進行一鍵部署的使用說明,感興趣的朋友一起看看吧
    2021-05-05
  • Python中關(guān)于字典的常規(guī)操作范例以及介紹

    Python中關(guān)于字典的常規(guī)操作范例以及介紹

    今天小編幫大家簡單介紹下Python的一種數(shù)據(jù)結(jié)構(gòu): 字典,字典是 Python 提供的一種常用的數(shù)據(jù)結(jié)構(gòu),它用于存放具有映射關(guān)系的數(shù)據(jù),通讀本篇對大家的學習或工作具有一定的價值,需要的朋友可以參考下
    2021-09-09
  • Python報錯:PermissionError:?[Errno?13]?Permission?denied的解決辦法

    Python報錯:PermissionError:?[Errno?13]?Permission?denied的解

    這篇文章主要給大家介紹了關(guān)于Python報錯:PermissionError:?[Errno?13]?Permission?denied的解決辦法,文中給出了詳細的解決辦法,需要的朋友可以參考下
    2022-02-02
  • pandas中關(guān)于apply+lambda的應用

    pandas中關(guān)于apply+lambda的應用

    本文主要介紹了pandas中關(guān)于apply+lambda的應用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02

最新評論

平泉县| 万盛区| 秦皇岛市| 绥德县| 波密县| 宝应县| 濉溪县| 都江堰市| 永修县| 嘉鱼县| 马山县| 石棉县| 云和县| 罗城| 凤阳县| 衡阳县| 开鲁县| 昌邑市| 和林格尔县| 安平县| 茂名市| 寿阳县| 汝阳县| 遂溪县| 贺州市| 南投市| 苍南县| 贵州省| 剑河县| 美姑县| 江达县| 安图县| 盖州市| 台中市| 沿河| 全南县| 曲阜市| 内黄县| 北流市| 临武县| 边坝县|