微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用“导入文件”或“从文件导入变量”破坏了我的程序

如何解决使用“导入文件”或“从文件导入变量”破坏了我的程序

所以假设我们有 2 个文件file1.pyfile2.py,我需要在 file2.py 内执行 file1.py,并且在我执行之后,我需要将一个名为 screen 的变量从 file1.py 转移到 file2.py 中,其中 screenpygame.display.set_mode()file1 的体现。所以在 file2.py 中,我尝试了 2 个场景:

import pygame,os,time,random,sys,file1
from pygame.locals import *
#some other code

import pygame,sys
from pygame.locals import *
from file1 import screen
#some other code

所以执行file2的部分没有问题,但导入screen的部分是问题。问题是,第一次 file1 尝试执行 file2 时,没有出现错误消息,但是 import file1from file1 import screen 之后没有代码起作用,并且 pygame 发生了一些事情窗口,因为其中的图像会扭曲,但是在第二次执行 file2 后,它工作正常,但 pygame 窗口中的扭曲不会消失。

在使用 print()time.sleep() 进行无数次测试后,我发现问题的根源在于 import file1 部分,尽管我完全不知道是什么原因造成的

编辑:我被要求提供我的代码示例来说明问题,所以这里是:

file1.py

import pygame,subprocess
from pygame.locals import *

os.chdir(os.path.dirname(__file__))


pygame.init()
flags = RESIZABLE
size_of_monitor = pygame.display.Info()
width = size_of_monitor.current_w - 25
height = size_of_monitor.current_h - 50
screen = pygame.display.set_mode((width,height),flags)
FPS = 30

black = (0,0)

mainClock = pygame.time.Clock()

screen_size = screen.get_size()

default_size = (300,80)

button_image = pygame.image.load(r'orange_button.png')
settings_stretched_image = pygame.transform.scale(button_image,default_size)

settings_rect = settings_stretched_image.get_rect()
settings_rect.centerx = round(width/2)
settings_rect.centery = round(height/2)

exit_stretched_image = pygame.transform.scale(button_image,default_size)
exit_rect = exit_stretched_image.get_rect()
exit_rect.centerx = round(width/2)
exit_rect.centery = round(height/2) + round(150*height/size_of_monitor.current_h)

default_font_size = round(84000/((size_of_monitor.current_w + size_of_monitor.current_h)/2))
font_size = default_font_size
font = pygame.font.SysFont(None,font_size)

def check_if_clicked_button(event_list,settings_rect,exit_rect):
    for event in event_list:
        if event.type == MOUSEBUTTONUP:
            if event.button == 1:
                if settings_rect.collidepoint(event.pos):
                    return 1
                if exit_rect.collidepoint(event.pos):
                    return 2

def draw_text(text,font,surface,color,x,y):
    text_obj = font.render(text,1,color)
    text_rect = text_obj.get_rect()
    text_rect.centerx = x
    text_rect.centery = y
    surface.blit(text_obj,text_rect)

while True:
    event_list = pygame.event.get()
    result = check_if_clicked_button(event_list,exit_rect)
    if result == 1:
        #open settings
        exec(open(r'settings.py').read())
    if result == 2:
        pygame.quit()
        sys.exit()
    screen.fill(black)
    pygame.Surface.blit(screen,settings_stretched_image,settings_rect)
    pygame.Surface.blit(screen,exit_stretched_image,exit_rect)
    draw_text('Settings',screen,black,settings_rect.centerx,settings_rect.centery)
    draw_text('Exit',exit_rect.centerx,exit_rect.centery)
    pygame.display.update()
    mainClock.tick(FPS)

所以这 file1 尽可能简化。所以我仍然保留了一些东西:屏幕上的一个功能退出按钮(不是在右上角),一个如果调整大小就会崩溃的游戏,因为我删除了大约 75% 的代码,以及一个功能性选项按钮(正如我之前所说,在第一次按下时它会中断,但如果按下更多,则一切正常),为了测试,如果按下,窗口上会出现一个白色的小方块正好 1.5 秒。

这里是 file2.py内容

import pygame,sys
from pygame.locals import *

os.chdir(os.path.dirname(__file__))

from file1 import screen

pygame.init()

for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
pygame.draw.rect(screen,(255,255,255),(200,200,10,10))
pygame.display.update()
time.sleep(1.5)

代码不多,功能也不多,仅用于连接file1.py和绘制一个白色方块。

为此,file1.pyfile2.py 以及按钮图片(我添加了我使用的那个)必须在同一个文件夹中,命名为 {{1} },file1.pyfile2.py 分别运行orange_button.png

This is the button I used on the code

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。