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

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

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

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

游戲的實(shí)現(xiàn)本質(zhì)是多個(gè)圖片的快速切換,類似動(dòng)畫一樣,達(dá)到動(dòng)態(tài)的效果。
比如子彈的發(fā)射,實(shí)際上是一個(gè)子彈的照片根據(jù)列表中存儲(chǔ)的位置多次粘貼到界面上。
還有飛機(jī)的移動(dòng)是首先收集到移動(dòng)信息將坐標(biāo)變化,然后下一次覆蓋頁(yè)面的時(shí)候進(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ù)性的畫面,有的時(shí)候可能為空。
? ? ? ? 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"]))
? ? ? ? ? ? # 這樣就可以自動(dòng)控制上升和時(shí)間間隔了
? ? ? ? ? ? i["y"] -= 10


def main():
? ? '''流程控制'''
? ? # 1 創(chuàng)建一個(gè)游戲窗口
? ? # display方法:展示相關(guān)的都會(huì)用到這個(gè)方法
? ? # 參數(shù)1:元組(長(zhǎng),高)像素
? ? # 參數(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 類似指針的使用帶動(dòng)目的地址的數(shù)據(jù)改動(dòng)
? ? while True:
? ? ? ? screen.blit(background, (0, 0))
? ? ? ? # 顯示英雄飛機(jī)
? ? ? ? hero_plant(screen, hero, bullet)
? ? ? ? # 顯示敵人飛機(jī)
? ? ? ? enemy_plant(screen, enemy, bomb)
? ? ? ? # 數(shù)據(jù)更新加載出來
? ? ? ? pygame.display.update()
? ? ? ? # 圖片多顯示一會(huì)
? ? ? ? time.sleep(0.1)

main()

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

相關(guān)文章

最新評(píng)論