飛機(jī)大戰(zhàn)
import pgzrun ?# 導(dǎo)入游戲庫
import random ?# 導(dǎo)入隨機(jī)庫
WIDTH = 480 ???# 設(shè)置窗口的寬度
HEIGHT = 700 ??# 設(shè)置窗口的高度
TITLE = 'Python飛機(jī)大戰(zhàn)'
?
background1 = Actor('background') ?# 導(dǎo)入背景1圖片
background1.x = 480/2 ?# 背景1的x坐標(biāo)
background1.y = 852/2 ?# 背景1的y坐標(biāo)
background2 = Actor('background') ?# 導(dǎo)入背景2圖片
background2.x = 480/2 ??# 背景2的x坐標(biāo)
background2.y = -852/2 ?# 背景2的y坐標(biāo)
?
bullet = Actor('bullet') ?# 導(dǎo)入子彈圖片
bullet.x = WIDTH/2 ???????# 子彈的x坐標(biāo)
bullet.y = -HEIGHT ??????# 子彈的y坐標(biāo),開始不可見
?
hero = Actor('hero') ?# 導(dǎo)入玩家飛機(jī)圖片
hero.x = WIDTH/2 ?????# 設(shè)置玩家飛機(jī)的x坐標(biāo)
hero.y = HEIGHT*2/3 ??# 設(shè)置玩家飛機(jī)的y坐標(biāo)
?
enemy = Actor('enemy') ?# 導(dǎo)入敵機(jī)圖片
enemy.x = WIDTH/2 ??????# 設(shè)置敵機(jī)的x坐標(biāo)
enemy.y = 0 ????????????# 設(shè)置敵機(jī)的y坐標(biāo)
?
score = 0 ????# 游戲得分
isLoose = False # 游戲是否失敗,初始不失敗
sounds. game_music.play(-1) ?# 循環(huán)播放背景音樂
?
def draw(): ?# 繪制模塊,每幀重復(fù)執(zhí)行
????background1.draw() ?# 繪制游戲背景
????background2.draw() ?# 繪制游戲背景
????hero.draw() ?# 繪制玩家飛機(jī)
????enemy.draw() ?# 繪制敵機(jī)飛機(jī)
????bullet.draw() ?# 繪制子彈
????# 下面顯示得分
????screen.draw.text("得分: "+str(score), (200, HEIGHT-50), fontsize=30,
?????????????????????fontname='s', color='black')
????if isLoose: ?# 游戲失敗后輸出信息
????????screen.draw.text("游戲失?。?#34;, (50, HEIGHT/2), fontsize=90,fontname='s', color='red')
?
def update(): ?# 更新模塊,每幀重復(fù)操作
????global score, isLoose
????if isLoose:
????????return # 如果游戲失敗,返回,不做下面的操作
?
????# 以下代碼用于實(shí)現(xiàn)背景圖片的循環(huán)滾動效果
????if background1.y > 852/2 + 852:
????????background1.y = -852/2 ?# 背景1移動到背景2的正上方
????if background2.y > 852/2 + 852:
????????background2.y = -852/2 ?# 背景2移動到背景1的正上方
????background1.y += 1 ?# 背景1向下滾動
????background2.y += 1 ?# 背景2向下滾動
?
????if bullet.y > -HEIGHT:
????????bullet.y = bullet.y - 10 # 子彈自動向上移動
?
????enemy.y += 3 # 敵機(jī)自動下落
????if enemy.y > HEIGHT: # 敵機(jī)落到畫面底部
????????enemy.y = 0 # 敵機(jī)從上面重新出現(xiàn)
????????enemy.x = random.randint(50, WIDTH-50) ?# 敵機(jī)水平位置隨機(jī)
?
????if bullet.colliderect(enemy): # 子彈與敵機(jī)發(fā)生碰撞后
????????sounds.got_enemy.play() ?# 播放擊中敵機(jī)音效
????????enemy.y = 0 ?# 敵機(jī)從上面重新出現(xiàn)
????????enemy.x = random.randint(0, WIDTH) ?# 敵機(jī)水平位置隨機(jī)
????????score = score + 1 # 得分加1
????????bullet.y = -HEIGHT ?# 隱藏子彈
?
????if hero.colliderect(enemy): # 玩家飛機(jī)和敵機(jī)發(fā)生碰撞
????????sounds.explode.play() ?# 播放玩家飛機(jī)爆炸音效
????????isLoose = True ?# 游戲失敗
????????hero.image = 'hero_blowup' # 更換游戲玩家的圖片為爆炸圖片
?
def on_mouse_move(pos, rel, buttons): ?# 當(dāng)鼠標(biāo)移動時執(zhí)行
????if isLoose:
????????return ?# 如果游戲失敗,返回,不做下面的操作
????hero.x = pos[0] ?# 玩家飛機(jī)的x坐標(biāo)設(shè)為鼠標(biāo)的x坐標(biāo)
????hero.y = pos[1] ?# 玩家飛機(jī)的y坐標(biāo)設(shè)為鼠標(biāo)的y坐標(biāo)
?
def on_mouse_down(): # 當(dāng)鼠標(biāo)鍵按下時
????if isLoose:
????????return ?# 如果游戲失敗,返回,不做下面的操作
????bullet.x = hero.x ??# 把子彈位置設(shè)為玩家飛機(jī)的正上方
????bullet.y = hero.y - 70
????sounds.gun.play() # 播放發(fā)射子彈音效
?
pgzrun.go() ?# 開始執(zhí)行游戲
?
?
?
?
import pgzrun
import random
WIDTH = 5*60
HEIGHT = 5*60
clicktime =0
id1 = -1
id2 = -1
tiles = [Actor('5×5_01'),Actor('5×5_02'),Actor('5×5_03'),Actor('5×5_04'),Actor('5×5_05'),Actor('5×5_06'),Actor('5×5_07'),Actor('5×5_08'),Actor('5×5_09'),Actor('5×5_10'),Actor('5×5_11'),Actor('5×5_12'),Actor('5×5_13'),Actor('5×5_14'),Actor('5×5_15'),Actor('5×5_16'),Actor('5×5_17'),Actor('5×5_18'),Actor('5×5_19'),Actor('5×5_20'),Actor('5×5_21'),Actor('5×5_22'),Actor('5×5_23'),Actor('5×5_24'),Actor('5×5_25'),]
grid = []
for i in range(5):
????for j in range(5):
????????tile = tiles[i*5+j]
????????tile.left = j*60
????????tile.top = i*60
????????grid.append(tile)
def draw():
????screen.clear()
????for tile in grid:
????????tile.draw()
def swap(i,j):
????temp = grid[i].pos
????grid[i].pos = grid[j].pos
????grid[j].pos = temp
for i in range(100):
????a = random.randint(0,24)
????b = random.randint(0,24)
????swap(a,b)
def on_mouse_down(pos,button):
????global clicktime,id1,id2
????for i in range(25):
????????if grid[i].collidepoint(pos):
????????????print(i)
????????????break
????if clicktime % 2 ==0:
????????id1 = j
????????clicktime +=1
????else:
????????id2 = i
????????clicktime +=1
????????swap(id1,id2)
pgzrun.go()