Python 太空入侵者:高分关键错误

Python 太空入侵者:高分关键错误,python,python-3.x,pygame,pycharm,Python,Python 3.x,Pygame,Pycharm,所以,我让我的程序正常工作,从上次开始,一切都很好,直到我不得不插入一个高分系统使用连接。我已经回答了之前提出的关于高分的一个问题: 由于该人询问的程序与我的想法非常吻合,我决定使用shelve模块保存score变量。在插入变量和模块并在Pycharm上重新格式化文件以查看是否一切正常之后。我得到这个错误(这是错误的前半部分): 现在,我认为上半场不是问题所在,就像我之前添加分数和搁置一样,它仍然显示了这一点,程序将按照我的意愿运行 因此,我认为这后半部分是主要问题: Traceback (mo

所以,我让我的程序正常工作,从上次开始,一切都很好,直到我不得不插入一个高分系统使用连接。我已经回答了之前提出的关于高分的一个问题:

由于该人询问的程序与我的想法非常吻合,我决定使用shelve模块保存score变量。在插入变量和模块并在Pycharm上重新格式化文件以查看是否一切正常之后。我得到这个错误(这是错误的前半部分):

现在,我认为上半场不是问题所在,就像我之前添加分数和搁置一样,它仍然显示了这一点,程序将按照我的意愿运行

因此,我认为这后半部分是主要问题:

Traceback (most recent call last):
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\lib\shelve.py", line 111, in __getitem__
    value = self.cache[key]
KeyError: 'score'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Dell/PycharmProjects/The Space Walker/main.py", line 482, in <module>
    start_screen()
  File "C:/Users/Dell/PycharmProjects/The Space Walker/main.py", line 471, in start_screen
    Score()
  File "C:/Users/Dell/PycharmProjects/The Space Walker/main.py", line 456, in Score
    score = max(d['score'])
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\lib\shelve.py", line 113, in __getitem__
    f = BytesIO(self.dict[key.encode(self.keyencoding)])
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\lib\dbm\dumb.py", line 147, in __getitem__
    pos, siz = self._index[key]     # may raise KeyError
KeyError: b'score'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\lib\shelve.py”,第111行,在\uu getitem中__
value=self.cache[key]
关键错误:“分数”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:/Users/Dell/PycharmProjects/The Space Walker/main.py”,第482行,在
启动屏幕()
文件“C:/Users/Dell/PycharmProjects/The Space Walker/main.py”,第471行,在启动屏幕中
分数()
文件“C:/Users/Dell/PycharmProjects/The Space Walker/main.py”,第456行,在Score中
分数=最大值(d['score'])
文件“C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\lib\shelve.py”,第113行,在\uu getitem中__
f=字节(self.dict[key.encode(self.keycodencing)])
文件“C:\Users\Dell\AppData\Local\Programs\Python38-32\lib\dbm\dumb.py”,第147行,在\uuu getitem中__
pos,siz=self._索引[键]#可能引发键错误
关键错误:b'score'
现在,我是一个初学者,我不明白为什么它告诉我,在一行有一个错误,这与分数无关。早些时候还不错! 第147行是关于激光的。我甚至不知道第113行的错误是什么。我该怎么办?我该如何解决这个问题

这是我的全部代码:

import random
import shelve

import pygame
from pygame import mixer

pygame.font.init()
pygame.init()

# THE SCREEN SIZE
WIDTH, HEIGHT = 800, 600
DIMENSIONS = pygame.display.set_mode((WIDTH, HEIGHT))

# THE GAME NAME
pygame.display.set_caption("The Space Walker")

# ENEMIES
REDEX = pygame.image.load("REDEX.png")
GENEX = pygame.image.load("GENEX.png")
BREX = pygame.image.load("BREX.png")
BOSS = pygame.image.load("Boss.png")

# THE PLAYER
player = pygame.image.load("BLShip.png")
THESPACEWALKER = pygame.image.load("SPACEWALKER.png")

# ENEMY LASERS
REDEXASER = pygame.image.load("REDEXASER.png")
GENEXASER = pygame.image.load("GENEXASER.png")
BREXASER = pygame.image.load("BREXASER.png")
BOSSXASER = pygame.image.load("BOSSXASER.png")

# PLAYER LASER
LAME = pygame.image.load("SPACEXASER.png")
NEOXASER = pygame.image.load("NEOXASER.png")

# GAME BACKGROUND
BG = pygame.transform.scale(pygame.image.load("background.png"), (WIDTH, HEIGHT))

# GAME MUSIC
music = pygame.mixer.music.load('background.mp3')
pygame.mixer.music.play(-1)


# FUNCTIONS BEING USED IN THE GAME

# PAUSE FUNCTION


def pause():
    paused = True

    while paused:
        pause_font = pygame.font.SysFont("freesansbold.ttf", 80)
        con_font = pygame.font.SysFont("freesansbold.ttf", 80)
        for event in pygame.event.get():

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_c:
                    paused = False

        pause_label = pause_font.render("Paused", 1, (255, 255, 255))
        DIMENSIONS.blit(pause_label, (WIDTH / 2 - pause_label.get_width() / 2, 250))
        con_label = con_font.render("C to Continue", 1, (255, 255, 255))
        DIMENSIONS.blit(con_label, (WIDTH / 2 - con_label.get_width() / 2, 300))
        pygame.mixer.pause()
        pygame.display.update()
        pygame.time.Clock()


class Laser:
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.mask = pygame.mask.from_surface(self.img)

    def draw(self, window):
        window.blit(self.img, (self.x, self.y))

    def move(self, vel):
        self.y += vel

    def off_screen(self, height):
        return not (height >= self.y >= 0)

    def collision(self, obj):
        return collide(self, obj)


class Ship:
    COOLDOWN = 30

    def __init__(self, x, y, health=100):
        self.x = x
        self.y = y
        self.health = health
        self.ship_img = None
        self.laser_img = None
        self.lasers = []
        self.cool_down_counter = 0

    def draw(self, window):
        window.blit(self.ship_img, (self.x, self.y))
        for laser in self.lasers:
            laser.draw(window)

    def move_lasers(self, vel, obj):
        self.cooldown()
        for laser in self.lasers:
            laser.move(vel)
            if laser.off_screen(HEIGHT):
                self.lasers.remove(laser)
            elif laser.collision(obj):
                obj.health -= 10
                self.lasers.remove(laser)

    def cooldown(self):
        if self.cool_down_counter >= self.COOLDOWN:
            self.cool_down_counter = 0
        elif self.cool_down_counter > 0:
            self.cool_down_counter += 1

    def shoot(self):
        if self.cool_down_counter == 0:
            laser = Laser(self.x - 17, self.y, self.laser_img)
            self.lasers.append(laser)
            self.cool_down_counter = 1
        lsound = mixer.Sound('Lfire.wav')
        lsound.play()

    def get_width(self):
        return self.ship_img.get_width()

    def get_height(self):
        return self.ship_img.get_height()


class Player(Ship):
    def __init__(self, x, y, health=100):
        super().__init__(x, y, health)
        self.ship_img = player
        self.laser_img = LAME
        self.mask = pygame.mask.from_surface(self.ship_img)
        self.max_health = health

    def move_lasers(self, vel, objs):
        self.cooldown()
        for laser in self.lasers:
            laser.move(vel)
            if laser.off_screen(HEIGHT):
                self.lasers.remove(laser)
            else:
                for obj in objs:
                    if laser.collision(obj):
                        colli = mixer.Sound('coll.wav')
                        colli.play()
                        obj.health -= 10
                        if laser in self.lasers:
                            self.lasers.remove(laser)

    def draw(self, window):
        super().draw(window)
        self.healthbar(window)

    def healthbar(self, window):
        pygame.draw.rect(window, (255, 0, 0),
                         (self.x, self.y + self.ship_img.get_height() + 10, self.ship_img.get_width(), 10))
        pygame.draw.rect(window, (0, 255, 0), (
            self.x, self.y + self.ship_img.get_height() + 10,
            self.ship_img.get_width() * (self.health / self.max_health),
            10))


class Enemy(Ship):
    COLOR_MAP = {
        "red": (REDEX, REDEXASER),
        "green": (GENEX, GENEXASER),
        "blue": (BREX, BREXASER),
        "black": (BOSS, BOSSXASER)
    }

    def __init__(self, x, y, color, health=10):
        super().__init__(x, y, health)
        self.ship_img, self.laser_img = self.COLOR_MAP[color]
        self.mask = pygame.mask.from_surface(self.ship_img)

    def move(self, vel):
        self.y += vel

    def shoot(self):
        if self.cool_down_counter == 0:
            laser = Laser(self.x - 20, self.y, self.laser_img)
            self.lasers.append(laser)
            self.cool_down_counter = 1
        if self.y > 0:
            lsound = mixer.Sound('Lfire.wav')
            lsound.play()


def collide(obj1, obj2):
    offset_x = int(obj2.x - obj1.x)
    offset_y = int(obj2.y - obj1.y)
    return obj1.mask.overlap(obj2.mask, (offset_x, offset_y)) is not None


def text_objects(text, font):
    textSurface = font.render(text, True, (0, 0, 0))
    return textSurface, textSurface.get_rect()


def main():
    run = True
    FPS = 60
    level = 0
    lives = 3
    crashed = 0
    # score = 0  # But do I need it since I'm making it as a dict?

    main_font = pygame.font.SysFont("freesansbold.ttf", 50)
    winc_font = pygame.font.SysFont("freesansbold.ttf", 70)
    lost_font = pygame.font.SysFont("freesansbold.ttf", 70)
    upgrad_font = pygame.font.SysFont("freesansbold.ttf", 30)

    enemies = []
    wave_length = 5
    enemy_vel = 1

    player_vel = 5
    laser_vel = 3

    player = Player(400 - 30, 500)

    clock = pygame.time.Clock()

    lost = False
    lost_count = 0

    winc = False
    winc_count = 0

    upgrad = False
    upgrad_count = 0

    upgraded = False
    upgraded_count = 0

    def redraw_window():
        DIMENSIONS.blit(BG, (0, 0))
        # draw text
        lives_label = main_font.render(f"Lives: {lives}", 1, (255, 255, 255))
        level_label = main_font.render(f"Level: {level}", 1, (255, 255, 255))
        crashed_label = main_font.render(f"Crashed: {crashed}", 1, (255, 255, 255))
        # score_label = main_font.render(f"Score: {score}", 1, (225, 225, 225)) # The score on-screen counter idea.
        # I've dropped the idea of having it on-screen for the entire game for now.

        DIMENSIONS.blit(lives_label, (10, 10))
        DIMENSIONS.blit(level_label, (WIDTH - level_label.get_width() - 10, 10))
        DIMENSIONS.blit(crashed_label, (20, 550))
        # DIMENSIONS.blit(score_label, (WIDTH - score_label.get_width() - 10, 550))  # Not using it for now.
        # Is 'score' required to be a variable here? If it's a dict. how do I do it?

        for enemy in enemies:
            enemy.draw(DIMENSIONS)

        player.draw(DIMENSIONS)

        if lost:
            pygame.mixer.music.pause()
            LoseSound = mixer.Sound('LoseSound.wav')
            LoseSound.play()
            lost_label = lost_font.render("GAME OVER", 1, (255, 255, 255))
            DIMENSIONS.blit(lost_label, (WIDTH / 2 - lost_label.get_width() / 2, 250))
            crashed_label = main_font.render(f"You crashed: {crashed} times", 1, (255, 60, 60))
            DIMENSIONS.blit(crashed_label, (WIDTH / 2 - crashed_label.get_width() / 2, 305))
            credit_label = main_font.render(f"Game by (Aaditya & Aaryan) Sharma", 1, (255, 255, 0))
            DIMENSIONS.blit(credit_label, (WIDTH / 2 - credit_label.get_width() / 2, 345))
            credit1_label = main_font.render(f"Thank you for playing The Space Walker!", 1, (255, 128, 0))
            DIMENSIONS.blit(credit1_label, (WIDTH / 2 - credit1_label.get_width() / 2, 385))
            score_label = main_font.render(f"Score: {score - crashed}", 1, (255, 60, 60))  # Total score calc.
            DIMENSIONS.blit(score_label, (WIDTH / 2 - score_label.get_width() / 2, 395))  # But again, this will be a
            # dictionary, so how will this work?

        if winc:
            pygame.mixer.music.pause()
            WinSound = mixer.Sound('WinSound.wav')
            WinSound.play()
            winc_label = winc_font.render("You Win!", 1, (255, 255, 255))
            DIMENSIONS.blit(winc_label, (WIDTH / 2 - winc_label.get_width() / 2, 250))
            crashed_label = main_font.render(f"You crashed: {crashed} times", 1, (255, 60, 60))
            DIMENSIONS.blit(crashed_label, (WIDTH / 2 - crashed_label.get_width() / 2, 305))
            credit_label = main_font.render(f"Game by (Aaditya & Aaryan) Sharma", 1, (255, 255, 0))
            DIMENSIONS.blit(credit_label, (WIDTH / 2 - credit_label.get_width() / 2, 345))
            credit1_label = main_font.render(f"Thank you for playing The Space Walker!", 1, (255, 128, 0))
            DIMENSIONS.blit(credit1_label, (WIDTH / 2 - credit1_label.get_width() / 2, 385))
            score_label = main_font.render(f"Score: {score - crashed}", 1, (255, 60, 60))  # Total score calc.
            DIMENSIONS.blit(score_label, (WIDTH / 2 - score_label.get_width() / 2, 395))  # Same problems as above.

        if upgrad:
            upgrad_label = upgrad_font.render("Your ship is being upgraded!", 1, (255, 255, 255))
            DIMENSIONS.blit(upgrad_label, (WIDTH / 2 - upgrad_label.get_width() / 2, 250))
            upgrad_label = upgrad_font.render("Survive this level!!", 1, (255, 255, 255))
            DIMENSIONS.blit(upgrad_label, (WIDTH / 2 - upgrad_label.get_width() / 2, 270))

        if upgraded:
            upgraded_label = winc_font.render("UPGRADE COMPLETED", 1, (255, 255, 255))
            DIMENSIONS.blit(upgraded_label, (WIDTH / 2 - upgraded_label.get_width() / 2, 250))
            upgraded_label = upgrad_font.render("Health Restored!!", 1, (0, 255, 0))
            DIMENSIONS.blit(upgraded_label, (WIDTH / 2 - upgraded_label.get_width() / 2, 300))
            upgraded_label = upgrad_font.render("Final Wave!!", 1, (255, 0, 0))
            DIMENSIONS.blit(upgraded_label, (WIDTH / 2 - upgraded_label.get_width() / 2, 330))

        pygame.display.update()

    while run:
        clock.tick(FPS)
        redraw_window()

        if lives <= 0 or player.health <= 0:
            lost = True
            lost_count += 1

        if lost:
            if lost_count > FPS * 8.9:
                run = False
            else:
                continue

        if level < 6:
            if len(enemies) == 0:
                level += 1
                wave_length += 1
                for i in range(wave_length):
                    if level == 1:
                        enemy = Enemy(random.randrange(25, WIDTH - 80), random.randrange(-1200, -100),
                                      random.choice(["blue"]))
                        enemies.append(enemy)
                        score += 10  # Here the var 'score' which I've deleted for now, is incrased at every level.
                    if level == 2:
                        enemy = Enemy(random.randrange(25, WIDTH - 80), random.randrange(-1200, -100),
                                      random.choice(["red"]))
                        enemies.append(enemy)
                        score += 10
                    if level == 3:
                        enemy = Enemy(random.randrange(25, WIDTH - 80), random.randrange(-1200, -100),
                                      random.choice(["green"]))
                        enemies.append(enemy)
                        score += 10
                    if level == 4:
                        enemy = Enemy(random.randrange(12, WIDTH - 80), random.randrange(-1200, -100),
                                      random.choice(["red", "green", "blue"]))
                        enemies.append(enemy)
                        score += 10
                wave_length = 15
                for i in range(wave_length):
                    if level == 5:
                        enemy_vel = 1.3
                        player.health = 100
                        player_vel = 3
                        player.ship_img = THESPACEWALKER
                        player.laser_img = NEOXASER
                        enemy = Enemy(random.randrange(0, WIDTH - 172), random.randrange(-1200, -100),
                                      random.choice(["black"]))
                        enemies.append(enemy)
                        score += 60

        if level > 5:
            winc = True
            winc_count += 1

        if level == 4:
            upgrad = True
            upgrad_count += 1

        if winc:
            if winc_count > FPS * 9:
                run = False
            else:
                continue

        if upgrad:
            if upgrad_count > FPS * 3:
                upgrad = False
            else:
                continue

        if level == 5:
            upgraded = True
            upgraded_count += 1

        if upgraded:
            if upgraded_count > FPS * 2:
                upgraded = False
            else:
                continue

        d = shelve.open('score.txt')  # I want to save the score at the end of the game, outside of the program.
        d['score'] = score  # Here's the error.
        d.close()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and player.x - player_vel > 0:  # left
            player.x -= player_vel
        if keys[pygame.K_RIGHT] and player.x + player_vel + player.get_width() < WIDTH:  # right
            player.x += player_vel
        if keys[pygame.K_SPACE]:
            player.shoot()
        if keys[pygame.K_p]:
            pause()

        for enemy in enemies[:]:
            enemy.move(enemy_vel)
            enemy.move_lasers(laser_vel, player)

            if enemy.health == 0:
                enemies.remove(enemy)

            if random.randrange(0, 2 * 60) == 1:
                enemy.shoot()

            if collide(enemy, player):
                player.health -= 10
                enemies.remove(enemy)
                crashed += 1

            elif enemy.y + enemy.get_height() > HEIGHT:
                lives -= 1
                enemies.remove(enemy)

        player.move_lasers(-laser_vel, enemies)


def button1():
    mouse = pygame.mouse.get_pos()
    if WIDTH / 2 - 75 + 150 > mouse[0] > WIDTH / 2 - 75 and 250 + 50 > mouse[1] > 250:
        pygame.draw.rect(DIMENSIONS, (100, 100, 100), (WIDTH / 2 - 75, 250, 150, 50))
    else:
        pygame.draw.rect(DIMENSIONS, (255, 255, 255), (WIDTH / 2 - 75, 250, 150, 50))

    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect = text_objects("BLAST OFF!", smallText)
    textRect.center = ((WIDTH / 2), (250 + (50 / 2)))
    DIMENSIONS.blit(textSurf, textRect)

    for event in pygame.event.get():
        if WIDTH / 2 - 75 + 150 > mouse[0] > WIDTH / 2 - 75 and 250 + 50 > mouse[1] > 250:
            if event.type == pygame.MOUSEBUTTONDOWN:
                main()


def Score1():
    d = shelve.open('score.txt')  # This is where I'm trying to get the saved score from outside the program.
    score = d['score']
    highscore = max(score)  # I want it to be a highscore display at the start screen.
    d.close()

    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect = text_objects(f"HIGH SCORE: {highscore}", smallText)
    textRect.center = ((WIDTH / 2), (260 + (50 / 2)))
    DIMENSIONS.blit(textSurf, textRect)


def start_screen():
    run = True
    while run:
        DIMENSIONS.blit(BG, (0, 0))

        button1()
        Score1()

        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

    pygame.quit()


start_screen()
随机导入
进口货架
导入pygame
从pygame导入混合器
pygame.font.init()
pygame.init()
#屏幕大小
宽度,高度=800600
尺寸=pygame.display.set_模式((宽度、高度))
#游戏名称
pygame.display.set_标题(“太空行走者”)
#敌人
REDEX=pygame.image.load(“REDEX.png”)
GENEX=pygame.image.load(“GENEX.png”)
BREX=pygame.image.load(“BREX.png”)
BOSS=pygame.image.load(“BOSS.png”)
#球员
player=pygame.image.load(“BLShip.png”)
SPACEWALKER=pygame.image.load(“SPACEWALKER.png”)
#敌方激光
REDEXASER=pygame.image.load(“REDEXASER.png”)
GENEXASER=pygame.image.load(“GENEXASER.png”)
BREXASER=pygame.image.load(“BREXASER.png”)
BOSSXASER=pygame.image.load(“BOSSXASER.png”)
#播放器激光器
LAME=pygame.image.load(“SPACEXASER.png”)
NEOXASER=pygame.image.load(“NEOXASER.png”)
#游戏背景
BG=pygame.transform.scale(pygame.image.load(“background.png”),(宽度、高度))
#游戏音乐
music=pygame.mixer.music.load('background.mp3')
pygame.mixer.music.play(-1)
#游戏中使用的函数
#暂停功能
def pause():
暂停=真
暂停时:
pause\u font=pygame.font.SysFont(“freesansbold.ttf”,80)
con_font=pygame.font.SysFont(“freesansbold.ttf”,80)
对于pygame.event.get()中的事件:
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_c:
暂停=错误
pause\u label=pause\u font.render(“暂停”,1,(255,255,255))
尺寸。blit(暂停标签,(宽度/2-暂停标签。获取宽度()/2250))
con_label=con_font.render(“C继续”,1,(255,255,255))
尺寸。blit(con_标签,(宽度/2-con_标签。获取_宽度()/2300))
pygame.mixer.pause()
pygame.display.update()
pygame.time.Clock()
激光等级:
定义初始值(self,x,y,img):
self.x=x
self.y=y
self.img=img
self.mask=pygame.mask.from_surface(self.img)
def绘图(自,窗口):
window.blit(self.img,(self.x,self.y))
def移动(自身、水平):
self.y+=vel
def关闭屏幕(自身、高度):
不返回(高度>=self.y>=0)
def碰撞(自身、obj):
返回碰撞(自身,obj)
船舶等级:
冷却时间=30
定义初始化(self,x,y,health=100):
self.x=x
self.y=y
自我健康
self.ship\u img=无
self.laser\u img=无
self.lasers=[]
self.cool\u down\u计数器=0
def绘图(自,窗口):
window.blit(self.ship\u img,(self.x,self.y))
对于自我激光器中的激光器:
激光绘图(窗口)
def move_激光器(自身、vel、obj):
self.cooldown()
对于自我激光器中的激光器:
激光移动(vel)
如果激光关闭屏幕(高度):
自我。激光。移除(激光)
elif激光碰撞(obj):
目标健康-=10
自我。激光。移除(激光)
def冷却(自):
如果self.cool\u down\u计数器>=self.cool:
self.cool\u down\u计数器=0
elif self.cool\u down\u计数器>0:
自我冷却计数器+=1
def喷射(自):
如果self.cool\u down\u计数器==0:
激光=激光(self.x-17,self.y,self.laser\u img)
self.lasers.append(激光)
自我冷却计数器=1
lsound=mixer.Sound('Lfire.wav'))
播放
def get_宽度(自身):
返回self.ship\u img.get\u width()
def get_高度(自身):
返回self.ship\u img.get\u height()
职业球员(船):
定义初始化(self,x,y,health=100):
super()。\uuuu init\uuuu(x,y,健康)
self.ship\u img=玩家
self.laser\u img=LAME
self.mask=pygame.mask.from_surface(self.ship_img)
self.max_health=健康
def move_激光器(自身、vel、objs):
self.cooldown()
用于激光器的self.l
import random
import shelve

