如何在不必重新启动程序的情况下更新哪个资产文件夹以使代码具有访问权限?

2024-04-19 07:08:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在为自己的练习开发一个非常简单的游戏

我有3个正在处理的文件:

我所有的主代码都存储在这里

main.py 

我的大部分函数都存储在这里。它也是我为我的游戏加载所有图形的地方

data.py 

跟踪当前加载的主题,并跟踪来自2个不同保存文件的数据

save_file.txt

下面是游戏流程的大致情况

[main.py]
imports data
    [data.py]
     checks save_file.txt line[0] 
     if line[0] == 1
         load theme_1
     elif line[0] == 2
         load theme_2
    if theme_1 is loaded
        Assets1 folder is called on to load in graphics
    elif theme_2 is loaded
        Assets2 folder is called on to load in graphics

让我们假设程序以默认为1的主题开始。现在,每当我的程序加载图片时,它都将是Assets1文件夹中的版本。但是,如果我写入theme.txt将1改为2,然后继续玩游戏,图形仍然会从Assets1调用,直到游戏关闭并重新执行

我怎样才能在不让用户重新开始他们的整个游戏的情况下让它更新呢。或者,是否有一个python命令将关闭程序并重新执行它

谢谢 以下是实际代码(如果有帮助):

[main.py]

import pygame
import os
import windows
pygame.font.init()
pygame.mixer.init()
import fileinput

assets = "Assets"
FPS = 120

WHITE = (0, 0, 0)
window_num = 1.0
WIDTH, HEIGHT = 800, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
break_tick = 5
pygame.display.set_caption("Double or Nothing")
balance_font = pygame.font.SysFont('comicsans', 60)
cash_out_font = pygame.font.SysFont('comicsans', 100)


