生活随笔
收集整理的這篇文章主要介紹了
飞机大战(源码+素材)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
素材:https://fy98.cn
最近自學了一些爬蟲知識,沒有及時更新博客。這兩天花時間做了一個類似飛機大戰的游戲,再次放出源碼以及思路。
需要實現的地方:
主角動作 碰撞效果 敵機狀態 分數記錄 游戲暫停 子彈效果 子彈傷害
設計思路:
設計游戲框架背景循環圖
設計主角動作 操控 (鼠標或鍵盤都可)
產生敵機工廠 產生敵機
設計子彈效果 主角發射子彈
設計獎勵效果
設計子彈 主角 敵機 獎勵 相互碰撞的結果
主函數 循環事件
-----------大概思路就是這些 ----------------
下面先放出效果圖
-----------放出代碼思路-------------
import pygame
,os
,sys
,math
,time
,random
from pygame
.locals import *
startImgs
=[pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\bk2.jpg"),(640,825)), pygame
.image
.load
(r
"Game_Main\Img_File\title2.png"), pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\icon2.png"), (220, 60)), pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\loading.png"),(640,640)), pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\music_stop.png"),(45,45)), pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\music_start.png"),(45,45)), pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\mouse3.png"),(30,30)), pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\icon2.png"),(284,82)), pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\bk1.png"),(640,850)), pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\bk4.png"),(640,850)), ]
statBotImg
=[pygame
.image
.load
(r
"Game_Main\Img_File\1.png"),
pygame
.image
.load
(r
"Game_Main\Img_File\2.png"),
pygame
.image
.load
(r
"Game_Main\Img_File\3.png"),
pygame
.image
.load
(r
"Game_Main\Img_File\4.png"),
pygame
.image
.load
(r
"Game_Main\Img_File\5.png"),
pygame
.image
.load
(r
"Game_Main\Img_File\6.png"),
pygame
.image
.load
(r
"Game_Main\Img_File\7.png"),
pygame
.image
.load
(r
"Game_Main\Img_File\8.png"),
pygame
.image
.load
(r
"Game_Main\Img_File\9.png"),
pygame
.image
.load
(r
"Game_Main\Img_File\10.png"),
pygame
.image
.load
(r
"Game_Main\Img_File\11.png"),]
class StartPanel:isIn_Start
=FalseisIn_BGM
=FalseisOn_BGM
=Truedef __init__(self
,screen
,imgs
=startImgs
):self
.screen
=screenself
.imgs
=imgsself
.sin_y
=0def Show(self
):self
.screen
.blit
(self
.imgs
[0],(0,0))Place_title
=self
.screen
.blit
(self
.imgs
[1],(180+30*math
.cos
(self
.sin_y
),100+50*math
.sin
(self
.sin_y
)))tempC
=Place_title
.collidepoint
(pygame
.mouse
.get_pos
())self
.sin_y
+= 0.05if self
.sin_y
>=100: self
.sin_y
=0elif tempC
:Place_title
=tempC
if StartPanel
.isOn_BGM
==True:Place_BGM
=self
.screen
.blit
(self
.imgs
[4], (30, 740))tempA
= Place_BGM
.collidepoint
(pygame
.mouse
.get_pos
())StartPanel
.isIn_BGM
= tempA
else:Place_BGM
= self
.screen
.blit
(self
.imgs
[5], (33, 743))tempA
= Place_BGM
.collidepoint
(pygame
.mouse
.get_pos
())StartPanel
.isIn_BGM
= tempAPlace_StartBtm
=self
.screen
.blit
(self
.imgs
[2],(210,350))tempB
=Place_StartBtm
.collidepoint
(pygame
.mouse
.get_pos
())StartPanel
.isIn_Start
=tempB
if StartPanel
.isIn_Start
:self
.screen
.blit
(self
.imgs
[7], (185, 340))self
.screen
.blit
(self
.imgs
[3],(0,66))temp
=pygame
.mouse
.get_pos
()self
.screen
.blit
(self
.imgs
[6], pygame
.mouse
.get_pos
())
loadImages
=[pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/bk5.png"),(640,800)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/bk6.png"),(150,160)),
]
class LoadWindows():load_time
=Falsetime_add
=0def __init__(self
, screen
, pos
,imgs
=loadImages
,btmimgs
=statBotImg
):self
.screen
=screenself
.imgs
=loadImages
[0]self
.rect
=self
.imgs
.get_rect
()self
.rect
.topleft
=posself
.image
=imgsself
.btmimgs
= btmimgsself
.btmIndex
= 0self
.btmAdd
= 0 def Show(self
):if self
.load_time
==False:self
.time_add
+= 1self
.screen
.blit
(self
.imgs
, self
.rect
)self
.screen
.blit
(self
.image
[1], (10, 10))if self
.time_add
== 260:self
.load_time
= Trueself
.Move
()self
.screen
.blit
(font_tips
.render
("Tips:",True,pygame
.Color
("black")),(25,540))self
.screen
.blit
(font_tips
.render
("1.您可通過F1鍵更換鍵鼠操作模式。",True,pygame
.Color
("red")),(50,580))self
.screen
.blit
(font_tips
.render
("2.游戲主界面左下角可關閉背景音樂。ESC鍵可強制返回主界面",True,pygame
.Color
("red")),(50,610))self
.screen
.blit
(font_tips
.render
("3.空格鍵可暫停游戲,再次按下即可開啟。",True,pygame
.Color
("red")),(50,640))self
.screen
.blit
(font_tips
.render
("4.更多精彩內容將會陸續更新。",True,pygame
.Color
("red")),(50,670))self
.screen
.blit
(self
.btmimgs
[self
.btmIndex
],(245,700))self
.btmAdd
+=1if self
.btmAdd
==25: self
.btmIndex
+=1self
.btmAdd
=0if self
.btmIndex
>=11: self
.btmIndex
=0def Move(self
):if self
.rect
.x
==0:self
.rect
.move
(0,0)else:self
.rect
= self
.rect
.move
(-32, 40)
class GameBG:def __init__(self
,image1
,image2
,screen1
,screen2
,speed1
,speed2
) -> None:self
.image1
=image1self
.image2
=image1
.copy
()self
.rect1
=self
.image1
.get_rect
()self
.rect2
=self
.image2
.get_rect
()self
.rect2
.topleft
=(0,-850)self
.image3
= image2self
.image4
= image2
.copy
()self
.rect3
= self
.image3
.get_rect
()self
.rect4
= self
.image4
.get_rect
()self
.rect4
.topleft
= (0, -850)self
.screen1
=screen1self
.speed1
= speed1self
.screen2
= screen2self
.speed2
= speed2
def Show(self
):self
.rect1
= self
.rect1
.move
(0, self
.speed1
)self
.rect2
= self
.rect2
.move
(0, self
.speed1
)if self
.rect1
.y
>=825:self
.rect1
.y
=self
.rect2
.y
-850if self
.rect2
.y
>=825:self
.rect2
.y
=self
.rect1
.y
-850self
.screen1
.blit
(self
.image1
, self
.rect1
)self
.screen1
.blit
(self
.image2
, self
.rect2
)self
.rect3
= self
.rect3
.move
(0, self
.speed2
)self
.rect4
= self
.rect4
.move
(0, self
.speed2
)if self
.rect3
.y
>=825:self
.rect3
.y
= self
.rect4
.y
- 850if self
.rect4
.y
>= 825:self
.rect4
.y
= self
.rect3
.y
- 850self
.screen2
.blit
(self
.image3
, self
.rect3
)self
.screen2
.blit
(self
.image4
, self
.rect4
)
GameImg
=[pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\hero3.png"), (68, 68)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\hero4.png"),(68,68)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\heroskill.png"),(120,120)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\planpigu4.png"),(20,20)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\planpigu4.png"),(40,40)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\planpigu4.png"),(60,80)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\planpigu4.png"),(80,80)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\planpigu4.png"),(100,100)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\planpigu4.png"),(80,80)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\planpigu4.png"),(60,60)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\planpigu4.png"),(40,40)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\planpigu4.png"),(20,20)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\planpigu4.png"),(0,0)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\safetao.png"),(200,200)),
]
class Hero:up
= Falsedown
= Falseleft
= Falseright
= FalseisHeroDead
=FalseisIn
=FalseisCMK
=FalsebuttleAdd
=0buttleCurve
=0On_Off_BUG1
=FalseBUG1_time
=0herf_pigu
=3herf_pigu_Add
=0isSafe
=FalsesafeTime
=0deadIndex
=0deadAdd
=0R_skill
= 0def __init__(self
,image
,pos
,screen
,speed
,hp
,score
,bulletspeed
,image2
,screen2
,speed2
):self
.image
= GameImg
[0]self
.rect
= self
.image
.get_rect
() self
.rect
.topleft
= pos self
.screen
= screenself
.speed
= speedself
.hp
= hpself
.score
= scoreself
.bulletspeed
=bulletspeedself
.imgs
=imageself
.heroAdd
=0self
.heroIndex
=0self
.image3
= image2self
.image4
= image2
.copy
()self
.rect3
= self
.image3
.get_rect
()self
.rect4
= self
.image4
.get_rect
()self
.rect4
.topleft
= (0, -850)self
.speed2
=speed2self
.screen2
=screen2
def Move(self
): if Hero
.up
:self
.rect
= self
.rect
.move
(0, -self
.speed
)elif Hero
.down
:self
.rect
= self
.rect
.move
(0, self
.speed
)elif Hero
.left
:self
.rect
= self
.rect
.move
(-self
.speed
, 0)elif Hero
.right
:self
.rect
= self
.rect
.move
(self
.speed
, 0)if Hero
.isCMK
:self
.rect
.center
=pygame
.mouse
.get_pos
()else:Hero
.isIn
=self
.rect
.collidepoint
(pygame
.mouse
.get_pos
())if self
.rect
.x
>=572:self
.rect
.x
=572if self
.rect
.x
<=0:self
.rect
.x
=0if self
.rect
.y
>=732:self
.rect
.y
=732if self
.rect
.y
<=0:self
.rect
.y
=0def Show(self
):if self
.hp
>0:self
.Move
()self
.Collide
()self
.screen
.blit
(self
.imgs
[self
.herf_pigu
], (self
.rect
.bottomleft
[0]+10,self
.rect
.midbottom
[1]))self
.herf_pigu_Add
+= 1if self
.herf_pigu_Add
==4:self
.herf_pigu
+= 1self
.herf_pigu_Add
=0if self
.herf_pigu
==13:self
.herf_pigu
=3if self
.On_Off_BUG1
==False:self
.screen
.blit
(self
.imgs
[0], self
.rect
)else:self
.screen
.blit
(self
.imgs
[1], self
.rect
)if self
.On_Off_BUG1
== True:heroObj
.BUG1_time
+=1self
.buttleAdd
+= 1self
.buttleCurve
+=0.1if self
.buttleAdd
>3:Bullet
(BulletImages
[1],self
.screen
,(self
.rect
.midtop
[0]-25+(30*math
.sin
(self
.buttleCurve
)),self
.rect
.midtop
[1]),speed
=self
.bulletspeed
+15)Bullet
(BulletImages
[0],self
.screen
,(self
.rect
.midtop
[0]+18+(-30*math
.sin
(self
.buttleCurve
)),self
.rect
.midtop
[1]),speed
=self
.bulletspeed
+15)Bullet1
(BulletImages
[3],self
.screen
,(self
.rect
.midtop
[0]-20,self
.rect
.midtop
[1]),speed
=self
.bulletspeed
+15)Bullet2
(BulletImages
[3],self
.screen
,(self
.rect
.midtop
[0]-20,self
.rect
.midtop
[1]),speed
=self
.bulletspeed
+15)self
.buttleAdd
= 0elif self
.On_Off_BUG1
==False:self
.buttleAdd
+= 1if self
.buttleAdd
== 10:Bullet
(BulletImages
[2],self
.screen
,(self
.rect
.midtop
[0]-7,self
.rect
.midtop
[1]),speed
=self
.bulletspeed
+50)self
.buttleAdd
= 0if self
.buttleCurve
>10:self
.buttleCurve
=0for bullet1
in Bullettemp
:bullet1
.Show
()if self
.BUG1_time
> 100:self
.BUG1_time
= 0self
.On_Off_BUG1
= Falseelse:self
.Dead
()soundlist
[4].play
()def Dead(self
): global isPlayself
.screen
.blit
(deadImages
[self
.deadIndex
],self
.rect
)soundlist
[0].play
()self
.deadAdd
+=1if self
.deadAdd
==4:self
.deadIndex
+=1self
.deadAdd
=0if self
.deadIndex
==len(deadImages
):self
.deadIndex
=0if self
.score
> history
:UpdateScore
(self
.score
)heroObj
.isHeroDead
=Truedef Collide(self
): tempReward
=pygame
.sprite
.spritecollideany
(self
,rewardlist
,collided
=pygame
.sprite
.collide_mask
)if tempReward
!=None:if tempReward
.tag
==3 and self
.hp
>=10:passelse:if tempReward
.tag
==1:self
.On_Off_BUG1
=Trueself
.BUG1_time
=0elif tempReward
.tag
==2:for i
in enemylist
:i
.hp
=0elif tempReward
.tag
==3 and self
.hp
<10:self
.hp
+=1if tempReward
in rewardlist
:rewardlist
.remove
(tempReward
)if self
.isSafe
==False:tempEnemy
=pygame
.sprite
.spritecollideany
(self
,enemylist
,collided
=pygame
.sprite
.collide_mask
)if tempEnemy
!=None:tempEnemy
.hp
=0self
.hp
-= 1self
.isSafe
=Trueelse:self
.safeTime
+=1self
.screen
.blit
(GameImg
[13],(self
.rect
[0]-68,self
.rect
[1]-68))if self
.safeTime
==100:self
.safeTime
=0self
.isSafe
=False
BulletImages
=[pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\bullet6.png"), (15, 30)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\bullet5.png"), (15, 30)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\bullet3.png"), (15, 30)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\bullet4.png"), (50, 30)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\bullet3.png"), (15, 30)),pygame
.transform
.scale
(pygame
.image
.load
(r
"Game_Main\Img_File\bullet3.png"), (15, 30)),
]
Bullettemp
=[]
class Bullet:def __init__(self
,image
,screen
,pos
,speed
):self
.image
= imageself
.screen
=screenself
.rect
=self
.image
.get_rect
() self
.rect
.center
=pos self
.speed
=speedBullettemp
.append
(self
)soundlist
[1].play
()def Show(self
):self
.rect
=self
.rect
.move
(0,-self
.speed
) self
.screen
.blit
(self
.image
,self
.rect
) if self
.rect
.y
< -50:if self
in Bullettemp
:Bullettemp
.remove
(self
)self
.Collide
()def Collide(self
):temp
=pygame
.sprite
.spritecollideany
(self
,enemylist
,collided
=pygame
.sprite
.collide_mask
)if temp
!=None:temp
.hp
-=1if self
in Bullettemp
:Bullettemp
.remove
(self
)
class Bullet1(Bullet
):def __init__(self
, image
, screen
, pos
, speed
):super().__init__
(image
, screen
, pos
, speed
)def Show(self
):self
.rect
=self
.rect
.move
(20,-self
.speed
) self
.screen
.blit
(self
.image
, self
.rect
) if self
.rect
.y
< -50:if self
in Bullettemp
:Bullettemp
.remove
(self
)self
.Collide
()def Collide(self
):super().Collide
()
class Bullet2(Bullet
):def __init__(self
, image
, screen
, pos
, speed
):super().__init__
(image
, screen
, pos
, speed
)def Show(self
):self
.rect
=self
.rect
.move
(-20,-self
.speed
) self
.screen
.blit
(self
.image
, self
.rect
) if self
.rect
.y
< -50:if self
in Bullettemp
:Bullettemp
.remove
(self
)self
.Collide
()def Collide(self
):super().Collide
()
enemyimage
=[pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/enemyair1.png"),(60,60)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/enemyair2.png"),(60,60)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/enemyair3.png"),(60,60)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/enemyair4.png"), (120, 120)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/enemyair5.png"),(120,120)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/enemyair6.png"),(120,120)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/enemyair7.png"),(240,240)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/enemyair8.png"),(240,240)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/enemyair9.png"),(240,240)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/enemyair10.png"),(240,240)),
]
enemylist
=[]
class Enemy:def __init__(self
,imgs
,screen
,speed
,pos
,hp
,tag
):self
.imgs
=imgsself
.image
=imgsself
.screen
=screenself
.speed
=speedself
.rect
=self
.image
.get_rect
()self
.rect
.midbottom
=posself
.tag
=tagself
.hp
=hpself
.deadIndex
=0self
.deadAdd
=0enemylist
.append
(self
)def Show(self
):if self
.hp
>0:self
.Move
()self
.screen
.blit
(self
.image
,self
.rect
)else:self
.Dead
()def Move(self
):self
.rect
=self
.rect
.move
(0,self
.speed
)if self
.rect
.y
>=801:if self
in enemylist
:enemylist
.remove
(self
)def Dead(self
):self
.screen
.blit
(deadImages
[self
.deadIndex
],self
.rect
)soundlist
[0].play
()self
.deadAdd
+=1if self
.deadAdd
==2:self
.deadIndex
+=1self
.deadAdd
=0if self
.deadIndex
==len(deadImages
):if self
.tag
== 1:heroObj
.score
+= 1elif self
.tag
== 2:heroObj
.score
+= 3elif self
.tag
== 3:heroObj
.score
+= 5enemylist
.remove
(self
)def Collide(self
):pass
rewardImage
=[pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/reward1.png"),(80,80)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/reward2.png"),(80,80)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/reward3.png"),(80,80)),
]
rewardlist
=[]
class Reward:def __init__(self
,image
,screen
,speed
,pos
,tag
):self
.image
=imageself
.screen
=screenself
.speed
=speedself
.rect
=self
.image
.get_rect
()self
.rect
.midbottom
=posself
.tag
=tagrewardlist
.append
(self
)def Show(self
):self
.Move
()self
.screen
.blit
(self
.image
,self
.rect
)def Move(self
):self
.rect
=self
.rect
.move
(0,self
.speed
)if self
.rect
.y
>=801:if self
in rewardlist
:rewardlist
.remove
(self
)
class Factory:createAdd
=0createrewardAdd
=0@
staticmethoddef CreateEnemy():Factory
.createAdd
+=1if Factory
.createAdd
==20:ranNum
=random
.randint
(1,100)if ranNum
<=70:Enemy
(enemyimage
[random
.randint
(0,2)],screen
=screen
,speed
=random
.randint
(4,6),pos
=(random
.randint
(0,580),-60),hp
=1,tag
=1)elif ranNum
<=90:Enemy
(enemyimage
[random
.randint
(3,5)],screen
=screen
,speed
=random
.randint
(2,3),pos
=(random
.randint
(0,520),-120),hp
=3,tag
=2)elif ranNum
<=100:Enemy
(enemyimage
[random
.randint
(6,9)],screen
=screen
,speed
=random
.randint
(1,2),pos
=(random
.randint
(0,400),-240),hp
=10,tag
=3)Factory
.createAdd
=0soundlist
[3].set_volume
(0.05)for temp
in enemylist
:temp
.Show
()@
staticmethoddef CreateReward():Factory
.createrewardAdd
+=1if Factory
.createrewardAdd
==100:ranNum
=random
.randint
(1,10)if ranNum
==1:Reward
(rewardImage
[0],screen
=screen
,speed
=random
.randint
(4,6),pos
=(random
.randint
(0,560),-81),tag
=1)elif ranNum
==2:Reward
(rewardImage
[1],screen
=screen
,speed
=random
.randint
(4,6),pos
=(random
.randint
(0,560),-81),tag
=2)elif ranNum
== 3:Reward
(rewardImage
[2], screen
=screen
, speed
=random
.randint
(4, 6), pos
=(random
.randint
(0, 560), -81),tag
=3)Factory
.createrewardAdd
=0for i
in rewardlist
:i
.Show
()
deadImages
=[pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/da_0.png"),(80,40)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/da_1.png"),(80,80)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/da_2.png"),(80,80)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/da_3.png"),(80,80)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/da_4.png"),(80,80)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/da_5.png"),(80,80)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/da_6.png"),(80,80)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/da_7.png"),(80,80)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/da_8.png"),(80,80)),
]
gameImgs
=[pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/gameover1.jpg"),(640,800)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/gameover2.png"),(611,412)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/gameover3.png"), (150, 100)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/gameover4.png"), (180, 75)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/gameover4.png"), (220, 90)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/gameover5.png"), (64, 64)),pygame
.transform
.scale
(pygame
.image
.load
("Game_Main/Img_File/gameover5.png"), (80, 80)),
]
class GameOver:isMouse_In_end
=FalseisMouse_In_again
= Falsedef __init__(self
,imgs
,pos
,screen
) -> None:self
.imgs
=gameImgsself
.image
=gameImgs
[0]self
.rect
=self
.image
.get_rect
()self
.rect
.topleft
=posself
.screen
=screen
def Show(self
):pygame
.mouse
.set_visible
(True)self
.screen
.blit
(gameImgs
[0], self
.rect
)self
.screen
.blit
(gameImgs
[1],(self
.rect
))self
.screen
.blit
(gameImgs
[2], (self
.rect
.midtop
[0],self
.rect
.midtop
[1]+30))again_icon
=self
.screen
.blit
(gameImgs
[3], (self
.rect
.center
[0]-80,self
.rect
.center
[1]+30))self
.isMouse_In_again
=again_icon
.collidepoint
(pygame
.mouse
.get_pos
())if self
.isMouse_In_again
:self
.screen
.blit
(gameImgs
[4], (self
.rect
.center
[0] - 100, self
.rect
.center
[1] + 20))end_icon
=self
.screen
.blit
(gameImgs
[5], (self
.rect
.topright
[0]-100, self
.rect
.topright
[1]+10))self
.isMouse_In_end
=end_icon
.collidepoint
(pygame
.mouse
.get_pos
())if self
.isMouse_In_end
:self
.screen
.blit
(gameImgs
[6], (self
.rect
.topright
[0] - 109, self
.rect
.topright
[1] + 5))self
.screen
.blit
(font
.render
("最終分數:%s"%heroObj
.score
, True, pygame
.Color
("green")), (self
.rect
.center
[0]-230,self
.rect
.center
[1]-75))self
.screen
.blit
(font
.render
("歷史最高分:%s"%history
, True, pygame
.Color
("green")), (self
.rect
.center
[0]+20,self
.rect
.center
[1]-75))self
.Move
()def Move(self
):if self
.rect
.y
<0:self
.rect
=self
.rect
.move
(0,12)else:self
.rect
=self
.rect
.move
(0,0)
isPlay
=False
clock
=pygame
.time
.Clock
()
pygame
.font
.init
()
font
=pygame
.font
.Font
("Game_Main/Img_File/fount.otf",35)
font_end
=pygame
.font
.Font
("Game_Main/Img_File/fount.otf",80)
font_tips
=pygame
.font
.Font
("Game_Main/Img_File/fount.otf",20)
pygame
.init
()
screen2
=pygame
.display
.set_mode
((640,800))
screen
=pygame
.display
.set_mode
((640,800))
pygame
.mixer
.music
.load
(r
"Game_Main/Img_File/bgm2.wav")
pygame
.mixer
.music
.play
(-1)
startObj
=StartPanel
(screen
)
loadObj
=LoadWindows
(screen
=screen
,pos
=(640,-800))
gameObj
=GameBG
(image1
=startImgs
[8],image2
=startImgs
[9],screen1
=screen
,screen2
=screen2
,speed1
=4,speed2
=2)
heroObj
=Hero
(image
=GameImg
,pos
=(310,600),screen
=screen
,speed
=25,hp
=1,score
=0,bulletspeed
=10,image2
=startImgs
[9],screen2
=screen2
,speed2
=2)
gameoverObj
=GameOver
(imgs
=gameImgs
,pos
=(0,-800),screen
=screen
)
history
=0
soundlist
=[
pygame
.mixer
.Sound
("Game_Main/Img_File/boom.wav"),
pygame
.mixer
.Sound
("Game_Main/Img_File/fashe.wav"),
pygame
.mixer
.Sound
("Game_Main/Img_File/kaishiyouxi.ogg"),
pygame
.mixer
.Sound
("Game_Main/Img_File/planhuxiao.wav"),
pygame
.mixer
.Sound
("Game_Main/Img_File/herodead.wav"),
]
def EventAll():global isPlay
for event
in pygame
.event
.get
():if event
.type==pygame
.QUIT
:pygame
.quit
()sys
.exit
()if event
.type==pygame
.MOUSEBUTTONDOWN
:if event
.button
==1 and StartPanel
.isIn_Start
==1:StartPanel
.isIn_Start
=FalseInitMethod
()gameObj
.Show
()soundlist
[2].play
()isPlay
= Trueif event
.button
==1 and gameoverObj
.isMouse_In_again
==1:gameoverObj
.isMouse_In_again
=FalseheroObj
.isHeroDead
=FalseInitMethod
()gameObj
.Show
()soundlist
[2].play
()isPlay
= TrueheroObj
.isHeroDead
= Falseif event
.button
==1 and gameoverObj
.isMouse_In_end
==1:exit
()sys
.exit
()elif event
.button
==1 and StartPanel
.isIn_BGM
==1:StartPanel
.isOn_BGM
=not StartPanel
.isOn_BGM
if StartPanel
.isOn_BGM
:pygame
.mixer
.music
.unpause
()else:pygame
.mixer
.music
.pause
()if event
.type == pygame
.KEYDOWN
: if event
.key
==pygame
.K_ESCAPE
:isPlay
=Falseif event
.key
==pygame
.K_SPACE
:x
=Truepygame
.mixer_music
.pause
()while x
:for stop
in pygame
.event
.get
():if stop
.type==pygame
.KEYDOWN
:if stop
.key
==pygame
.K_SPACE
:pygame
.mixer_music
.unpause
()x
=Falsebreakif event
.key
== pygame
.K_w
:Hero
.up
= Trueelif event
.key
== pygame
.K_s
:Hero
.down
= Trueelif event
.key
== pygame
.K_a
:Hero
.left
= Trueelif event
.key
== pygame
.K_d
:Hero
.right
= Trueif event
.type == pygame
.KEYUP
: if event
.key
== pygame
.K_w
:Hero
.up
= Falseelif event
.key
== pygame
.K_s
:Hero
.down
= Falseelif event
.key
== pygame
.K_a
:Hero
.left
= Falseelif event
.key
== pygame
.K_d
:Hero
.right
= Falseif event
.type==pygame
.KEYDOWN
:if event
.key
==pygame
.K_F1
:Hero
.isCMK
=not Hero
.isCMK
def ReadScore(path
="score.json"):if os
.path
.exists
(path
):with open(path
,"r") as f_r
:return int(f_r
.read
())else:with open(path
,"w") as f_w
:f_w
.write
("0")return 0
def UpdateScore(newScore
,path
="score.json"):with open(path
,"w") as f_w
:f_w
.write
(str(newScore
))
def InitMethod():global historyhistory
=ReadScore
()heroObj
.hp
=1heroObj
.score
=0heroObj
.rect
.topleft
=(310,600)enemylist
.clear
()Bullettemp
.clear
()rewardlist
.clear
()gameoverObj
.rect
.topleft
=(0,-800)
def Main():pygame
.mouse
.set_visible
(False)while True:EventAll
()if isPlay
and heroObj
.isHeroDead
==False:loadObj
.Show
()if loadObj
.load_time
==True:gameObj
.Show
()Factory
.CreateEnemy
()Factory
.CreateReward
()heroObj
.Show
()screen
.blit
(font
.render
("血量:%s"%heroObj
.hp
,True,pygame
.Color
("red")),(0,0))screen
.blit
(font
.render
("分數:%s"%heroObj
.score
,True,pygame
.Color
("red")),(0,50))screen
.blit
(font
.render
("歷史最高分:%s"%history
,True,pygame
.Color
("red")),(0,100))screen
.blit
(font
.render
("終極技能:%s" %(heroObj
.R_skill
), True, pygame
.Color
("green")), (200, 0))elif isPlay
==False:startObj
.Show
()elif isPlay
and heroObj
.isHeroDead
==True:gameoverObj
.Show
()pygame
.display
.update
()clock
.tick
(120)
if __name__
=="__main__":Main
()
如果想要素材的話請留言吧~
總結
以上是生活随笔為你收集整理的飞机大战(源码+素材)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。