import pygame
from pygame import mixer

pygame.font.init()
pygame.init()

# THE SCREEN SIZE
WIDTH, HEIGHT = 800, 600
DIMENSIONS = pygame.display.set_mode((WIDTH, HEIGHT))

# THE GAME NAME
pygame.display.set_caption("The Space Walker")

# ENEMIES
REDEX = pygame.image.load("REDEX.png")
GENEX = pygame.image.load("GENEX.png")
BREX = pygame.image.load("BREX.png")
BOSS = pygame.image.load("Boss.png")

# THE PLAYER
player = pygame.image.load("BLShip.png")
THESPACEWALKER = pygame.image.load("SPACEWALKER.png")

# ENEMY LASERS
REDEXASER = pygame.image.load("REDEXASER.png")
GENEXASER = pygame.image.load("GENEXASER.png")
BREXASER = pygame.image.load("BREXASER.png")
BOSSXASER = pygame.image.load("BOSSXASER.png")

# PLAYER LASER
LAME = pygame.image.load("SPACEXASER.png")
NEOXASER = pygame.image.load("NEOXASER.png")

# GAME BACKGROUND
BG = pygame.transform.scale(pygame.image.load("background.png"), (WIDTH, HEIGHT))

# GAME MUSIC
music = pygame.mixer.music.load('background.mp3')
pygame.mixer.music.play(-1)


