"""
植物大战僵尸 - 精简版
"""

import pygame
import random
import math
import sys
import os

pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 1400, 900
FPS = 60
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("植物大战僵尸")
clock = pygame.time.Clock()

# 颜色
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (220,50,50)
GREEN = (76,176,50)
YELLOW = (255,235,50)
ORANGE = (255,166,0)
GRAY = (120,120,120)
DKGRAY = (60,60,60)

# 网格
ROWS, COLS = 8, 10
CW, CH = 90, 95
GX, GY = 195, 78

# 字体
def get_font(size):
    paths = ["C:/Windows/Fonts/msyh.ttc","C:/Windows/Fonts/simhei.ttf"]
    for p in paths:
        if os.path.exists(p):
            return pygame.font.Font(p, size)
    return pygame.font.Font(None, size)

font18 = get_font(18)
font22 = get_font(22)
font36 = get_font(36)

# 工具函数
def cell_center(r,c):
    return GX + c*CW + CW//2, GY + r*CH + CH//2

def cell_from_pos(x,y):
    c = (x-GX)//CW
    r = (y-GY)//CH
    if 0<=r<ROWS and 0<=c<COLS:
        return r,c
    return None,None

def draw_text(surf,text,pos,color=WHITE,font=font22,center=True):
    ts = font.render(text,True,color)
    tr = ts.get_rect(center=pos) if center else ts.get_rect(topleft=pos)
    surf.blit(ts,tr)

# 粒子系统
class Particle:
    def __init__(self,x,y,vx,vy,color,life):
        self.x,self.y=x,y
        self.vx,self.vy=vx,vy
        self.color=color
        self.life=life
        self.max_life=life
    def update(self):
        self.x+=self.vx
        self.y+=self.vy
        self.vy+=0.08
        self.life-=1
    def draw(self,surf):
        a=int(255*self.life/self.max_life)
        tmp=pygame.Surface((8,8),pygame.SRCALPHA)
        pygame.draw.circle(tmp,(*self.color[:3],a),(4,4),4)
        surf.blit(tmp,(int(self.x-4),int(self.y-4)))

class Particles:
    def __init__(self):
        self.list=[]
    def burst(self,x,y,color,n=10):
        for _ in range(n):
            ang=random.uniform(0,2*math.pi)
            spd=random.uniform(1,4)
            self.list.append(Particle(x,y,math.cos(ang)*spd,math.sin(ang)*spd,color,random.randint(15,35)))
    def update(self):
        self.list=[p for p in self.list if p.life>0]
        for p in self.list:p.update()
    def draw(self,surf):
        for p in self.list:p.draw(surf)

parts = Particles()

