import pygame
import random
import sys
import math

pygame.init()

# 像素画布设置
INTERNAL_W, INTERNAL_H = 320, 200
SCALE = 2
WIDTH, HEIGHT = INTERNAL_W * SCALE, INTERNAL_H * SCALE
TILE = 8  # 每个地图字符对应8像素
FPS = 60

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("🟢 史莱姆穿越异世界 - 纯像素版")
clock = pygame.time.Clock()
canvas = pygame.Surface((INTERNAL_W, INTERNAL_H))

# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
SKY_BLUE = (100, 160, 255)
DARK_BLUE = (30, 30, 80)
PURPLE = (120, 40, 180)
GREEN = (60, 200, 80)
DARK_GREEN = (30, 120, 40)
BROWN = (139, 90, 43)
DARK_BROWN = (80, 50, 20)
RED = (220, 50, 50)
ORANGE = (240, 150, 30)
YELLOW = (255, 230, 50)
CYAN = (80, 220, 255)
GRAY = (150, 150, 150)
DARK_GRAY = (60, 60, 60)
GOLD = (255, 200, 50)
PINK = (255, 130, 180)

# 字体
def init_fonts():
    paths = [
        "C:/Windows/Fonts/msyh.ttc",
        "C:/Windows/Fonts/simhei.ttf",
        "/System/Library/Fonts/PingFang.ttc",
        "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc"
    ]
    for p in paths:
        try:
            f_large = pygame.font.Font(p, 16)
            f_mid = pygame.font.Font(p, 10)
            f_small = pygame.font.Font(p, 8)
            print("✅ 中文字体加载成功:", p)
            return f_large, f_mid, f_small
        except:
            continue
    print("⚠️ 未找到中文字体，中文将显示为英文")
    return pygame.font.Font(None, 28), pygame.font.Font(None, 16), pygame.font.Font(None, 12)

font_large, font_mid, font_small = init_fonts()

TEXT_MAP = {
    "🟢 史莱姆穿越异世界": "Slime Adventure",
    "像素横版冒险": "Pixel Platformer",
    "按 ENTER 开始冒险": "Press ENTER to Start",
    "← → / A D  移动": "← → / A D Move",
    "↑ / W / 空格  跳跃（可二段跳）": "↑ / W / SPACE Jump",
    "Shift  冲刺": "Shift Dash",
    "💀 史莱姆被消灭了...": "Slime Died...",
    "按 ENTER 重试本关": "ENTER Retry",
    "按 ESC 返回标题": "ESC Back to Title",
    "🎉 异世界征服完成！": "Victory!",
    "⭐ 总计收集": "Total Stars",
    "HP": "HP",
    "🌿 绿色草原": "Green Grassland",
    "🦇 暗夜洞穴": "Dark Cave",
    "🔥 熔岩地狱": "Lava Hell",
    "← → 移动 | ↑/W 跳跃 | Shift 冲刺": "Move Jump Dash"
}

def render_text(text, color):
    try:
        return font_mid.render(text, True, color)
    except:
        return font_mid.render(TEXT_MAP.get(text, text), True, color)