# FUNCTIONS BEING USED IN THE GAME

# PAUSE FUNCTION


def pause():
    paused = True

    while paused:
        pause_font = pygame.font.SysFont("freesansbold.ttf", 80)
        con_font = pygame.font.SysFont("freesansbold.ttf", 80)
        for event in pygame.event.get():

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_c:
                    paused = False

        pause_label = pause_font.render("Paused", 1, (255, 255, 255))
        DIMENSIONS.blit(pause_label, (WIDTH / 2 - pause_label.get_width() / 2, 250))
        con_label = con_font.render("C to Continue", 1, (255, 255, 255))
        DIMENSIONS.blit(con_label, (WIDTH / 2 - con_label.get_width() / 2, 300))
        pygame.mixer.pause()
        pygame.display.update()
        pygame.time.Clock()


class Laser:
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.mask = pygame.mask.from_surface(self.img)

    def draw(self, window):
        window.blit(self.img, (self.x, self.y))

    def move(self, vel):
        self.y += vel

    def off_screen(self, height):
        return not (height >= self.y >= 0)

    def collision(self, obj):
        return collide(self, obj)


class Ship:
    COOLDOWN = 30

    def __init__(self, x, y, health=100):
        self.x = x
        self.y = y
        self.health = health
        self.ship_img = None
        self.laser_img = None
        self.lasers = []
        self.cool_down_counter = 0

    def draw(self, window):
        window.blit(self.ship_img, (self.x, self.y))
        for laser in self.lasers:
            laser.draw(window)

    def move_lasers(self, vel, obj):
        self.cooldown()
        for laser in self.lasers:
            laser.move(vel)
            if laser.off_screen(HEIGHT):
                self.lasers.remove(laser)
            elif laser.collision(obj):
                obj.health -= 10
                self.lasers.remove(laser)

    def cooldown(self):
        if self.cool_down_counter >= self.COOLDOWN:
            self.cool_down_counter = 0
        elif self.cool_down_counter > 0:
            self.cool_down_counter += 1

    def shoot(self):
        if self.cool_down_counter == 0:
            laser = Laser(self.x - 17, self.y, self.laser_img)
            self.lasers.append(laser)
            self.cool_down_counter = 1
        lsound = mixer.Sound('Lfire.wav')
        lsound.play()

    def get_width(self):
        return self.ship_img.get_width()

    def get_height(self):
        return self.ship_img.get_height()


