Untitled

mail@pastecode.io avatar
unknown
python
17 days ago
5.6 kB
1
Indexable
Never
import pygame
import sys
from settings import *
from world import World
from button import Button
import subprocess



# Pygame setup
pygame.mixer.init()
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
world = World(map, screen)
Menu_bg = pygame.image.load("ScuttleKnight/levels/backgrounds/game_background_4.png").convert()
Game_bg = pygame.image.load("ScuttleKnight/levels/backgrounds/bg.png").convert()
soundObj = pygame.mixer.Sound('ScuttleKnight/Sounds/death.mp3')
musicObj = pygame.mixer.Sound('ScuttleKnight/Sounds/game_music.mp3')
musicObj.set_volume(0.3)
winObj = pygame.mixer.Sound('ScuttleKnight/Sounds/win.mp3')
menuObj = pygame.mixer.Sound('ScuttleKnight/Sounds/menu_music.mp3')




def level_finish():
    finish = True
    while finish == True:
        screen.fill('black')
        mousePos = pygame.mouse.get_pos()
        font = pygame.font.Font(None, 100)

        menuText = font.render("LEVEL FINISHED", True, (139, 224, 20))
        menuRect = menuText.get_rect(center=(960, 100))

        next_button = Button(image=pygame.image.load("button.png"), pos=(960, 350), textINP="NEXT LEVEL",
                                font=font, colour=(219, 214, 200), hover=(255, 203, 61))
        quit_button = Button(image=pygame.image.load("button.png"), pos=(960, 800), textINP="QUIT",
                                font=font, colour=(219, 214, 200), hover=(255, 203, 61))
                
        screen.blit(menuText, menuRect)

        for button in [next_button, quit_button]:
            button.colourChange(mousePos)
            button.update(screen)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if next_button.input_check(mousePos):
                    
                    world.change_map(map2)
                    play_game()
                if quit_button.input_check(mousePos):
                    pygame.quit()
                    sys.exit()

        pygame.display.update()

def play_game():
    musicObj.play()
    is_playing = True
    while is_playing:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        if world.next_level():
            musicObj.stop()
            winObj.play()
            is_playing = False
            level_finish()
        elif world.check_bounds() == True:
            musicObj.stop()
            soundObj.play()
            screen.fill('black')
            mousePos = pygame.mouse.get_pos()
            font = pygame.font.Font(None, 100)

            menuText = font.render("GAME OVER", True, (139, 224, 20))
            menuRect = menuText.get_rect(center=(960, 100))

            menu_button = Button(image=pygame.image.load("button.png"), pos=(960, 350), textINP="MENU",
                            font=font, colour=(219, 214, 200), hover=(255, 203, 61))
            quit_button = Button(image=pygame.image.load("button.png"), pos=(960, 800), textINP="QUIT",
                            font=font, colour=(219, 214, 200), hover=(255, 203, 61))
        
            screen.blit(menuText, menuRect)

            for button in [menu_button, quit_button]:
                button.colourChange(mousePos)
                button.update(screen)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if menu_button.input_check(mousePos):
                        python = sys.executable
                        subprocess.Popen([python] + sys.argv)
                        sys.exit()
                    if quit_button.input_check(mousePos):
                        pygame.quit()
                        sys.exit()
        else:
            screen.blit(Game_bg,(0,0))
            world.run()
        pygame.display.update()
        clock.tick(60)

def menu():
    menuObj.play()
    while True:
        screen.blit(Menu_bg, (0, 0))

        mousePos = pygame.mouse.get_pos()
        font = pygame.font.Font(None, 100)  # Change None to the font file path if you have a specific font

        menuText = font.render("SCUTTLE KNIGHT", True, (139, 224, 20))
        menuRect = menuText.get_rect(center=(960, 100))

        play_button = Button(image=pygame.image.load("button.png"), pos=(960, 350), textINP="PLAY",
                             font=font, colour=(219, 214, 200), hover=(255, 203, 61))
        quit_button = Button(image=pygame.image.load("button.png"), pos=(960, 800), textINP="QUIT",
                             font=font, colour=(219, 214, 200), hover=(255, 203, 61))

        screen.blit(menuText, menuRect)

        for button in [play_button, quit_button]:
            button.colourChange(mousePos)
            button.update(screen)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if play_button.input_check(mousePos):
                    menuObj.stop()
                    play_game()
                if quit_button.input_check(mousePos):
                    pygame.quit()
                    sys.exit()

        pygame.display.update()

menu()
Leave a Comment