# 像素绘制函数
def draw_pixel_slime(surface, x, y, size, color, frame=0):
    s = size
    bounce = int(math.sin(frame * 0.15))
    # 身体（矩形像素块）
    pygame.draw.rect(surface, color, (x - s//2, y - s//2 + bounce, s, s-2))
    # 高光
    pygame.draw.rect(surface, tuple(min(c+50,255) for c in color),
                     (x - s//4, y - s//3 + bounce, s//4, s//5))
    # 眼睛
    pygame.draw.rect(surface, WHITE, (x - s//5, y - s//6 + bounce, s//6, s//6))
    pygame.draw.rect(surface, WHITE, (x + s//10, y - s//6 + bounce, s//6, s//6))
    pygame.draw.rect(surface, BLACK, (x - s//6, y - s//6 + 1 + bounce, s//10, s//10))
    pygame.draw.rect(surface, BLACK, (x + s//8, y - s//6 + 1 + bounce, s//10, s//10))

def draw_pixel_star(surface, x, y, size, color):
    # 简化为一个像素菱形
    for i in range(size):
        pygame.draw.rect(surface, color, (x - i, y - size + i, 2*i+1, 1))
    pygame.draw.rect(surface, color, (x - size, y, size*2+1, 1))
    for i in range(size):
        pygame.draw.rect(surface, color, (x - size + i, y + i+1, 2*(size-i)-1, 1))

def draw_portal(surface, x, y, frame):
    for i in range(3):
        r = 6 - i*2
        color = (150+i*30, 60+i*40, 255)
        pygame.draw.ellipse(surface, color, (x-r, y-r-3+math.sin(frame*0.08+i)*1, r*2, r*2+3), 1)
    pygame.draw.circle(surface, WHITE, (x, y-2), 1)

class Particle:
    def __init__(self, x, y, color, vx=0, vy=0, life=20):
        self.x, self.y = x, y
        self.color = color
        self.vx = vx + random.uniform(-0.5,0.5)
        self.vy = vy + random.uniform(-1,0)
        self.life = life
        self.max_life = life

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += 0.03
        self.life -= 1

    def draw(self, surface, cam_x=0):
        if self.life > 0:
            # 像素粒子：1x1或2x2
            s = 2 if self.life > self.max_life//2 else 1
            pygame.draw.rect(surface, self.color, (int(self.x - cam_x), int(self.y), s, s))

class Slime:
    def __init__(self, x, y):
        self.x, self.y = x, y
        self.w, self.h = 6, 6
        self.vx = self.vy = 0
        self.on_ground = False
        self.hp = 3
        self.max_hp = 3
        self.invincible = 0
        self.frame = 0
        self.jump_count = 0
        self.max_jumps = 2
        self.color = GREEN
        self.alive = True
        self.dash_timer = 0

    def update(self, keys, tiles):
        self.frame += 1
        if self.invincible > 0:
            self.invincible -= 1

        speed = 1
        if self.dash_timer > 0:
            speed = 2
            self.dash_timer -= 1

        if keys[pygame.K_LEFT] or keys[pygame.K_a]:
            self.vx = -speed
        elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:
            self.vx = speed
        else:
            self.vx *= 0.7

        self.vy += 0.15
        if self.vy > 3:
            self.vy = 3

        self.x += self.vx
        self._collide_x(tiles)
        self.y += self.vy
        self.on_ground = False
        self._collide_y(tiles)

        if self.y > INTERNAL_H:
            self.alive = False

    def jump(self):
        if self.jump_count < self.max_jumps:
            self.vy = -4
            self.jump_count += 1
            return True
        return False

    def dash(self):
        if self.dash_timer == 0:
            self.dash_timer = 6
            self.vx = (1 if self.vx >= 0 else -1) * 4
            return True
        return False

    def _collide_x(self, tiles):
        rect = pygame.Rect(self.x - self.w//2, self.y - self.h//2, self.w, self.h)
        for t in tiles:
            if rect.colliderect(t):
                if self.vx > 0:
                    self.x = t.left - self.w//2
                elif self.vx < 0:
                    self.x = t.right + self.w//2
                self.vx = 0
                rect = pygame.Rect(self.x - self.w//2, self.y - self.h//2, self.w, self.h)

    def _collide_y(self, tiles):
        rect = pygame.Rect(self.x - self.w//2, self.y - self.h//2, self.w, self.h)
        for t in tiles:
            if rect.colliderect(t):
                if self.vy > 0:
                    self.y = t.top - self.h//2
                    self.on_ground = True
                    self.jump_count = 0
                elif self.vy < 0:
                    self.y = t.bottom + self.h//2
                self.vy = 0
                rect = pygame.Rect(self.x - self.w//2, self.y - self.h//2, self.w, self.h)

    def take_damage(self):
        if self.invincible <= 0:
            self.hp -= 1
            self.invincible = 20
            if self.hp <= 0:
                self.alive = False
            return True
        return False

    def draw(self, surface, cam_x):
        if self.invincible > 0 and self.invincible % 4 < 2:
            return
        draw_pixel_slime(surface, int(self.x - cam_x), int(self.y), self.w, self.color, self.frame)

class Enemy:
    def __init__(self, x, y, pl, pr):
        self.x, self.y = x, y
        self.w, self.h = 6, 6
        self.patrol_left, self.patrol_right = pl, pr
        self.speed = 0.5
        self.direction = 1
        self.frame = 0
        self.alive = True

    def update(self):
        self.frame += 1
        self.x += self.speed * self.direction
        if self.x >= self.patrol_right:
            self.direction = -1
        elif self.x <= self.patrol_left:
            self.direction = 1

    def draw(self, surface, cam_x):
        dx = int(self.x - cam_x)
        dy = int(self.y)
        # 像素蝙蝠
        wing = int(math.sin(self.frame*0.3))
        pygame.draw.rect(surface, PURPLE, (dx-3, dy-2+wing, 6, 4))
        pygame.draw.rect(surface, PURPLE, (dx-5, dy-1+wing, 2, 2))
        pygame.draw.rect(surface, PURPLE, (dx+3, dy-1+wing, 2, 2))
        pygame.draw.rect(surface, RED, (dx-1, dy-1+wing, 1, 1))
        pygame.draw.rect(surface, RED, (dx+1, dy-1+wing, 1, 1))

    def get_rect(self):
        return pygame.Rect(self.x - self.w//2, self.y - self.h//2, self.w, self.h)

class Spring:
    def __init__(self, x, y):
        self.x, self.y = x, y
        self.compressed = 0

    def draw(self, surface, cam_x):
        dx = int(self.x - cam_x)
        h = 4 - self.compressed*2
        pygame.draw.rect(surface, YELLOW, (dx-3, self.y-h, 6, h))
        pygame.draw.rect(surface, ORANGE, (dx-4, self.y-h-1, 8, 2))
        if self.compressed > 0:
            self.compressed -= 0.05

    def get_rect(self):
        return pygame.Rect(self.x-4, self.y-4, 8, 4)

LEVELS = [
    {
        "name": "🌿 绿色草原",
        "bg_top": (130,200,255),
        "bg_bottom": (200,230,255),
        "ground_color": DARK_GREEN,
        "dirt_color": BROWN,
        "map": [
            "0000000000000000000000000000000000000000",
            "0000000000000000000000000000000000000000",
            "0000000000000000000000000000000000000000",
            "0000000000000000000000000000000000000005",
            "0000000000000000000000000000000000000011",
            "0000000000000000000000220000000000000011",
            "0000000000000000000000000000000002200011",
            "0000000000000022000000000000000000000011",
            "0000000000000000000000000000220000000011",
            "0000000000000000000000000000000000000011",
            "1111111111111111111111111111111111111111",
        ],
        "stars": [(50,60),(100,50),(150,40),(250,60),(300,30),(350,60)],
        "enemies": [(130,77,110,160),(220,77,200,260)],
        "springs": [],
    },
    {
        "name": "🦇 暗夜洞穴",
        "bg_top": (20,15,50),
        "bg_bottom": (50,30,80),
        "ground_color": DARK_GRAY,
        "dirt_color": (40,35,60),
        "map": [
            "1111111111111111111111111111111111111111",
            "1000000000000000000000000000000000000001",
            "1000000000000000000000000000000000000001",
            "1000000000000000000000000000000000000051",
            "1000000000000000000000000000000000000011",
            "1000000000002200000000000000220000000011",
            "1000000000000000000000000000000000220011",
            "1000000000000000000022000000000000000011",
            "1000000000000000000000000000000000000011",
            "1000000000000000000000000000000000000011",
            "1111111111111111111111111111111111111111",
        ],
        "stars": [(50,60),(90,50),(150,90),(200,50),(280,60),(330,90)],
        "enemies": [(100,77,80,130),(180,77,160,220),(280,77,260,320)],
        "springs": [(80,76)],
    },
    {
        "name": "🔥 熔岩地狱",
        "bg_top": (80,20,10),
        "bg_bottom": (150,40,20),
        "ground_color": (100,60,40),
        "dirt_color": (60,30,20),
        "map": [
            "0000000000000000000000000000000000000000",
            "0000000000000000000000000000000000000000",
            "0000000000000000000000000000000000000000",
            "0000000000000000000000000000000000000005",
            "0000000000000000000000000000000000000011",
            "0000000000002200000000000000220000000011",
            "0000000000000000000000000000000000220011",
            "0000000000000000000022000000000000000011",
            "0000000000000000000000000000000000000011",
            "0000000000000000000000000000000000000011",
            "1111111100311111110031111111003111111111",
        ],
        "stars": [(40,60),(100,40),(160,80),(220,40),(280,60),(340,80)],
        "enemies": [(90,77,70,120),(170,77,150,220),(250,77,230,300),(330,77,310,370)],
        "springs": [(70,76),(240,76)],
    },
]

class Game:
    def __init__(self):
        self.state = "title"
        self.level_idx = 0
        self.total_stars = 0
        self.frame = 0
        self.particles = []
        self.transition_timer = 0
        self.shake = 0
        self.slime = None
        self.camera_x = 0
        self.enemies = []
        self.stars = []
        self.springs = []
        self.tiles = []
        self.portal_rect = None
        self.level_data = None

    def load_level(self, idx):
        self.level_idx = idx
        data = LEVELS[idx]
        self.level_data = data
        self.tiles = []
        self.enemies = []
        self.stars = []
        self.springs = []
        self.portal_rect = None

        for row_i, row in enumerate(data["map"]):
            for col_i, ch in enumerate(row):
                x = col_i * TILE
                y = row_i * TILE
                if ch == "1":
                    self.tiles.append(pygame.Rect(x, y, TILE, TILE))
                elif ch == "2":
                    self.tiles.append(pygame.Rect(x, y, TILE, TILE//2))
                elif ch == "3":
                    self.tiles.append(pygame.Rect(x, y+TILE//2, TILE, TILE//2))
                elif ch == "5":
                    self.portal_rect = pygame.Rect(x, y-5, TILE, TILE+5)

        for sx, sy in data["stars"]:
            self.stars.append({"x": sx, "y": sy, "collected": False})
        for ex, ey, pl, pr in data["enemies"]:
            self.enemies.append(Enemy(ex, ey, pl, pr))
        for sx, sy in data.get("springs", []):
            self.springs.append(Spring(sx, sy))

        self.slime = Slime(15, 60)
        self.camera_x = 0

    def spawn_particles(self, x, y, color, count=6):
        for _ in range(count):
            self.particles.append(Particle(x, y, color, random.uniform(-1,1), random.uniform(-1,0), 20))

    def update_playing(self):
        self.frame += 1
        keys = pygame.key.get_pressed()
        self.slime.update(keys, self.tiles)

        target_cam = self.slime.x - INTERNAL_W//3
        self.camera_x += (target_cam - self.camera_x)*0.1
        if self.camera_x < 0: self.camera_x = 0

        for e in self.enemies:
            if e.alive: e.update()

        s_rect = pygame.Rect(self.slime.x - self.slime.w//2, self.slime.y - self.slime.h//2, self.slime.w, self.slime.h)
        for e in self.enemies:
            if e.alive and s_rect.colliderect(e.get_rect()):
                if self.slime.vy > 0 and self.slime.y < e.y - 1:
                    e.alive = False
                    self.slime.vy = -2
                    self.spawn_particles(e.x, e.y, PURPLE, 8)
                else:
                    if self.slime.take_damage():
                        self.spawn_particles(self.slime.x, self.slime.y, RED, 5)
                        self.shake = 3
                        self.slime.vx = -self.slime.vx*2
                        self.slime.vy = -1

        for star in self.stars:
            if not star["collected"]:
                if math.hypot(self.slime.x-star["x"], self.slime.y-star["y"]) < 6:
                    star["collected"] = True
                    self.total_stars += 1
                    self.spawn_particles(star["x"], star["y"], GOLD, 6)

        for spring in self.springs:
            if s_rect.colliderect(spring.get_rect()):
                self.slime.vy = -3
                spring.compressed = 1
                self.spawn_particles(spring.x, spring.y, YELLOW, 3)

        if self.portal_rect and s_rect.colliderect(self.portal_rect):
            if self.level_idx < len(LEVELS)-1:
                self.state = "transition"
                self.transition_timer = 60
            else:
                self.state = "victory"

        if not self.slime.alive:
            self.state = "gameover"
            self.spawn_particles(self.slime.x, self.slime.y, GREEN, 10)

        for p in self.particles: p.update()
        self.particles = [p for p in self.particles if p.life > 0]
        if self.shake > 0: self.shake -= 1

    def draw_background(self):
        data = self.level_data
        for y in range(INTERNAL_H):
            ratio = y/INTERNAL_H
            r = int(data["bg_top"][0]*(1-ratio) + data["bg_bottom"][0]*ratio)
            g = int(data["bg_top"][1]*(1-ratio) + data["bg_bottom"][1]*ratio)
            b = int(data["bg_top"][2]*(1-ratio) + data["bg_bottom"][2]*ratio)
            pygame.draw.line(canvas, (r,g,b), (0,y), (INTERNAL_W,y))

    def draw_tiles(self):
        data = self.level_data
        for t in self.tiles:
            dx = t.x - self.camera_x
            if -TILE < dx < INTERNAL_W+TILE:
                if t.h < TILE:
                    pygame.draw.rect(canvas, GRAY, (dx, t.y, t.w, t.h))
                else:
                    pygame.draw.rect(canvas, data["ground_color"], (dx, t.y, t.w, t.h))
                    pygame.draw.rect(canvas, data["dirt_color"], (dx, t.y+2, t.w, t.h-2))

    def draw_playing(self):
        shake_x = random.randint(-self.shake, self.shake) if self.shake>0 else 0
        shake_y = random.randint(-self.shake, self.shake) if self.shake>0 else 0
        canvas.fill(BLACK)
        self.draw_background()
        self.draw_tiles()

        if self.portal_rect:
            draw_portal(canvas, int(self.portal_rect.centerx - self.camera_x), int(self.portal_rect.centery), self.frame)

        for star in self.stars:
            if not star["collected"]:
                draw_pixel_star(canvas, int(star["x"]-self.camera_x), int(star["y"]), 3, GOLD)

        for spring in self.springs: spring.draw(canvas, self.camera_x)
        for e in self.enemies:
            if e.alive: e.draw(canvas, self.camera_x)
        self.slime.draw(canvas, self.camera_x)
        for p in self.particles: p.draw(canvas, self.camera_x)

        # HUD
        pygame.draw.rect(canvas, DARK_GRAY, (4,4,40,6))
        hp_w = int(38*self.slime.hp/self.slime.max_hp)
        pygame.draw.rect(canvas, GREEN if self.slime.hp>1 else RED, (5,5,hp_w,4))
        star_text = font_small.render(f"⭐{self.total_stars}", True, GOLD)
        canvas.blit(star_text, (50,2))
        level_text = font_small.render(self.level_data["name"], True, WHITE)
        canvas.blit(level_text, (INTERNAL_W-level_text.get_width()-4,2))

        # 放大到屏幕
        screen.blit(pygame.transform.scale(canvas, (WIDTH, HEIGHT)), (0,0))
        if self.shake > 0:
            screen.scroll(shake_x, shake_y)

    def draw_title(self):
        self.frame += 1
        canvas.fill((20,10,40))
        # 星空
        for i in range(30):
            sx = (i*37 + self.frame*0.2) % INTERNAL_W
            sy = (i*23) % (INTERNAL_H-40)
            pygame.draw.rect(canvas, (200,200,200), (sx,sy,1,1))

        title = font_large.render("🟢 史莱姆穿越异世界", True, GREEN)
        canvas.blit(title, (INTERNAL_W//2-title.get_width()//2, 40))
        sub = font_mid.render("像素横版冒险", True, CYAN)
        canvas.blit(sub, (INTERNAL_W//2-sub.get_width()//2, 60))
        draw_pixel_slime(canvas, INTERNAL_W//2, 90, 12, GREEN, self.frame)

        if self.frame%60 < 40:
            start = font_mid.render("按 ENTER 开始冒险", True, YELLOW)
            canvas.blit(start, (INTERNAL_W//2-start.get_width()//2, 110))

        controls = ["← → / A D  移动", "↑ / W / 空格  跳跃", "Shift  冲刺"]
        for i,c in enumerate(controls):
            t = font_small.render(c, True, (180,180,200))
            canvas.blit(t, (INTERNAL_W//2-t.get_width()//2, 130+i*12))

        screen.blit(pygame.transform.scale(canvas, (WIDTH, HEIGHT)), (0,0))

    def draw_gameover(self):
        self.frame += 1
        canvas.fill((30,10,10))
        for p in self.particles:
            p.update()
            p.draw(canvas, 0)
        self.particles = [p for p in self.particles if p.life>0]

        text = font_large.render("💀 史莱姆被消灭了...", True, RED)
        canvas.blit(text, (INTERNAL_W//2-text.get_width()//2, 60))
        stars_text = font_mid.render(f"收集星星: {self.total_stars}", True, GOLD)
        canvas.blit(stars_text, (INTERNAL_W//2-stars_text.get_width()//2, 90))

        if self.frame%60 < 40:
            retry = font_mid.render("按 ENTER 重试本关", True, YELLOW)
            canvas.blit(retry, (INTERNAL_W//2-retry.get_width()//2, 120))
            back = font_small.render("按 ESC 返回标题", True, GRAY)
            canvas.blit(back, (INTERNAL_W//2-back.get_width()//2, 140))

        screen.blit(pygame.transform.scale(canvas, (WIDTH, HEIGHT)), (0,0))

    def draw_victory(self):
        self.frame += 1
        for y in range(INTERNAL_H):
            hue = (y/INTERNAL_H*360 + self.frame*2)%360
            r = int(128+127*math.sin(math.radians(hue)))
            g = int(128+127*math.sin(math.radians(hue+120)))
            b = int(128+127*math.sin(math.radians(hue+240)))
            pygame.draw.line(canvas, (r,g,b), (0,y), (INTERNAL_W,y))

        if self.frame%5 == 0:
            self.spawn_particles(random.randint(30,290), random.randint(20,80), random.choice([GOLD,CYAN,PINK]), 2)
        for p in self.particles:
            p.update()
            p.draw(canvas, 0)
        self.particles = [p for p in self.particles if p.life>0]

        text = font_large.render("🎉 异世界征服完成！", True, WHITE)
        canvas.blit(text, (INTERNAL_W//2-text.get_width()//2, 50))
        stars_text = font_mid.render(f"⭐ 总计收集: {self.total_stars} 颗星星", True, GOLD)
        canvas.blit(stars_text, (INTERNAL_W//2-stars_text.get_width()//2, 80))
        draw_pixel_slime(canvas, INTERNAL_W//2, 120, 12, GREEN, self.frame)

        if self.frame%60 < 40:
            back = font_mid.render("按 ENTER 返回标题", True, YELLOW)
            canvas.blit(back, (INTERNAL_W//2-back.get_width()//2, 150))

        screen.blit(pygame.transform.scale(canvas, (WIDTH, HEIGHT)), (0,0))

    def draw_transition(self):
        self.transition_timer -= 1
        self.frame += 1
        self.draw_playing()
        alpha = int(255*(1 - self.transition_timer/60))
        overlay = pygame.Surface((INTERNAL_W, INTERNAL_H))
        overlay.fill(WHITE)
        overlay.set_alpha(alpha)
        canvas.blit(overlay, (0,0))
        screen.blit(pygame.transform.scale(canvas, (WIDTH, HEIGHT)), (0,0))
        if self.transition_timer <= 0:
            self.load_level(self.level_idx+1)
            self.state = "playing"

    def run(self):
        running = True
        while running:
            clock.tick(FPS)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                if event.type == pygame.KEYDOWN:
                    if self.state == "title" and event.key == pygame.K_RETURN:
                        self.total_stars = 0
                        self.load_level(0)
                        self.state = "playing"
                    elif self.state == "playing":
                        if event.key in (pygame.K_UP, pygame.K_w, pygame.K_SPACE):
                            if self.slime.jump():
                                self.spawn_particles(self.slime.x, self.slime.y+3, CYAN, 3)
                        if event.key in (pygame.K_LSHIFT, pygame.K_RSHIFT):
                            if self.slime.dash():
                                self.spawn_particles(self.slime.x, self.slime.y, CYAN, 4)
                    elif self.state == "gameover":
                        if event.key == pygame.K_RETURN:
                            self.load_level(self.level_idx)
                            self.state = "playing"
                        elif event.key == pygame.K_ESCAPE:
                            self.state = "title"
                    elif self.state == "victory" and event.key == pygame.K_RETURN:
                        self.state = "title"

            if self.state == "title": self.draw_title()
            elif self.state == "playing": self.update_playing(); self.draw_playing()
            elif self.state == "gameover": self.draw_gameover()
            elif self.state == "victory": self.draw_victory()
            elif self.state == "transition": self.draw_transition()

            pygame.display.flip()
        pygame.quit()
        sys.exit()

if __name__ == "__main__":
    Game().run()