python實現(xiàn)飛機大戰(zhàn)(面向過程)
更新時間:2022年05月07日 17:05:17 作者:QLUGCL
這篇文章主要為大家詳細介紹了python面向過程實現(xiàn)飛機大戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python實現(xiàn)飛機大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下
游戲的實現(xiàn)本質(zhì)是多個圖片的快速切換,類似動畫一樣,達到動態(tài)的效果。
比如子彈的發(fā)射,實際上是一個子彈的照片根據(jù)列表中存儲的位置多次粘貼到界面上。
還有飛機的移動是首先收集到移動信息將坐標(biāo)變化,然后下一次覆蓋頁面的時候進行粘貼。

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"
# 飛機爆炸
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))
? ? ? ? # 事件捕獲,將捕獲的事件放在列表中
? ? ? ? # 快速運行然后接受命令造成連續(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")
? ? # 加載飛機圖片
? ? hero = pygame.image.load("./feiji/hero1.png")
? ? # 加載子彈照片
? ? bullet = pygame.image.load("./feiji/plane.png")
? ? # 加載導(dǎo)彈
? ? bomb = pygame.image.load("./feiji/bomb-1.gif")
? ? # 加載敵人飛機照片
? ? enemy = pygame.image.load("./feiji/enemy1.png")
? ? # 圖片添加到屏幕
? ? # blit剪切,粘貼
? ? # screen 類似指針的使用帶動目的地址的數(shù)據(jù)改動
? ? while True:
? ? ? ? screen.blit(background, (0, 0))
? ? ? ? # 顯示英雄飛機
? ? ? ? hero_plant(screen, hero, bullet)
? ? ? ? # 顯示敵人飛機
? ? ? ? enemy_plant(screen, enemy, bomb)
? ? ? ? # 數(shù)據(jù)更新加載出來
? ? ? ? pygame.display.update()
? ? ? ? # 圖片多顯示一會
? ? ? ? time.sleep(0.1)
main()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Python實現(xiàn)URL監(jiān)測與即時推送
這篇文章主要為大家介紹了Python實現(xiàn)URL監(jiān)測與即時推送,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11
OpenCV(python)版實現(xiàn)文本分割之水平投影法
本文主要介紹了OpenCV(python)版實現(xiàn)文本分割之水平投影法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
在python3中使用shuffle函數(shù)要注意的地方
今天小編就為大家分享一篇在python3中使用shuffle函數(shù)要注意的地方,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02