def main():
    balance = 1000
    debt = 1000
    instance = 3
    current_screen = 1
    clock = pygame.time.Clock()
    run = True
    while run:

        clock.tick(FPS)
        profit = balance - debt
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        #MAIN MENU
        if current_screen == 1:
            WIN.blit(windows.main_menu(instance), (0,0))
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_UP] and instance > 1:
                instance -= 1
                clock.tick(break_tick)
            if keys_pressed[pygame.K_DOWN] and instance < 4:
                instance += 1
                clock.tick(break_tick)
            if keys_pressed[pygame.K_SPACE]:
                current_screen = windows.main_menu_space(instance)
                clock.tick(break_tick)
        #THEME PAGE
        elif current_screen == 12:
            if instance > 2:
                instance = 1
            WIN.blit(windows.theme(instance), (0, 0))
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_UP] and instance > 1:
                instance -= 1
                clock.tick(break_tick)
            if keys_pressed[pygame.K_DOWN] and instance < 2:
                instance += 1
                clock.tick(break_tick)
            if keys_pressed[pygame.K_SPACE]:
                current_screen = 1
                windows.change_theme(instance)




        #HELP PAGE
        elif current_screen == 2:
            WIN.blit(windows.help, (0,0))
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_SPACE]:
                current_screen = 1
                clock.tick(break_tick)
        #MAIN
        elif current_screen == 3:
            WIN.blit(windows.main(instance,), (0,0))
            balance_text = balance_font.render(
                "{:,}".format(balance), 1, WHITE)
            WIN.blit(balance_text, (650, 558))


            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_UP] and instance > 1:
                instance -= 1
                clock.tick(break_tick)
            if keys_pressed[pygame.K_DOWN] and instance < 4:
                instance += 1
                clock.tick(break_tick)
            if keys_pressed[pygame.K_SPACE]:
                current_screen = windows.main_space(instance)
                clock.tick(break_tick)
                instance = 1
        #DEBT CHECKER
        elif current_screen == 4:
            WIN.blit(windows.debt, (0, 0))

            balance_text = balance_font.render(
                "{:,}".format(balance), 1, WHITE)
            WIN.blit(balance_text, (270, 205))

            balance_text = balance_font.render(
                "{:,}".format(debt), 1, WHITE)
            WIN.blit(balance_text, (270, 282))

            if debt <= balance:
                balance_text = balance_font.render(
                    "{:,}".format(profit), 1, WHITE)
                WIN.blit(balance_text, (270, 359))
            else:
                balance_text = balance_font.render(
                    "0", 1, WHITE)
                WIN.blit(balance_text, (270, 359))

            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_SPACE]:
                current_screen = 3
                instance = 1
                clock.tick(break_tick)

        #DOUBLE OR NOTHING
        elif current_screen == 50:
            WIN.blit(windows.double_or_nothing(instance), (0, 0))
            balance_text = balance_font.render(
                "{:,}".format(balance), 1, WHITE)
            WIN.blit(balance_text, (650, 558))



            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_UP] and instance > 1:
                instance -= 1
                clock.tick(break_tick)
            if keys_pressed[pygame.K_DOWN] and instance < 3:
                instance += 1
                clock.tick(break_tick)
            if keys_pressed[pygame.K_SPACE]:
                bet = windows.double_or_nothing_space(instance)
                clock.tick(break_tick)
                instance = 1
                if bet > balance:
                        current_screen = 51
                elif bet <= balance:
                    if windows.play_function(bet):
                        balance += bet
                        current_screen = 52
                    else:
                        balance -= bet
                        current_screen = 53
        #DOUBLE OR NOTHING OUTCOME
        elif current_screen == 51:
            WIN.blit(windows.double_or_nothing_no_funds, (0,0))
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_SPACE]:
                current_screen = 54
                instance = 1
                clock.tick(break_tick)
        elif current_screen == 52:
            WIN.blit(windows.double_or_nothing_winner, (0,0))
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_SPACE]:
                current_screen = 54
                instance = 1
                clock.tick(break_tick)
        elif current_screen == 53:
            WIN.blit(windows.double_or_nothing_loser, (0,0))
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_SPACE]:
                current_screen = 54
                instance = 1
                clock.tick(break_tick)
        #PLAY AGAIN SCREEN
        elif current_screen == 54:
            WIN.blit(windows.play_again(instance), (0,0))
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_DOWN] and instance > 1:
                instance -= 1
                clock.tick(break_tick)
            if keys_pressed[pygame.K_UP] and instance < 2:
                instance += 1
                clock.tick(break_tick)
            if keys_pressed[pygame.K_SPACE]:
                current_screen = windows.play_again_space(instance)
                clock.tick(break_tick)
        #LOAN
        elif current_screen == 6:
            WIN.blit(windows.loan(instance), (0, 0))
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_UP] and instance > 1:
                instance -= 1
                clock.tick(break_tick)
            if keys_pressed[pygame.K_DOWN] and instance < 2:
                instance += 1
                clock.tick(break_tick)
            if keys_pressed[pygame.K_SPACE]:
                debt_add = 0
                current_screen = 3
                debt_add += windows.loan_space(instance)
                balance += debt_add
                debt += debt_add
                clock.tick(break_tick)
                instance = 1
        #CASH OUT
        elif current_screen == 7:
            #IF YOU HAVE DEBT
            if debt > balance:
                WIN.blit(windows.cash_out_debt, (0,0))
                debt_end = debt - balance
                balance_text = cash_out_font.render(
                    "{:,}".format(debt_end), 1, WHITE)
                WIN.blit(balance_text, (258, 288))

            #IF YOU MADE A PROFIT
            elif balance >= debt:
                WIN.blit(windows.cash_out_profit, (0,0))

                balance_text = cash_out_font.render(
                    str(profit), 1, WHITE)
                WIN.blit(balance_text, (258, 288))


            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_SPACE]:
                clock.tick(break_tick)
                current_screen = 8
        elif current_screen == 8:
            WIN.blit(windows.thank_you, (0, 0))
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_SPACE]:
                current_screen = 1
                clock.tick(break_tick)
        elif current_screen == 10:
            run = False
        print("BG: " + str(current_screen))
        print("Instance: " + str(instance))



        pygame.display.update()
    pygame.quit()

if __name__ == "__main__":
    main()

[windows.py]

import os
import pygame
import random
import fileinput
pygame.font.init()
pygame.mixer.init()

def get_assets():
    file = open("save_file", "r")
    file_list = []
    assets = file.read(1)
    return assets
assets = "Assets" + str(get_assets())

#Intro Main Menu
main_menu_play = pygame.image.load(os.path.join(assets, 'bg_1_a.png'))
main_menu_help = pygame.image.load(os.path.join(assets, 'bg_1_b.png'))

main_menu_quit = pygame.image.load(os.path.join(assets, 'bg_1_c.png'))
main_menu_theme = pygame.image.load(os.path.join(assets, 'bg_1_d.png'))

#THEME PAGE
theme_1 = pygame.image.load(os.path.join(assets, 'bg_12_a.png'))
theme_2 = pygame.image.load(os.path.join(assets, 'bg_12_b.png'))

#Help Page
help = pygame.image.load(os.path.join(assets, 'bg_2.png'))

#Menu
main_check_debt = pygame.image.load(os.path.join(assets, 'bg_3_a.png'))
main_take_loan = pygame.image.load(os.path.join(assets, 'bg_3_b.png'))
main_double_or_nothing = pygame.image.load(os.path.join(assets, 'bg_3_c.png'))
main_cash_out = pygame.image.load(os.path.join(assets, 'bg_3_d.png'))