class Player(Ship):
    def __init__(self, x, y, health=100):
        super().__init__(x, y, health)
        self.ship_img = player
        self.laser_img = LAME
        self.mask = pygame.mask.from_surface(self.ship_img)
        self.max_health = health

    def move_lasers(self, vel, objs):
        self.cooldown()
        for laser in self.lasers:
            laser.move(vel)
            if laser.off_screen(HEIGHT):
                self.lasers.remove(laser)
            else:
                for obj in objs:
                    if laser.collision(obj):
                        colli = mixer.Sound('coll.wav')
                        colli.play()
                        obj.health -= 10
                        if laser in self.lasers:
                            self.lasers.remove(laser)

    def draw(self, window):
        super().draw(window)
        self.healthbar(window)

    def healthbar(self, window):
        pygame.draw.rect(window, (255, 0, 0),
                         (self.x, self.y + self.ship_img.get_height() + 10, self.ship_img.get_width(), 10))
        pygame.draw.rect(window, (0, 255, 0), (
            self.x, self.y + self.ship_img.get_height() + 10,
            self.ship_img.get_width() * (self.health / self.max_health),
            10))


class Enemy(Ship):
    COLOR_MAP = {
        "red": (REDEX, REDEXASER),
        "green": (GENEX, GENEXASER),
        "blue": (BREX, BREXASER),
        "black": (BOSS, BOSSXASER)
    }

    def __init__(self, x, y, color, health=10):
        super().__init__(x, y, health)
        self.ship_img, self.laser_img = self.COLOR_MAP[color]
        self.mask = pygame.mask.from_surface(self.ship_img)

    def move(self, vel):
        self.y += vel

    def shoot(self):
        if self.cool_down_counter == 0:
            laser = Laser(self.x - 20, self.y, self.laser_img)
            self.lasers.append(laser)
            self.cool_down_counter = 1
        if self.y > 0:
            lsound = mixer.Sound('Lfire.wav')
            lsound.play()