# 植物
class Plant:
    def __init__(self,typ,r,c):
        self.typ=typ
        self.r,self.c=r,c
        self.x,self.y=cell_center(r,c)
        self.alive=True
        self.hp={"sun":60,"pea":100,"nut":400,"bomb":100,"ice":100,"double":100,"chomp":150,"mine":50,"fire":100}[typ]
        self.max_hp=self.hp
        self.timer=0
        self.shoot_timer=0
        self.sun_timer=0
        self.fuse_t=2500 if typ=="bomb" else 0
        self.armed=False
        self.arm_t=14000 if typ=="mine" else 0
        self.chewing=False
        self.chew_t=0
        self.shots=0
        self.burst_t=0
        
    def update(self,dt,game):
        self.timer+=dt
        if self.typ=="sun":
            self.sun_timer+=dt
            if self.sun_timer>=8000:
                self.sun_timer=0
                game.add_sun(self.x+random.randint(-20,20),self.y-30,True)
        elif self.typ=="pea":
            self.shoot_timer+=dt
            if self.shoot_timer>=1500 and game.has_zombie(self.r,self.x):
                self.shoot_timer=0
                game.add_bullet(self.x+20,self.y-10,self.r,20,"normal")
        elif self.typ=="ice":
            self.shoot_timer+=dt
            if self.shoot_timer>=1600 and game.has_zombie(self.r,self.x):
                self.shoot_timer=0
                game.add_bullet(self.x+20,self.y-10,self.r,20,"ice")
        elif self.typ=="double":
            self.shoot_timer+=dt
            if self.shoot_timer>=1400 and game.has_zombie(self.r,self.x):
                self.shoot_timer=0
                self.shots=2
            if self.shots>0:
                self.burst_t+=dt
                if self.burst_t>=200:
                    self.burst_t=0
                    self.shots-=1
                    game.add_bullet(self.x+20,self.y-10,self.r,20,"normal")
        elif self.typ=="bomb":
            self.fuse_t-=dt
            if self.fuse_t<=0:
                game.explode(self.x,self.y,300,1.5)
                self.alive=False
        elif self.typ=="mine":
            if not self.armed:
                self.arm_t-=dt
                if self.arm_t<=0:self.armed=True
            else:
                z=game.get_zombie_at(self.r,self.c)
                if z:
                    game.explode(self.x,self.y,400,1.2)
                    self.alive=False
        elif self.typ=="chomp":
            if self.chewing:
                self.chew_t-=dt
                if self.chew_t<=0:self.chewing=False
            else:
                z=game.get_zombie_near(self.r,self.x)
                if z and z.x-self.x<90:
                    z.hp-=999
                    z.alive=False
                    self.chewing=True
                    self.chew_t=4000
                    
    def hurt(self,dmg):
        self.hp-=dmg
        if self.hp<=0:self.alive=False
        
    def draw(self,surf):
        if not self.alive:return
        # 阴影
        pygame.draw.ellipse(surf,(0,0,0,40),(self.x-18,self.y+28,36,8))
        
        if self.typ=="pea":
            pygame.draw.rect(surf,(50,139,40),(self.x-4,self.y-10,8,28))
            hx=self.x+int(3*math.sin(self.timer*0.01))
            pygame.draw.circle(surf,(40,180,40),(hx+8,self.y-18),14)
            pygame.draw.circle(surf,(30,118,30),(hx+8,self.y-18),14,2)
            pygame.draw.ellipse(surf,(30,79,30),(hx+14,self.y-22,10,8))
            pygame.draw.circle(surf,WHITE,(hx+12,self.y-22),3)
            pygame.draw.circle(surf,BLACK,(hx+13,self.y-22),1)
        elif self.typ=="sun":
            pygame.draw.rect(surf,(50,139,40),(self.x-3,self.y-8,6,30))
            hy=self.y-12+int(2*math.sin(self.timer*0.008))
            for ang in range(0,360,45):
                px=self.x+int(18*math.cos(math.radians(ang)))
                py=hy+int(18*math.sin(math.radians(ang)))
                pygame.draw.circle(surf,YELLOW,(px,py),7)
            pygame.draw.circle(surf,(120,80,20),(self.x,hy),8)
        elif self.typ=="nut":
            by=self.y+int(2*math.sin(self.timer*0.01))
            pygame.draw.ellipse(surf,(180,120,60),(self.x-18,by-20,36,42))
            pygame.draw.ellipse(surf,(200,150,80),(self.x-14,by-16,28,34))
            pygame.draw.circle(surf,WHITE,(self.x-6,by-5),4)
            pygame.draw.circle(surf,WHITE,(self.x+6,by-5),4)
            pygame.draw.circle(surf,BLACK,(self.x-5,by-4),2)
            pygame.draw.circle(surf,BLACK,(self.x+7,by-4),2)
        elif self.typ=="bomb":
            bob=int(3*math.sin(self.timer*0.02))
            pygame.draw.circle(surf,RED,(self.x-8,self.y-10+bob),12)
            pygame.draw.circle(surf,(200,40,40),(self.x+8,self.y-6-bob),12)
            if self.fuse_t<500:
                for _ in range(3):
                    fx=self.x+random.randint(-15,15)
                    fy=self.y-25+random.randint(-5,5)
                    pygame.draw.circle(surf,YELLOW,(fx,fy),random.randint(1,3))
        elif self.typ=="ice":
            pygame.draw.rect(surf,(40,120,100),(self.x-4,self.y-10,8,28))
            hx=self.x+int(3*math.sin(self.timer*0.01))
            pts=[(hx+8,self.y-28),(hx+18,self.y-16),(hx+2,self.y-10)]
            pygame.draw.polygon(surf,(50,200,220),pts)
            pygame.draw.circle(surf,WHITE,(hx+10,self.y-18),3)
        elif self.typ=="double":
            pygame.draw.rect(surf,(40,130,50),(self.x-5,self.y-10,10,26))
            hx=self.x+int(3*math.sin(self.timer*0.01))
            pygame.draw.rect(surf,(60,200,60),(hx+5,self.y-25,8,17))
            pygame.draw.rect(surf,(60,200,60),(hx+13,self.y-22,8,17))
        elif self.typ=="chomp":
            pygame.draw.rect(surf,(100,50,100),(self.x-4,self.y-8,8,28))
            mo=0.3+0.2*math.sin(self.timer*0.015) if not self.chewing else 0.7
            ty=self.y-25
            by=self.y-10+int(15*mo)
            pygame.draw.ellipse(surf,(180,80,180),(self.x-16,ty-5,32,19))
            pygame.draw.ellipse(surf,(160,60,160),(self.x-16,by-5,32,19))
            pygame.draw.circle(surf,WHITE,(self.x-8,ty-2),4)
            pygame.draw.circle(surf,WHITE,(self.x+8,ty-2),4)
            pygame.draw.circle(surf,RED,(self.x-7,ty-1),2)
            pygame.draw.circle(surf,RED,(self.x+9,ty-1),2)
        elif self.typ=="mine":
            bob=int(2*math.sin(self.timer*0.012))
            if self.armed:
                pygame.draw.circle(surf,RED,(self.x,self.y-5+bob),16)
                if int(self.timer*0.001)%2==0:
                    pygame.draw.circle(surf,YELLOW,(self.x,self.y-5+bob),18,2)
            else:
                pygame.draw.ellipse(surf,(160,140,80),(self.x-12,self.y-12+bob,24,16))
        elif self.typ=="fire":
            pygame.draw.rect(surf,(120,70,30),(self.x-14,self.y-20,28,43))
            fh=15+int(5*math.sin(self.timer*0.018))
            for i in range(3):
                fx=self.x-6+i*6
                pygame.draw.polygon(surf,ORANGE,[(fx,self.y-20),(fx+3,self.y-20-fh),(fx+6,self.y-20)])
                pygame.draw.polygon(surf,YELLOW,[(fx+1,self.y-20),(fx+3,self.y-20-fh*0.66),(fx+5,self.y-20)])
        
        # 血条
        if self.hp<self.max_hp:
            bw,bh=36,4
            r=max(0,self.hp/self.max_hp)
            pygame.draw.rect(surf,DKGRAY,(self.x-bw//2,self.y-36,bw,bh))
            c=GREEN if r>0.5 else(YELLOW if r>0.25 else RED)
            pygame.draw.rect(surf,c,(self.x-bw//2,self.y-36,int(bw*r),bh))

# 僵尸
class Zombie:
    def __init__(self,typ,r):
        self.typ=typ
        self.r=r
        self.x=WIDTH+50
        self.y=GY+r*CH+CH//2+10
        self.alive=True
        self.eating=False
        self.slow_t=0
        self.anim_t=0
        self.frame=0
        if typ==0:
            self.hp,self.max_hp,self.spd,self.dmg=100,100,0.09,0.035
            self.color=(75,136,58)
        elif typ==1:
            self.hp,self.max_hp,self.spd,self.dmg=210,210,0.080,0.033
            self.color=(78,138,57)
        elif typ==2:
            self.hp,self.max_hp,self.spd,self.dmg=370,370,0.070,0.030
            self.color=(79,137,59)
        elif typ==3:
            self.hp,self.max_hp,self.spd,self.dmg=80,80,0.150,0.020
            self.color=(98,158,69)
        elif typ==4:
            self.hp,self.max_hp,self.spd,self.dmg=610,610,0.050,0.090
            self.color=(62,122,49)
            
    def update(self,dt,game):
        self.anim_t+=dt
        if self.anim_t>150:
            self.frame=(self.frame+1)%4
            self.anim_t=0
        sm=0.45 if self.slow_t>0 else 1.0
        if self.slow_t>0:self.slow_t-=dt
        p=game.get_plant(self.r,self.x)
        if p and p.alive:
            self.eating=True
            p.hurt(self.dmg*dt*0.060)
        else:
            self.eating=False
            self.x-=self.spd*sm*dt*0.055
        if self.x<GX-54:
            game.game_over=True
            self.alive=False
        if self.hp<=0:
            self.alive=False
            game.kill_zombie(self)
            
    def hurt(self,dmg,slow=False):
        self.hp-=dmg
        if slow:self.slow_t=3000
        
    def draw(self,surf):
        if not self.alive:return
        t=self.frame
        wl=int(3*math.sin(t*math.pi/2))
        pygame.draw.ellipse(surf,(0,0,0,50),(self.x-16,self.y+27,32,9))
        pygame.draw.ellipse(surf,(81,108,56),(self.x-13,self.y-20+wl,25,29))
        hx=self.x+wl//2
        pygame.draw.circle(surf,self.color,(hx,self.y-28),12)
        pygame.draw.circle(surf,WHITE,(hx-4,self.y-28),3)
        pygame.draw.circle(surf,WHITE,(hx+4,self.y-28),3)
        pygame.draw.circle(surf,RED,(hx-4,self.y-28),1)
        pygame.draw.circle(surf,RED,(hx+4,self.y-28),1)
        lo=int(5*math.sin(t*math.pi/2))
        pygame.draw.rect(surf,(49,49,49),(self.x-9,self.y+5,6,19+lo))
        pygame.draw.rect(surf,(49,49,49),(self.x+4,self.y+5,6,19-lo))
        if self.typ==1 and self.hp>106:
            pygame.draw.polygon(surf,ORANGE,[(hx,self.y-48),(hx-9,self.y-36),(hx+9,self.y-36)])
        if self.typ==2 and self.hp>212:
            pygame.draw.rect(surf,GRAY,(hx-10,self.y-40,20,8))
        if self.hp<self.max_hp:
            bw,bh=36,4
            r=max(0,self.hp/self.max_hp)
            pygame.draw.rect(surf,DKGRAY,(self.x-bw//2,self.y-46,bw,bh))
            c=GREEN if r>0.5 else(YELLOW if r>0.25 else RED)
            pygame.draw.rect(surf,c,(self.x-bw//2,self.y-46,int(bw*r),bh))

# 子弹
class Bullet:
    def __init__(self,x,y,r,dmg,typ):
        self.x,self.y,self.r=x,y,r
        self.dmg,self.typ=dmg,typ
        self.alive,self.spd=True,0.80
    def update(self,dt,game):
        self.x+=self.spd*dt*0.056
        for z in game.zombies:
            if z.alive and z.r==self.r and abs(z.x-self.x)<25 and abs(z.y-self.y)<40:
                z.hurt(self.dmg,slow=self.typ=="ice")
                parts.burst(self.x,self.y,YELLOW,5)
                self.alive=False
                break
        if self.x>WIDTH+20:self.alive=False
    def draw(self,surf):
        if self.typ=="normal":
            pygame.draw.circle(surf,(80,200,50),(int(self.x),int(self.y)),5)
            pygame.draw.circle(surf,(120,230,80),(int(self.x)-1,int(self.y)-1),3)
        else:
            pygame.draw.circle(surf,(150,220,255),(int(self.x),int(self.y)),6)
            pygame.draw.circle(surf,WHITE,(int(self.x)-1,int(self.y)-1),3)

# 阳光
class Sun:
    def __init__(self,x,y,val=25,fall=False):
        self.x,self.y=x,y
        self.val=val
        self.alive=True
        self.fall=fall
        self.ty=y+random.randint(50,150) if fall else y
        self.life=10000
        self.t=0
    def update(self,dt,game):
        self.t+=dt
        ho=int(3*math.sin(self.t*0.005))
        if self.fall and self.y<self.ty:
            self.y+=0.3*dt*0.052
        self.life-=dt
        if self.life<=0:self.alive=False
        mx,my=pygame.mouse.get_pos()
        if abs(mx-self.x)<22 and abs(my-(self.y+ho))<22:
            if pygame.mouse.get_pressed()[0]:
                game.sun+=self.val
                self.alive=False
    def draw(self,surf):
        s=18
        ho=int(3*math.sin(self.t*0.005))
        for ang in range(0,360,30):
            ex=self.x+int((s+6)*math.cos(math.radians(ang)))
            ey=self.y+ho+int((s+6)*math.sin(math.radians(ang)))
            sx=self.x+int(s*0.6*math.cos(math.radians(ang)))
            sy=self.y+ho+int(s*0.6*math.sin(math.radians(ang)))
            pygame.draw.line(surf,(255,230,80),(sx,sy),(ex,ey),2)
        pygame.draw.circle(surf,(255,220,30),(self.x,self.y+ho),s)
        pygame.draw.circle(surf,(255,240,100),(self.x-s//4,self.y+ho-s//4),s//3)

# 卡片
class Card:
    def __init__(self,typ,x,y,cost,name):
        self.typ=typ
        self.x,self.y=x,y
        self.w,self.h=70,85
        self.cost=cost
        self.name=name
        self.cd_t=0
        self.cd=5000
        self.sel=False
        self.ok=True
    def update(self,dt,sun):
        if self.cd_t>0:self.cd_t-=dt
        self.ok=sun>=self.cost and self.cd_t<=0
    def draw(self,surf):
        rect=pygame.Rect(self.x,self.y,self.w,self.h)
        if self.sel:bg,bc=(80,80,120),YELLOW
        elif self.ok:bg,bc=(60,60,80),GREEN
        else:bg,bc=(40,40,50),GRAY
        pygame.draw.rect(surf,bg,rect,border_radius=8)
        pygame.draw.rect(surf,bc,rect,2,border_radius=8)
        pygame.draw.rect(surf,(30,30,40),(self.x+4,self.y+4,self.w-8,52),border_radius=5)
        draw_text(surf,self.name[:2],(self.x+self.w//2,self.y+12),WHITE,font18)
        draw_text(surf,str(self.cost),(self.x+self.w//2,self.y+62),YELLOW if self.ok else GRAY,font18)
        if self.cd_t>0:
            r=self.cd_t/self.cd
            m=pygame.Surface((self.w-8,int(52*r)),pygame.SRCALPHA)
            m.fill((0,0,0,180))
            surf.blit(m,(self.x+4,self.y+4))
    def click(self,x,y):
        return self.x<=x<=self.x+self.w and self.y<=y<=self.y+self.h
    def start_cd(self):
        self.cd_t=self.cd

# 游戏主类
class Game:
    def __init__(self):
        self.sun=200
        self.score=0
        self.kills=0
        self.wave=1
        self.game_over=False
        self.plants=[]
        self.zombies=[]
        self.bullets=[]
        self.suns=[]
        self.grid=[[None]*COLS for _ in range(ROWS)]
        self.sel_card=None
        self.z_timer=0
        self.z_int=6000
        self.s_timer=0
        # 卡片
        self.cards=[]
        cards_data=[
            ("sun","向日葵",50),
            ("pea","豌豆射手",100),
            ("nut","坚果墙",50),
            ("bomb","樱桃炸弹",150),
            ("ice","寒冰射手",175),
            ("double","双发射手",200),
            ("chomp","食人花",150),
            ("mine","土豆雷",25),
            ("fire","火炬树桩",175),
        ]
        for i,(typ,name,cost) in enumerate(cards_data):
            self.cards.append(Card(typ,10,85+i*88,cost,name))
            
    def has_zombie(self,r,x=None):
        for z in self.zombies:
            if z.alive and z.r==r:
                if x is None or z.x>x-200:
                    return True
        return False
        
    def get_plant(self,r,x):
        for p in self.plants:
            if p.alive and p.r==r and abs(p.x-x)<40:
                return p
        return None
        
    def get_zombie_at(self,r,c):
        cx,_=cell_center(r,c)
        for z in self.zombies:
            if z.alive and z.r==r and abs(z.x-cx)<40:
                return z
        return None
        
    def get_zombie_near(self,r,x):
        for z in self.zombies:
            if z.alive and z.r==r and z.x>x and z.x-x<120:
                return z
        return None
        
    def add_sun(self,x,y,fall=False):
        self.suns.append(Sun(x,y,fall=fall))
        
    def add_bullet(self,x,y,r,dmg,typ):
        self.bullets.append(Bullet(x,y,r,dmg,typ))
        
    def explode(self,x,y,dmg,r):
        for z in self.zombies:
            if z.alive and abs(z.x-x)<r*80 and abs(z.y-y)<r*60:
                z.hp-=dmg
                if z.hp<=0:
                    z.alive=False
                    self.kill_zombie(z)
        parts.burst(x,y,(255,100,0),30)
        
    def kill_zombie(self,z):
        self.kills+=1
        self.score+=10
        parts.burst(z.x,z.y,(100,200,100),15)
        
    def update(self,dt):
        if self.game_over:return
        for c in self.cards:c.update(dt,self.sun)
        for p in self.plants[:]:
            p.update(dt,self)
            if not p.alive:
                self.plants.remove(p)
                self.grid[p.r][p.c]=None
        for z in self.zombies[:]:
            z.update(dt,self)
            if not z.alive:self.zombies.remove(z)
        for b in self.bullets[:]:
            b.update(dt,self)
            if not b.alive:self.bullets.remove(b)
        for s in self.suns[:]:
            s.update(dt,self)
            if not s.alive:self.suns.remove(s)
        parts.update()
        # 自动掉阳光
        self.s_timer+=dt
        if self.s_timer>=8000:
            self.s_timer=0
            x=random.randint(GX,GX+COLS*CW)
            self.add_sun(x,GY-50,True)
        # 刷僵尸
        self.z_timer+=dt
        if self.z_timer>=self.z_int:
            self.z_timer=0
            r=random.randint(0,ROWS-1)
            types=[0,0,1,3]
            if self.wave>=3:types+=[2,4]
            self.zombies.append(Zombie(random.choice(types),r))
            if self.z_int>3000:self.z_int-=100
            
    def handle(self,event):
        if event.type==pygame.MOUSEBUTTONDOWN:
            mx,my=pygame.mouse.get_pos()
            for c in self.cards:
                if c.click(mx,my):
                    if c.ok:
                        self.sel_card=c
                        c.sel=True
                    break
            else:
                for s in self.suns:
                    ho=int(3*math.sin(s.t*0.005))
                    if abs(mx-s.x)<22 and abs(my-(s.y+ho))<22:
                        self.sun+=s.val
                        s.alive=False
                        break
                else:
                    if self.sel_card:
                        r,c=cell_from_pos(mx,my)
                        if r is not None and self.grid[r][c] is None:
                            self.sun-=self.sel_card.cost
                            p=Plant(self.sel_card.typ,r,c)
                            self.plants.append(p)
                            self.grid[r][c]=p
                            self.sel_card.start_cd()
                        self.sel_card.sel=False
                        self.sel_card=None
                        
    def draw(self):
        screen.fill((0,0,0))
        # 草地
        for c in range(COLS):
            for r in range(ROWS):
                x,y=GX+c*CW,GY+r*CH
                color=(85,160,55) if (c+r)%2==0 else (75,145,45)
                pygame.draw.rect(screen,color,(x,y,CW,CH))
        # 实体
        for p in self.plants:p.draw(screen)
        for b in self.bullets:b.draw(screen)
        for z in self.zombies:z.draw(screen)
        for s in self.suns:s.draw(screen)
        parts.draw(screen)
        # UI
        pygame.draw.rect(screen,(50,50,60),(0,0,WIDTH,78))
        draw_text(screen,f"☀ {self.sun}",(WIDTH-140,12),YELLOW,font22)
        draw_text(screen,f"得分:{self.score}",(WIDTH-140,42),WHITE,font18)
        draw_text(screen,f"第{self.wave}波",(WIDTH//2,15),YELLOW,font36)
        for c in self.cards:c.draw(screen)
        if self.sel_card:
            mx,my=pygame.mouse.get_pos()
            pygame.draw.rect(screen,(255,255,255,40),(mx-45,my-48,CW,CH),2)
        # 游戏结束
        if self.game_over:
            ov=pygame.Surface((WIDTH,HEIGHT),pygame.SRCALPHA)
            ov.fill((0,0,0,200))
            screen.blit(ov,(0,0))
            draw_text(screen,"游戏结束",(WIDTH//2,HEIGHT//2-50),RED,font36)
            draw_text(screen,f"最终得分:{self.score}",(WIDTH//2,HEIGHT//2+10),WHITE,font22)
            draw_text(screen,"按R重新开始",(WIDTH//2,HEIGHT//2+60),YELLOW,font22)
        pygame.display.flip()
        
    def run(self):
        running=True
        while running:
            dt=clock.tick(FPS)
            for event in pygame.event.get():
                if event.type==pygame.QUIT:
                    running=False
                elif event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_r and self.game_over:
                        self.__init__()
                else:
                    self.handle(event)
            self.update(dt)
            self.draw()
        pygame.quit()
        sys.exit()

# 启动
if __name__=="__main__":
    Game().run()