from pgzrun import *
from random import *
#{
music.play("bg.mp3")
WIDTH = 360
HEIGHT = 642
bg0 = Actor("bg0.png")
bg1 = Actor("bg1.png")
bg2 = Actor("bg2.png")
bg2.bottom = 0
plane = Actor("plane.png", [180, 570])
life_show = Actor("life.png", [300, 38])
score_show = Actor("score.png", [60, 38])
again = Actor("again.png",[180,375])
over = Actor("over.png")
boss = Actor("boss.png", [1000, 1000])
lucky = Actor("lucky.png",[1000, 1000])
#}
me = Actor("me.png")
score = 0
life = 3
num = 0 #計數:子彈擊中大boss的次數
kind = "1" #子彈類型
#{
#創建子彈
bullet = []
def create_bullet():
b = Actor("bullet"+kind+".png")
b.x = plane.x
b.y = plane.y
bullet.append(b)
clock.schedule_interval(create_bullet, 0.2)
#創建菜鳥
bird = []
def create_bird():
b = Actor("bird.png")
b.x = randint(20,330)
b.y = randint(-100, -50)
bird.append(b)
clock.schedule_interval(create_bird, 0.5)
#按空格鍵切換游戲狀態為運行狀態
state = "ready"
def on_key_down():
global state
if keyboard.space == True:
state = "run"
def draw():
global state
#開始狀態則繪制bg0
if state == "ready":
bg0.draw()
#如果是運行狀態,則繪制相關角色
if state == "run":
bg1.draw()
bg2.draw()
for b in bullet:
b.draw()
plane.draw()
me.draw()
for a in bird:
a.draw()
life_show.draw()
score_show.draw()
boss.draw()
lucky.draw()
screen.draw.text(
str(score),
fontsize=30,
center=[75, 38]
)
screen.draw.text(
str(life),
fontsize=30,
center=[315, 38]
)
#如果是結束狀態,則繪制結束頁、重玩按鈕、最終分數,結束音樂播放
if state == "over":
over.draw()
again.draw()
screen.draw.text(
str(score),
fontsize=80,
center=[178, 310],
color=(138, 43, 226)
)
music.stop()
#背景滾動
def move_bg():
bg1.y = bg1.y + 10
bg2.y = bg2.y + 10
if bg1.top > HEIGHT:
bg1.bottom = 0
if bg2.top > HEIGHT:
bg2.bottom = 0
#子彈移動
def move_bullet():
global score, num, kind
for b in bullet:
b.y = b.y - 2
#子彈擊中boss後,計數,移除子彈
if b.colliderect(boss):
num = num + 1
bullet.remove(b)
break
for a in bird:
#如果菜鳥和子彈相碰,則分別移除菜鳥和子彈,加分
if b.colliderect(a):
bird.remove(a)
if b in bullet:
bullet.remove(b)
score = score + 1
sounds.hit.play()
"""
如果子彈移出屏幕,則移除子彈,注意有可能子彈剛移出屏幕時
和菜鳥碰撞,這時第123行已經將子彈移除,此時若再移除一遍
則會報錯,因此需要判斷b是否在bullet中,即b是否已被移除
"""
if b.bottom < 0:
if b in bullet:
bullet.remove(b)
#}
#每得50分boss和幸運箱就出現一次
if score % 50 == 0 and score > 0 :
boss.pos = [160 , 180]
lucky.pos = [randint(24,330), -200]
#子彈每擊中boss 50下就加100分,boss消失,num歸零下次重新計數,子彈復原
if num == 50:
score = score + 100
boss.pos = [1000,1000]
num = 0
kind = "1"
#菜鳥移動
def move_bird():
global life, state
for a in bird:
a.y = a.y + 1
#如果菜鳥碰到戰機,則將菜鳥移除,同時戰機損失一條生命
if a.colliderect(plane):
bird.remove(a)
life = life - 1
if life == 0:
state = "over"
#如果菜鳥移出屏幕,則移除菜鳥
if a.top > HEIGHT:
bird.remove(a)
#幸運補給箱移動
def move_lucky():
global kind
lucky.y = lucky.y + 5
if lucky.top > HEIGHT:
lucky.pos = [1000, 1000]
if lucky.colliderect(plane):
kind = "4"
lucky.pos = [1000, 1000]
sounds.lucky.play()
#角色自動連續移動、按鍵控制連續移動
def update():
#在運行狀態下才移動背景、菜鳥、子彈、戰機等
if state == "run":
move_bg()
move_bullet()
move_bird()
move_lucky()
me.pos = plane.pos
if keyboard.left == True:
if plane.left > 0:
plane.x = plane.x - 2
if keyboard.right == True:
if plane.right < WIDTH:
plane.x = plane.x + 2
#一鍵重玩(拓展、不講)
def on_mouse_down(pos):
global state, life, score, bullet, bird, num, kind
if again.collidepoint(pos):
state = "ready"
life = 3
score = 0
bullet = []
bird = []
kind = "1"
num = 0
music.play("bg.mp3")
go()
pandas Get the time characteri
author | pythonic Biological m