def collide(obj1, obj2):
    offset_x = int(obj2.x - obj1.x)
    offset_y = int(obj2.y - obj1.y)
    return obj1.mask.overlap(obj2.mask, (offset_x, offset_y)) is not None


def text_objects(text, font):
    textSurface = font.render(text, True, (0, 0, 0))
    return textSurface, textSurface.get_rect()


def main():
    run = True
    FPS = 60
    level = 0
    lives = 3
    crashed = 0
    # score = 0  # But do I need it since I'm making it as a dict?

    main_font = pygame.font.SysFont("freesansbold.ttf", 50)
    winc_font = pygame.font.SysFont("freesansbold.ttf", 70)
    lost_font = pygame.font.SysFont("freesansbold.ttf", 70)
    upgrad_font = pygame.font.SysFont("freesansbold.ttf", 30)

    enemies = []
    wave_length = 5
    enemy_vel = 1

    player_vel = 5
    laser_vel = 3

    player = Player(400 - 30, 500)

    clock = pygame.time.Clock()

    lost = False
    lost_count = 0

    winc = False
    winc_count = 0

    upgrad = False
    upgrad_count = 0

    upgraded = False
    upgraded_count = 0

    def redraw_window():
        DIMENSIONS.blit(BG, (0, 0))
        # draw text
        lives_label = main_font.render(f"Lives: {lives}", 1, (255, 255, 255))
        level_label = main_font.render(f"Level: {level}", 1, (255, 255, 255))
        crashed_label = main_font.render(f"Crashed: {crashed}", 1, (255, 255, 255))
        # score_label = main_font.render(f"Score: {score}", 1, (225, 225, 225)) # The score on-screen counter idea.
        # I've dropped the idea of having it on-screen for the entire game for now.

        DIMENSIONS.blit(lives_label, (10, 10))
        DIMENSIONS.blit(level_label, (WIDTH - level_label.get_width() - 10, 10))
        DIMENSIONS.blit(crashed_label, (20, 550))
        # DIMENSIONS.blit(score_label, (WIDTH - score_label.get_width() - 10, 550))  # Not using it for now.
        # Is 'score' required to be a variable here? If it's a dict. how do I do it?

        for enemy in enemies:
            enemy.draw(DIMENSIONS)

        player.draw(DIMENSIONS)

        if lost:
            pygame.mixer.music.pause()
            LoseSound = mixer.Sound('LoseSound.wav')
            LoseSound.play()
            lost_label = lost_font.render("GAME OVER", 1, (255, 255, 255))
            DIMENSIONS.blit(lost_label, (WIDTH / 2 - lost_label.get_width() / 2, 250))
            crashed_label = main_font.render(f"You crashed: {crashed} times", 1, (255, 60, 60))
            DIMENSIONS.blit(crashed_label, (WIDTH / 2 - crashed_label.get_width() / 2, 305))
            credit_label = main_font.render(f"Game by (Aaditya & Aaryan) Sharma", 1, (255, 255, 0))
            DIMENSIONS.blit(credit_label, (WIDTH / 2 - credit_label.get_width() / 2, 345))
            credit1_label = main_font.render(f"Thank you for playing The Space Walker!", 1, (255, 128, 0))
            DIMENSIONS.blit(credit1_label, (WIDTH / 2 - credit1_label.get_width() / 2, 385))
            score_label = main_font.render(f"Score: {score - crashed}", 1, (255, 60, 60))  # Total score calc.
            DIMENSIONS.blit(score_label, (WIDTH / 2 - score_label.get_width() / 2, 395))  # But again, this will be a
            # dictionary, so how will this work?

        if winc:
            pygame.mixer.music.pause()
            WinSound = mixer.Sound('WinSound.wav')
            WinSound.play()
            winc_label = winc_font.render("You Win!", 1, (255, 255, 255))
            DIMENSIONS.blit(winc_label, (WIDTH / 2 - winc_label.get_width() / 2, 250))
            crashed_label = main_font.render(f"You crashed: {crashed} times", 1, (255, 60, 60))
            DIMENSIONS.blit(crashed_label, (WIDTH / 2 - crashed_label.get_width() / 2, 305))
            credit_label = main_font.render(f"Game by (Aaditya & Aaryan) Sharma", 1, (255, 255, 0))
            DIMENSIONS.blit(credit_label, (WIDTH / 2 - credit_label.get_width() / 2, 345))
            credit1_label = main_font.render(f"Thank you for playing The Space Walker!", 1, (255, 128, 0))
            DIMENSIONS.blit(credit1_label, (WIDTH / 2 - credit1_label.get_width() / 2, 385))
            score_label = main_font.render(f"Score: {score - crashed}", 1, (255, 60, 60))  # Total score calc.
            DIMENSIONS.blit(score_label, (WIDTH / 2 - score_label.get_width() / 2, 395))  # Same problems as above.

        if upgrad:
            upgrad_label = upgrad_font.render("Your ship is being upgraded!", 1, (255, 255, 255))
            DIMENSIONS.blit(upgrad_label, (WIDTH / 2 - upgrad_label.get_width() / 2, 250))
            upgrad_label = upgrad_font.render("Survive this level!!", 1, (255, 255, 255))
            DIMENSIONS.blit(upgrad_label, (WIDTH / 2 - upgrad_label.get_width() / 2, 270))

        if upgraded:
            upgraded_label = winc_font.render("UPGRADE COMPLETED", 1, (255, 255, 255))
            DIMENSIONS.blit(upgraded_label, (WIDTH / 2 - upgraded_label.get_width() / 2, 250))
            upgraded_label = upgrad_font.render("Health Restored!!", 1, (0, 255, 0))
            DIMENSIONS.blit(upgraded_label, (WIDTH / 2 - upgraded_label.get_width() / 2, 300))
            upgraded_label = upgrad_font.render("Final Wave!!", 1, (255, 0, 0))
            DIMENSIONS.blit(upgraded_label, (WIDTH / 2 - upgraded_label.get_width() / 2, 330))

        pygame.display.update()

    while run:
        clock.tick(FPS)
        redraw_window()

        if lives <= 0 or player.health <= 0:
            lost = True
            lost_count += 1

        if lost:
            if lost_count > FPS * 8.9:
                run = False
            else:
                continue

        if level < 6:
            if len(enemies) == 0:
                level += 1
                wave_length += 1
                for i in range(wave_length):
                    if level == 1:
                        enemy = Enemy(random.randrange(25, WIDTH - 80), random.randrange(-1200, -100),
                                      random.choice(["blue"]))
                        enemies.append(enemy)
                        score += 10  # Here the var 'score' which I've deleted for now, is incrased at every level.
                    if level == 2:
                        enemy = Enemy(random.randrange(25, WIDTH - 80), random.randrange(-1200, -100),
                                      random.choice(["red"]))
                        enemies.append(enemy)
                        score += 10
                    if level == 3:
                        enemy = Enemy(random.randrange(25, WIDTH - 80), random.randrange(-1200, -100),
                                      random.choice(["green"]))
                        enemies.append(enemy)
                        score += 10
                    if level == 4:
                        enemy = Enemy(random.randrange(12, WIDTH - 80), random.randrange(-1200, -100),
                                      random.choice(["red", "green", "blue"]))
                        enemies.append(enemy)
                        score += 10
                wave_length = 15
                for i in range(wave_length):
                    if level == 5:
                        enemy_vel = 1.3
                        player.health = 100
                        player_vel = 3
                        player.ship_img = THESPACEWALKER
                        player.laser_img = NEOXASER
                        enemy = Enemy(random.randrange(0, WIDTH - 172), random.randrange(-1200, -100),
                                      random.choice(["black"]))
                        enemies.append(enemy)
                        score += 60

        if level > 5:
            winc = True
            winc_count += 1

        if level == 4:
            upgrad = True
            upgrad_count += 1

        if winc:
            if winc_count > FPS * 9:
                run = False
            else:
                continue

        if upgrad:
            if upgrad_count > FPS * 3:
                upgrad = False
            else:
                continue

        if level == 5:
            upgraded = True
            upgraded_count += 1

        if upgraded:
            if upgraded_count > FPS * 2:
                upgraded = False
            else:
                continue

        d = shelve.open('score.txt')  # I want to save the score at the end of the game, outside of the program.
        d['score'] = score  # Here's the error.
        d.close()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and player.x - player_vel > 0:  # left
            player.x -= player_vel
        if keys[pygame.K_RIGHT] and player.x + player_vel + player.get_width() < WIDTH:  # right
            player.x += player_vel
        if keys[pygame.K_SPACE]:
            player.shoot()
        if keys[pygame.K_p]:
            pause()

        for enemy in enemies[:]:
            enemy.move(enemy_vel)
            enemy.move_lasers(laser_vel, player)

            if enemy.health == 0:
                enemies.remove(enemy)

            if random.randrange(0, 2 * 60) == 1:
                enemy.shoot()

            if collide(enemy, player):
                player.health -= 10
                enemies.remove(enemy)
                crashed += 1

            elif enemy.y + enemy.get_height() > HEIGHT:
                lives -= 1
                enemies.remove(enemy)

        player.move_lasers(-laser_vel, enemies)


