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

python實(shí)現(xiàn)飛機(jī)大戰(zhàn)(面向過程)

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

本文實(shí)例為大家分享了python實(shí)現(xiàn)飛機(jī)大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下

游戲的實(shí)現(xiàn)本質(zhì)是多個圖片的快速切換,類似動畫一樣,達(dá)到動態(tài)的效果。
比如子彈的發(fā)射,實(shí)際上是一個子彈的照片根據(jù)列表中存儲的位置多次粘貼到界面上。
還有飛機(jī)的移動是首先收集到移動信息將坐標(biāo)變化,然后下一次覆蓋頁面的時候進(jìn)行粘貼。

import pygame
import time
from pygame.locals import *

hero_x = 150
hero_y = 600
# 子彈夾
mybullet = []
# 導(dǎo)彈夾
bomb_list = []
enemy_x = 0
enemy_y = 0
flag = 0
enemy_life = "live"
hero_life = "live"
# 飛機(jī)爆炸
a = pygame.image.load("./feiji/enemy1_down1.png")
b = pygame.image.load("./feiji/enemy1_down2.png")
c = pygame.image.load("./feiji/enemy1_down3.png")
d = pygame.image.load("./feiji/enemy1_down4.png")
e = pygame.image.load("./feiji/enemy1_hit.png")
a1 = pygame.image.load("./feiji/hero_blowup_n1.png")
b1 = pygame.image.load("./feiji/hero_blowup_n2.png")
c1 = pygame.image.load("./feiji/hero_blowup_n3.png")
d1 = pygame.image.load("./feiji/hero_blowup_n4.png")
hero_explode = [a1, b1, c1, d1]
explode = [a, b, c, d, e]
num = 0
hnum = 0
count = 0


def enemy_plant(screen, enemy, bomb):
? ? global enemy_x
? ? global enemy_y
? ? global flag
? ? global num
? ? global count
? ? global bomb_list
? ? global enemy_life
? ? if enemy_life == "live":
? ? ? ? if flag == 1 and enemy_x >= 0:
? ? ? ? ? ? enemy_x -= 20
? ? ? ? else:
? ? ? ? ? ? flag = 0
? ? ? ? if flag == 0 and enemy_x <= 320:
? ? ? ? ? ? enemy_x += 20
? ? ? ? else:
? ? ? ? ? ? flag = 1

? ? ? ? screen.blit(enemy, (enemy_x, enemy_y))
? ? elif enemy_life == "death":
? ? ? ? screen.blit(explode[num], (enemy_x, enemy_y))
? ? ? ? bomb_list.clear()
? ? ? ? num += 1
? ? ? ? if num == 4:
? ? ? ? ? ? enemy_life = "live"
? ? ? ? ? ? num = 0
? ? if count % 10 == 0:
? ? ? ? bomb_list.append({"x": enemy_x + 30, "y": enemy_y + 20})
? ? count += 1
? ? for b in bomb_list:
? ? ? ? screen.blit(bomb, (b["x"], b["y"]))
? ? ? ? b["y"] += 10


def hero_plant(screen, hero, bullet):
? ? global hero_x
? ? global hero_y
? ? global enemy_life
? ? global hero_explode
? ? global enemy_x
? ? global enemy_y
? ? global hnum
? ? global bomb_list
? ? global hero_life
? ? for b in bomb_list:
? ? ?? ?# 注意區(qū)間的取值
? ? ? ? if b["x"] <= hero_x + 30 and b["x"] >= hero_x and b["y"] >= hero_y and b["y"] <= hero_y + 30:
? ? ? ? ? ? hero_life = "death"
? ? ? ? ? ? break
? ? if hero_life == "death":
? ? ? ? mybullet.clear()
? ? ? ? screen.blit(hero_explode[hnum], (hero_x, hero_y))
? ? ? ? hnum += 1
? ? ? ? if hnum == 4:
? ? ? ? ? ? hnum = 0
? ? ? ? ? ? hero_life = "live"
? ? if hero_life == "live":
? ? ? ? screen.blit(hero, (hero_x, hero_y))
? ? ? ? # 事件捕獲,將捕獲的事件放在列表中
? ? ? ? # 快速運(yùn)行然后接受命令造成連續(xù)性的畫面,有的時候可能為空。
? ? ? ? for event in pygame.event.get():
? ? ? ? ? ? # 關(guān)閉游戲
? ? ? ? ? ? if event.type == pygame.QUIT:
? ? ? ? ? ? ? ? # 退出游戲
? ? ? ? ? ? ? ? exit()
? ? ? ? ? ? elif event.type == pygame.KEYDOWN:
? ? ? ? ? ? ? ? if event.key == pygame.K_RIGHT:
? ? ? ? ? ? ? ? ? ? hero_x += 10
? ? ? ? ? ? ? ? elif event.key == pygame.K_LEFT:
? ? ? ? ? ? ? ? ? ? hero_x -= 10
? ? ? ? ? ? ? ? elif event.key == pygame.K_DOWN:
? ? ? ? ? ? ? ? ? ? hero_y += 10
? ? ? ? ? ? ? ? elif event.key == pygame.K_UP:
? ? ? ? ? ? ? ? ? ? hero_y -= 10
? ? ? ? ? ? ? ? elif event.key == pygame.K_SPACE:
? ? ? ? ? ? ? ? ? ? print("發(fā)射子彈")
? ? ? ? ? ? ? ? ? ? mybullet.append({"x": hero_x + 30, "y": hero_y - 20})
? ? ? ? for i in mybullet:
? ? ? ? ? ? # 注意出界的子彈所以要大于0
? ? ? ? ? ? if i["x"] <= enemy_x + 20 and i["x"] >= enemy_x and i["y"] <= 20 and i["y"] >= 0:
? ? ? ? ? ? ? ? enemy_life = "death"
? ? ? ? ? ? screen.blit(bullet, (i["x"], i["y"]))
? ? ? ? ? ? # 散彈模式
? ? ? ? ? ? # screen.blit(bullet, (i["x"]-20, i["y"]))
? ? ? ? ? ? # screen.blit(bullet, (i["x"]+20, i["y"]))
? ? ? ? ? ? # 這樣就可以自動控制上升和時間間隔了
? ? ? ? ? ? i["y"] -= 10


