w'cwc下面我們進入詳細教程:
?文章來源地址http://www.zghlxwxcb.cn/news/detail-485275.html
一、首先我們先建一個文件夾 planewars(名字隨便取):
?然后用我們python中的pycharm打開這個文件,我們飛機大戰(zhàn)的項目就在這進行
二、我們要寫這個小游戲要用到pygame模
? ? ? ? 補充: Pygame是一個利用SDL庫的寫就的游戲庫,Pygame就是Python中使用它的一個庫。
?1.安裝pygame模塊
其安裝命令如下:pip install pygame
我們直接在pycharm中打開 命令行(終端) 下載(我已下載)
?也可以用(cmd)來下載
?完成后嘗試使用pygame模塊
1、??? ?首先新建一個py文件pygame體驗.py
?
??? ?然后編輯如下代碼
?
import pygame
pygame.init() ?# 初始化
print("游戲代碼....")
pygame.quit() # 卸載模塊,釋放資源
如下:?可以點擊我標記的那個網(wǎng)址看下pygame
?三、補充知識(使用rect 描述飛機大戰(zhàn)英雄機的位置)
????????1、Rect是用于存儲矩形坐標的pygame對象,rect對象有一些虛擬屬性,比如top,left,bottom,right這些是用來固定矩形的位置的,還有size,width,height,這些是描述矩形大小,寬高分別是多大。
center為矩形的中心點,其實就是關于橫縱坐標的二元組,因此又有centerx,centery兩個屬性。此外,還有x,y。
Rect構造方法:
rect = pygame.Rect( left , top, width, height )
? ? ? ? 2、下面創(chuàng)建plane_rect.py文件演示
import pygame
pygame.init()
hero_rect = pygame.Rect(100, 200, 125, 300)
# 100: 表示距離x軸原點的位置
# 200: 表示距離y軸原點的位置
# 125:矩形的寬度
# 300:矩形的長度
print("英雄機矩形的x={},y={}".format(hero_rect.x, hero_rect.y))
print("英雄機矩形的寬width = {},高height = {}".format(hero_rect.width, hero_rect.height))
print("英雄機矩形的中心 centerx={}".format(hero_rect.centerx))
print("英雄機的低部 bottom={}".format(hero_rect.bottom))
print("英雄機的左部 left = {}".format(hero_rect.left))
print("英雄機的右部right = {}".format(hero_rect.right))
print("英雄機大小".format(hero_rect.size))
pygame.quit()
?3、下面就來實現(xiàn)創(chuàng)建游戲窗口和游戲循環(huán)
import pygame
pygame.init()
# 1.創(chuàng)建游戲窗口
pygame.display.set_mode((480,650)) # 元組中480表示寬度, 650表示高度
# 游戲循環(huán)
while True:
pass
pygame.quit()
我們寫的游戲就在模板中添加代碼
運行結果:
4、我們要用到的圖片(你可以直接網(wǎng)上找)
(將你用的圖片放在 img 里 )
四、下面是我們飛機大戰(zhàn)的源碼(有注釋自己看)
import random
import pygame
import time
class HeroBullet():
"""
英雄精靈子彈的類
"""
def __init__(self, x, y, screen):
"""
:param x: x坐標
:param y: y 坐標
:param screen: 窗口對象
"""
self.x = x
self.y = y
self.screen = screen
self.pic = pygame.image.load("img/bullet.png")#圖片是英雄機的子彈圖片
def draw(self):
"""用來畫子彈"""
self.screen.blit(self.pic, (self.x, self.y))
self.move()
def move(self):
self.y -= 5
class EnemyBullet():
"""敵機精靈子彈類"""
def __init__(self, x, y, screen):
self.x = x
self.y = y
self.screen = screen
self.pic = pygame.image.load("img/bullet1.png")#圖片是敵機的子彈圖片
def draw(self):
"""用來畫子彈"""
self.screen.blit(self.pic, (self.x, self.y))
self.move()
def move(self):
self.y += 5
pygame.init() # 游戲初始化
# 使用變量screen 接收返回值,代表整個窗口對象
screen = pygame.display.set_mode((480, 650)) # 元組中320表示寬度, 568表示高度
# 修改游戲名稱
pygame.display.set_caption("飛機大戰(zhàn)")
# 修改游戲圖標
icon = pygame.image.load("img/icon72x72.png")#英雄機的圖片
pygame.display.set_icon(icon)
# 加載背景圖片
bg_img = pygame.image.load("img/background.png")#游戲背景圖
# 加載英雄飛機
hero_img1 = pygame.image.load("img/hero1.png")#英雄機尾氣加速的圖片
hero_img2 = pygame.image.load("img/hero2.png")#英雄機尾氣的圖片
# 加載英雄爆炸圖片
hero_bomb_list = ["img/hero_blowup_n1.png", "img/hero_blowup_n2.png", "img/hero_blowup_n3.png","img/hero_blowup_n4.png"]#英雄機逐漸被擊毀的四張圖片
# 加載敵機精靈
enemy_img = pygame.image.load("img/enemy1.png")#敵機圖片
# 加載敵機爆炸圖片
enemy_bomb_list = ["img/enemy1_down1.png", "img/enemy1_down2.png", "img/enemy1_down3.png",
"img/enemy1_down4.png"]#敵機逐漸被擊毀的四張圖片
heroIndexShift = 0
# 定義英雄飛機的rect
hero_rect = pygame.rect.Rect(190, 526, 100, 124)
# 定義敵機精靈的rect
enemy_rect = pygame.rect.Rect(206, 0, 50, 56) # x= 480//2-69//2
# 敵機精靈的x,y軸坐標
enemyPlaneX = enemy_rect.x
enemyPlaneY = enemy_rect.y
direct = '左' # 定義敵機初始移動方向
# 創(chuàng)建游戲時鐘
clock = pygame.time.Clock()
# 英雄精靈的x,y軸坐標
heroPlaneX = hero_rect.x
heroPlaneY = hero_rect.y
pygame.key.set_repeat(20, 30) # 重復按鍵操作
# 存放英雄機子彈的列表
HeroBiulist = []
# 存放敵機子彈列表
EnemyBiulist = []
enemy_is_bomb = False # 敵機爆炸條件
enemy_bomb_index = 0 # 敵機爆炸圖片索引
hero_is_bomb = False # 英雄機爆炸條件
hero_bomb_index = 0 # 英雄機爆炸圖片索引
while True: # 游戲循環(huán) ->意味著游戲正式開始
clock.tick(60) # 60表示每秒鐘刷新60次
# 將背景圖片加載到窗口中,(0,0)表示背景圖片放到原點位置
screen.blit(bg_img, (0, 0))
# 修改英雄飛機的y軸值
hero_rect.y -= 1
# 讓英雄精靈飛機從底部飛進
if hero_rect.bottom <= 0:
hero_rect.y = 650
# 將英雄的飛機繪制到窗口上
if heroIndexShift == 0:
screen.blit(hero_img1, (heroPlaneX, heroPlaneY))
heroIndexShift += 1
else:
screen.blit(hero_img2, (heroPlaneX, heroPlaneY))
heroIndexShift = 0
# 獲取所有的事件
event_list = pygame.event.get()
# 捕獲窗口退出事件
for event in event_list:
if event.type == pygame.QUIT: # 加上這個模塊就不卡了
print("游戲結束了.......")
pygame.quit() # 卸載模塊
exit(0) # 終止Python程序, exit(0)表示正常退出 exit(1)表示異常退出
# 控制英雄精靈移動
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT: # 向左移動
heroPlaneX = heroPlaneX - 5 if heroPlaneX >= 5 else 0
elif event.key == pygame.K_RIGHT: # 向右移動
heroPlaneX = heroPlaneX + 5 if heroPlaneX <= 375 else 380
elif event.key == pygame.K_DOWN: # 向下移動
heroPlaneY = heroPlaneY + 5 if heroPlaneY <= 521 else 526
elif event.key == pygame.K_UP: # 向上移動
heroPlaneY = heroPlaneY - 5 if heroPlaneY >= 5 else 0
elif event.key == pygame.K_SPACE: # 英雄機控制發(fā)射子彈
hero_bullet = HeroBullet(heroPlaneX + 50 - 11, heroPlaneY - 22, screen)
HeroBiulist.append(hero_bullet)
# # 繪制敵機精靈
# screen.blit(enemy_img, (enemyPlaneX, enemyPlaneY))
# 控制敵機精靈移動
if direct == "左":
enemyPlaneX -= 5
if enemyPlaneX <= 0:
direct = "右"
elif direct == "右":
enemyPlaneX += 5
if enemyPlaneX >= 480 - 69:
direct = "左"
# 畫出英雄戰(zhàn)機的子彈每一個子彈
for bullet in HeroBiulist:
bullet.draw() # 繪制子彈
# 讓子彈到最上邊的時候消失
HeroBiulist.remove(bullet) if bullet.y < 0 else ""
hero_bullet_rect = pygame.rect.Rect(bullet.x, bullet.y, 10, 10) # 定義英雄子彈的rect
flag = hero_bullet_rect.colliderect(enemy_rect) # 檢測敵機和子彈的矩形是否相交
if flag:
print("敵機爆炸了......")
enemy_is_bomb = True # 爆炸條件為真
HeroBiulist.remove(bullet)
# 繪制敵機爆炸的圖片
if enemy_is_bomb == False:
# 如果沒有檢測到爆炸,就繪制沒有爆炸敵機的圖片
screen.blit(enemy_img, (enemyPlaneX, enemyPlaneY))
else: # 繪制敵機爆炸
if enemy_bomb_index == len(enemy_bomb_list): # 當敵機爆炸圖片的下表和圖片總數(shù)相同時,說明爆炸圖片已經(jīng)繪制結束
time.sleep(0.2)
exit(0) # 結束程序
enemy_bomb_img = pygame.image.load(enemy_bomb_list[enemy_bomb_index]) # 加載敵機爆炸圖片
screen.blit(enemy_bomb_img, (enemyPlaneX, enemyPlaneY)) # 繪制敵機爆炸圖片
enemy_bomb_index += 1
time.sleep(0.2)
# 畫出敵機子彈
# 產是生隨機數(shù)
x = random.randint(0, 100)
if x == 5 or x == 78:
# 實例化一個子彈
enemy_bullet = EnemyBullet(enemyPlaneX + 69 // 2 - 9 // 2, enemyPlaneY + 89, screen)
# 產生的每一個子彈放到一個列表里
EnemyBiulist.append(enemy_bullet)
for bullet in EnemyBiulist:
bullet.draw() # 繪制子彈
EnemyBiulist.remove(bullet) if bullet.y > 650 - 89 - 21 // 2 else "" # 讓子彈到最下面的時候消失
enemy_bullet_rect = pygame.rect.Rect(bullet.x, bullet.y, 9, 21) # 定義敵機子彈rect
flag = enemy_bullet_rect.colliderect(hero_rect) # 英雄機爆炸的條件
if flag:
print("英雄爆炸了.....")
hero_is_bomb = True # 爆炸條件為真
EnemyBiulist.remove(bullet)# 當戰(zhàn)機爆炸的時候,移除子彈
if hero_is_bomb == False:
# 將英雄的飛機繪制到窗口上
if heroIndexShift == 0:
screen.blit(hero_img1, (heroPlaneX, heroPlaneY))
heroIndexShift += 1
else:
screen.blit(hero_img2, (heroPlaneX, heroPlaneY))
heroIndexShift = 0
else:
if hero_bomb_index == len(hero_bomb_list):# 當爆炸圖片加載結束后
time.sleep(0.3)
exit()
# 加載英雄機爆炸圖片
hero_bomb_img = pygame.image.load(hero_bomb_list[hero_bomb_index])
# 繪制英雄機爆炸的圖片
screen.blit(hero_bomb_img,(heroPlaneX,heroPlaneY))
hero_bomb_index += 1
time.sleep(0.2)
pygame.display.update()
?運行結果:
?????????????????????????????????????????????????????????????????????????????????就到這吧!
?文章來源:http://www.zghlxwxcb.cn/news/detail-485275.html
?
?
?
到了這里,關于用python寫(飛機大戰(zhàn)小游戲)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!