def button1():
    mouse = pygame.mouse.get_pos()
    if WIDTH / 2 - 75 + 150 > mouse[0] > WIDTH / 2 - 75 and 250 + 50 > mouse[1] > 250:
        pygame.draw.rect(DIMENSIONS, (100, 100, 100), (WIDTH / 2 - 75, 250, 150, 50))
    else:
        pygame.draw.rect(DIMENSIONS, (255, 255, 255), (WIDTH / 2 - 75, 250, 150, 50))

    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect = text_objects("BLAST OFF!", smallText)
    textRect.center = ((WIDTH / 2), (250 + (50 / 2)))
    DIMENSIONS.blit(textSurf, textRect)

    for event in pygame.event.get():
        if WIDTH / 2 - 75 + 150 > mouse[0] > WIDTH / 2 - 75 and 250 + 50 > mouse[1] > 250:
            if event.type == pygame.MOUSEBUTTONDOWN:
                main()


def Score1():
    d = shelve.open('score.txt')  # This is where I'm trying to get the saved score from outside the program.
    score = d['score']
    highscore = max(score)  # I want it to be a highscore display at the start screen.
    d.close()

    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect = text_objects(f"HIGH SCORE: {highscore}", smallText)
    textRect.center = ((WIDTH / 2), (260 + (50 / 2)))
    DIMENSIONS.blit(textSurf, textRect)


def start_screen():
    run = True
    while run:
        DIMENSIONS.blit(BG, (0, 0))

        button1()
        Score1()

        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

    pygame.quit()


start_screen()