def main():
? ? '''流程控制'''
? ? # 1 創(chuàng)建一個游戲窗口
? ? # display方法:展示相關(guān)的都會用到這個方法
? ? # 參數(shù)1:元組(長,高)像素
? ? # 參數(shù)2:有無特殊功能
? ? # 參數(shù)3:像素深度
? ? screen = pygame.display.set_mode((400, 800), 0, 32)
? ? # 加載背景圖片
? ? background = pygame.image.load("./feiji/background.png")
? ? # 加載飛機(jī)圖片
? ? hero = pygame.image.load("./feiji/hero1.png")
? ? # 加載子彈照片
? ? bullet = pygame.image.load("./feiji/plane.png")
? ? # 加載導(dǎo)彈
? ? bomb = pygame.image.load("./feiji/bomb-1.gif")
? ? # 加載敵人飛機(jī)照片
? ? enemy = pygame.image.load("./feiji/enemy1.png")
? ? # 圖片添加到屏幕
? ? # blit剪切,粘貼
? ? # screen 類似指針的使用帶動目的地址的數(shù)據(jù)改動
? ? while True:
? ? ? ? screen.blit(background, (0, 0))
? ? ? ? # 顯示英雄飛機(jī)
? ? ? ? hero_plant(screen, hero, bullet)
? ? ? ? # 顯示敵人飛機(jī)
? ? ? ? enemy_plant(screen, enemy, bomb)
? ? ? ? # 數(shù)據(jù)更新加載出來
? ? ? ? pygame.display.update()
? ? ? ? # 圖片多顯示一會
? ? ? ? time.sleep(0.1)

main()

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

相關(guān)文章

  • Python字符串刪除指定字符的三個方法

    Python字符串刪除指定字符的三個方法

    這篇文章主要給大家介紹了關(guān)于Python字符串刪除指定字符的三個方法,我們在使用 Python處理字符串的時候,經(jīng)常會遇到一些字符串中出現(xiàn)了指定字符,需要的朋友可以參考下
    2023-07-07
  • 用Python搶火車票的簡單小程序?qū)崿F(xiàn)解析

    用Python搶火車票的簡單小程序?qū)崿F(xiàn)解析

    這篇文章主要介紹了用Python搶火車票的簡單小程序?qū)崿F(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • python創(chuàng)建模板文件及使用教程示例

    python創(chuàng)建模板文件及使用教程示例

    這篇文章主要介紹了python創(chuàng)建模板文件及使用教程示例
    2021-10-10
  • django框架如何集成celery進(jìn)行開發(fā)

    django框架如何集成celery進(jìn)行開發(fā)

    本文給大家詳細(xì)講解了在django框架中如何集成celery進(jìn)行開發(fā),步驟非常詳細(xì),有需要的小伙伴可以參考下
    2017-05-05
  • 提升Python效率之使用循環(huán)機(jī)制代替遞歸函數(shù)

    提升Python效率之使用循環(huán)機(jī)制代替遞歸函數(shù)

    這篇文章主要介紹了提升Python效率之使用循環(huán)機(jī)制代替遞歸函數(shù)的相關(guān)知識,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-07-07
  • python中實(shí)現(xiàn)延時回調(diào)普通函數(shù)示例代碼

    python中實(shí)現(xiàn)延時回調(diào)普通函數(shù)示例代碼

    這篇文章主要給大家介紹了關(guān)于python中實(shí)現(xiàn)延時回調(diào)普通函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • Python虛擬環(huán)境的原理及使用詳解

    Python虛擬環(huán)境的原理及使用詳解

    這篇文章主要介紹了Python虛擬環(huán)境的原理及使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 淺析Python中嵌套字典的訪問與操作

    淺析Python中嵌套字典的訪問與操作

    在Python編程中,嵌套字典是一種常見的數(shù)據(jù)結(jié)構(gòu),它可以以層次結(jié)構(gòu)的方式組織和存儲數(shù)據(jù),本文將詳細(xì)介紹如何在Python中訪問和操作嵌套字典,需要的可以參考下
    2024-02-02
  • python中count函數(shù)簡單的實(shí)例講解

    python中count函數(shù)簡單的實(shí)例講解

    在本篇文章里小編給大家整理了關(guān)于python中count函數(shù)簡單的實(shí)例講解,有興趣的朋友們可以參考下。
    2020-02-02
  • python調(diào)用pyaudio使用麥克風(fēng)錄制wav聲音文件的教程

    python調(diào)用pyaudio使用麥克風(fēng)錄制wav聲音文件的教程

    這篇文章主要介紹了python調(diào)用pyaudio使用麥克風(fēng)錄制wav聲音文件的教程,詳細(xì)的給大家介紹了pyaudio庫的安裝與使用,需要的朋友可以參考下
    2019-06-06

最新評論