資源簡介
用python+pygame實現(xiàn)的飛機大戰(zhàn)的源碼,包含資源,可直接運行。通過鼠標(biāo)控制。
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 繪畫背景
screen.blit(background,(0, 0))
# 檢測游戲狀態(tài)
if not gameover:
# 定位鼠標(biāo)的x, y坐標(biāo)
x, y = pygame.mouse.get_pos()
# 發(fā)射子彈
interval_b -= 1
if interval_b < 0:
bullets[index_b].restart()
interval_b = 100
index_b = (index_b + 1) % count_b
for b in bullets:
if b.active:
# 檢查子彈命中情況
for e in enemies:
# 擊中敵機,分?jǐn)?shù)加100
if checkHit(e, b):
score += 100
b.move()
screen.blit(b.image, (b.x, b.y))
# 繪畫機群
for e in enemies:
e.move()
screen.blit(e.image, (e.x, e.y))
if checkCrash(e, plane):
gameover = True
e.move()
screen.blit(e.image, (e.x, e.y))
plane.move()
screen.blit(plane.image, (plane.x, plane.y))
# 屏幕左上角顯示分?jǐn)?shù)
text = font.render("Score: %d" % score, 1, (0, 0, 0))
screen.blit(text, (0, 0))
else:
text = font.render("Score: %d" % score, 1, (0, 0, 0))
screen.blit(text, (190, 400))
# 游戲結(jié)束后,檢測鼠標(biāo)抬起就“重置游戲”
if gameover and event.type == pygame.MOUSEBUTTONUP:
plane.restart()
for e in enemies:
e.restart()
for b in bullets:
b.active = False
score = 0
gameover = False
pygame.display.update()

代碼片段和文件信息
import?pygame
import?os
from?sys?import?exit?
import?random
class?Bullet:
????def?__init__(self):
????????‘‘‘初始化子彈坐標(biāo)并加載子彈圖片‘‘‘
????????self.x?=?0
????????self.y?=?-1
????????self.image?=?pygame.image.load(bullet_img).convert_alpha()
????????self.active?=?False
????def?move(self):
????????‘‘‘移動子彈‘‘‘
????????if?self.active:
????????????self.y?-=?3
????????if?self.y?0:
????????????self.active?=?False
????def?restart(self):
????????mouseX?mouseY?=?pygame.mouse.get_pos()
????????self.x?=?x?-?self.image.get_width()/2
????????self.y?=?y?-?self.image.get_height()/2
????????self.active?=?True
class?Enemy:
????def?__init__(self):
????????self.restart()
????????self.image?=?pygame.image.load(enemy_img).convert_alpha()
????def?restart(self):
????????self.x?=?random.randint(50400)
????????self.y?=?random.randint(-200-50)
????????self.speed?=?random.random()?+?0.1
????def?move(self):
????????if?self.y?800:
????????????self.y?+=?self.speed
????????else:
????????????self.restart()
pygame.init()
bg_img?=?‘.\\ui\shoot_background\\background.png‘
bg_img2?=?‘bg2.jpg‘
plane_img?=?‘.\\ui\\plane.png‘
bullet_img?=?‘.\\ui\\bullet.png‘
enemy_img?=?‘.\\ui\\enemy.png‘
screen?=?pygame.display.set_mode((450?800)?0?32)
pygame.display.set_caption(‘Hello?World!‘)
background?=?pygame.image.load(bg_img).convert()
plane?=?pygame.image.load(plane_img).convert_alpha()
bullet?=?Bullet()
enemy?=?Enemy()?????#?建立敵機
bullets?=?[]
for?i?in?range(5):
????bullets.append(Bullet())
count_b?=?len(bullets)??????#?子彈的總數(shù)
index_b?=?0?????????#?即將激活的子彈序號
interval_b?=?0??????#?發(fā)射子彈的間隔
while?True:
????for?event?in?pygame.event.get():
????????if?event.type?==?pygame.QUIT:
????????????pygame.quit()
????????????exit()
????#?繪畫背景
????screen.blit(background(0?0))
????#?定位鼠標(biāo)的x?y坐標(biāo)
????x?y?=?pygame.mouse.get_pos()
????#?繪畫敵機到屏幕上
????enemy.move()
????screen.blit(enemy.image?(enemy.x?enemy.y))
????#?發(fā)射子彈
????interval_b?-=?1
????if?interval_b?0:
????????bullets[index_b].restart()
????????interval_b?=?100
????????index_b?=?(index_b?+?1)?%?count_b
????for?b?in?bullets:
????????if?b.active:
????????????b.move()
????????????screen.blit(b.image?(b.x?b.y))
????#?定位并繪畫飛機到屏幕上
????x?-=?plane.get_width()/2
????y?-=?plane.get_height()/2
????screen.blit(plane?(x?y))
????pygame.display.update()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4714??2014-07-17?15:26??Airplane\121.png
?????文件??????68134??2014-12-27?16:16??Airplane\bg1.jpg
?????文件??????27996??2014-12-27?16:10??Airplane\bg2.jpg
?????文件???????1679??2013-08-29?08:17??Airplane\font\font.fnt
?????文件???????5193??2013-08-29?08:17??Airplane\font\font.png
?????文件????1437502??2014-12-27?19:35??Airplane\game04.gif
?????文件???????6590??2014-07-17?15:58??Airplane\game_Reagain.png
?????文件???????6871??2014-07-17?15:57??Airplane\game_start.png
?????文件????1278671??2014-12-27?19:19??Airplane\PyGame系列-12.pdf
?????文件???????2553??2017-10-11?14:17??Airplane\pygam_hello.py
?????文件???????2979??2017-10-11?14:19??Airplane\pygam_hello_10.py
?????文件???????4102??2017-10-11?14:19??Airplane\pygam_hello_11.py
?????文件???????4903??2017-10-11?15:05??Airplane\pygam_hello_12.py
?????文件???????2553??2017-10-11?14:18??Airplane\pygam_hello_8.py
?????文件???????2625??2017-10-11?14:19??Airplane\pygam_hello_9.py
?????文件???????8831??2013-08-29?08:17??Airplane\sound\achievement.mp3
?????文件??????12327??2013-08-29?08:17??Airplane\sound\big_spaceship_flying.mp3
?????文件???????7751??2013-08-29?08:17??Airplane\sound\bullet.mp3
?????文件???????6815??2013-08-29?08:17??Airplane\sound\button.mp3
?????文件??????11287??2013-08-29?08:17??Airplane\sound\enemy1_down.mp3
?????文件??????12847??2013-08-29?08:17??Airplane\sound\enemy2_down.mp3
?????文件???????9415??2013-08-29?08:17??Airplane\sound\enemy3_down.mp3
?????文件?????117728??2013-08-29?08:17??Airplane\sound\game_music.mp3
?????文件??????14615??2013-08-29?08:17??Airplane\sound\game_over.mp3
?????文件???????9653??2013-08-29?08:17??Airplane\sound\get_bomb.mp3
?????文件??????13494??2013-08-29?08:17??Airplane\sound\get_double_laser.mp3
?????文件??????10693??2013-08-29?08:17??Airplane\sound\out_porp.mp3
?????文件??????10703??2013-08-29?08:17??Airplane\sound\use_bomb.mp3
?????文件???????1378??2014-07-21?09:00??Airplane\ui\bkmusic_close.png
?????文件???????1398??2014-07-21?09:00??Airplane\ui\bkmusic_play.png
............此處省略62個文件信息
- 上一篇:ArcGIS_Python入門指南
- 下一篇:python知乎評論爬蟲源代碼
評論
共有 條評論