#Debt
debt = pygame.image.load(os.path.join(assets, 'bg_4.png'))



#Double or Nothing

    #Main Double Or Nothing Screen
double_or_nothing_10 = pygame.image.load(os.path.join(assets, 'bg_5_a.png'))
double_or_nothing_100 = pygame.image.load(os.path.join(assets, 'bg_5_b.png'))
double_or_nothing_1000 = pygame.image.load(os.path.join(assets, 'bg_5_c.png'))

    #Insufficient Funds
double_or_nothing_no_funds = pygame.image.load(os.path.join(assets, 'bg_5_d.png'))
    #Winner
double_or_nothing_winner = pygame.image.load(os.path.join(assets, 'bg_5_e.png'))
    #Loser
double_or_nothing_loser = pygame.image.load(os.path.join(assets, 'bg_5_f.png'))

    #Play Again?
double_or_nothing_yes = pygame.image.load(os.path.join(assets, 'bg_5_g.png'))
double_or_nothing_no = pygame.image.load(os.path.join(assets, 'bg_5_h.png'))



#Take out a loan
loan_yes = pygame.image.load(os.path.join(assets, 'bg_6_a.png'))
loan_no = pygame.image.load(os.path.join(assets, 'bg_6_b.png'))

#cash out
cash_out_debt = pygame.image.load(os.path.join(assets, 'bg_7.png'))
cash_out_profit = pygame.image.load(os.path.join(assets, 'bg_8.png'))

#Thanks for playing!
thank_you = pygame.image.load(os.path.join(assets, 'bg_9.png'))

def main_menu(instance):
    if instance == 1:
        x = main_menu_play
    elif instance == 2:
        x = main_menu_help
    elif instance == 3:
        x = main_menu_quit
    elif instance == 4:
        x = main_menu_theme
    return x

def main_menu_space(instance):
    if instance == 1:
        x = 3
    elif instance == 2:
        x = 2
    elif instance == 3:
        x = 10
    elif instance == 4:
        x = 12
    return x

def main(instance):
    if instance == 1:
        x = main_check_debt
    elif instance == 2:
        x = main_take_loan
    elif instance == 3:
        x = main_double_or_nothing
    elif instance == 4:
        x = main_cash_out
    return x

def main_space(instance):
    if instance == 1:
        x = 4
    elif instance == 2:
        x = 6
    elif instance == 3:
        x = 50
    elif instance == 4:
        x = 7
    return x

def double_or_nothing(instance):
    if instance == 3:
        x = double_or_nothing_1000
    elif instance == 2:
        x = double_or_nothing_100
    elif instance == 1:
        x = double_or_nothing_10
    return x

def double_or_nothing_space(instance):
    if instance == 3:
        x = 1000
    if instance == 2:
        x = 100
    if instance == 1:
        x = 10
    return x

def play_function(bet):
    rand = [1, 0]
    if random.choice(rand) == 1:
        x = True
    else:
        x = False
    return x

def play_again(instance):
    if instance == 1:
        x = double_or_nothing_yes
    elif instance == 1:
        x = double_or_nothing_no
    return x

def play_again_space(instance):
    if instance == 1:
        x = 3
    elif instance == 2:
        x = 50
    return x

def loan(instance):
    if instance == 1:
        x = loan_yes
    elif instance == 2:
        x = loan_no
    return x
def loan_space(instance):
    if instance == 1:
        x = 100
    elif instance == 2:
        x = 0
    return x

def change_theme(theme):
    file = open("save_file", "r")
    file_list = []
    for line in file.readlines():
        file_list.append(line)
    file.close()

    file_list[0] = str(theme) + "\n"

    file = open("save_file", "w")
    file.writelines(file_list)
    file.close


def theme(instance):
    if instance == 1:
        x = theme_1
    elif instance == 2:
        x = theme_2
    return x

def theme_space(instance):
    if instance == 1:
        change_theme(1)
    elif instance == 2:
        change_theme(2)





def write_file(balance, debt, save_file, theme):

    file = open("save_file", "r")
    file_list = []
    for line in file.readlines():
        file_list.append(line)
    file.close()

    if save_file == 1:
        #balance
        file_list[3] = str(balance) + "\n"
        # balance
        file_list[5] = str(debt) + "\n"
        # balance
        file_list[7] = theme + "\n"
    elif save_file == 2:
        #balance
        file_list[11] = str(balance) + "\n"
        # balance
        file_list[13] = str(debt) + "\n"
        # balance
        file_list[15] = theme + "\n"


    file = open("save_file", "w")
    file.writelines(file_list)
    file.close