Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我得到了一个错误;TypeError:参数1必须是pygame.Surface“;因为我试图使用.txt文件在两个python代码之间共享一个值_Python_File_Pygame - Fatal编程技术网

我得到了一个错误;TypeError:参数1必须是pygame.Surface“;因为我试图使用.txt文件在两个python代码之间共享一个值

我得到了一个错误;TypeError:参数1必须是pygame.Surface“;因为我试图使用.txt文件在两个python代码之间共享一个值,python,file,pygame,Python,File,Pygame,print('Hello world!')我正试图使用两个不同的python文件(fish_simulator.py和settings.py)控制一个pygame窗口,因此,我将fish_simuator.py作为主文件,如果发生某种事情,它将执行settings.py,但是我在使用名为storage_file.txt的.txt文件在fish simulator.py和settings.py之间共享数据时遇到了一个问题,下面是我的一些程序: 这是fish simulator.py的一部分 pyg

print('Hello world!')
我正试图使用两个不同的python文件(
fish_simulator.py
settings.py
)控制一个pygame窗口,因此,我将
fish_simuator.py
作为主文件,如果发生某种事情,它将执行
settings.py
,但是我在使用名为storage_file.txt的.txt文件在
fish simulator.py
settings.py
之间共享数据时遇到了一个问题,下面是我的一些程序:

这是
fish simulator.py的一部分

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)

#some code later

if result == 1:
    #open settings
    with open(r'files\storage_file.txt', 'w') as storage_file:
        storage_file.write(str(screen))
    exec(open(r'.\extra programs\settings.py').read())
#inside of the code that gets executed
import pygame, os, time, random, sys
from pygame.locals import *

pygame.init()

with open(r'files\storage_file.txt', 'r') as storage_file:
    screen = storage_file.read()

for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
pygame.draw.rect(screen, (255, 255, 255), (random.randint(1, 400), random.randint(1, 400), 10, 10))
pygame.display.update()
time.sleep(2)
这是
settings.py的一部分

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)

#some code later

if result == 1:
    #open settings
    with open(r'files\storage_file.txt', 'w') as storage_file:
        storage_file.write(str(screen))
    exec(open(r'.\extra programs\settings.py').read())
#inside of the code that gets executed
import pygame, os, time, random, sys
from pygame.locals import *

pygame.init()

with open(r'files\storage_file.txt', 'r') as storage_file:
    screen = storage_file.read()

for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
pygame.draw.rect(screen, (255, 255, 255), (random.randint(1, 400), random.randint(1, 400), 10, 10))
pygame.display.update()
time.sleep(2)
在主代码中一切正常,
screen
被创建,然后保存在
storage_file.txt
中,然后当
fish simulator
执行
settings.py
settings.py
提取
screen
作为字符串,当我尝试运行
pygame.draw.rect(screen,(255,255),(random.randint(1400),random.randint(1400),10,10))
,它给了我一个错误

TypeError: argument 1 must be pygame.Surface, not str
因此,我没有使用
surface==
而是从
storage\u file.txt
中获取
surface===“
。虽然对某些人来说这似乎很愚蠢,但我非常绝望,以至于我尝试了raw\u input()和exec(),因为我在这个问题上找不到其他任何东西,我也在这些地方寻找,但这不是我需要的:


如果要将屏幕保存到文件中,需要将其转换为数字:

宽度是屏幕宽度 高度是屏幕高度

screen_array = pygame.surfarray.array3d(screen)

you then save the r, g, b values
with open("test1", "w") as a_file:  
    for y in range(height):
        for x in range(width):
           a_file.write(str(screen_array[x][y][0])
           a_file.write(" ")
           a_file.write(str(screen_array[x][y][1])
           a_file.write(" ")
           a_file.write(str(screen_array[x][y][2])
           a_file.write("\n")       
而要写回去,你就要颠倒这个过程

Surfarray需要numpy。我可能有错误的数组索引,所以你可能需要稍微处理一下

要重新阅读,请执行以下操作:

surf = pygame.Surface((width, height))
def load(self):
    x = y = 0
    
    with open("test1", "r") as a_file:
        for a_line in a_file:
            vals = a_line.split()
            r = int(vals[0])
            g = int(vals[1])
            b = int(vals[2])
            a = 255

            surf.set_at((x,y), (r,g,b,a))
            x += 1
            if x == self.width:
                y += 1
                x = 0
            

这将读取返回的值并在表面上设置它们。

您所说的“因为从.txt到.py的转换”是什么意思?问题很明显。您使用变量
screen
两次。您对
screen=storage\u file.read()有何期望
sceen
是与显示器相关联的表面。您必须为文件使用另一个名称。您不能将
屏幕
表面保存为文本文件。显示表面必须是由生成的。您希望通过
存储文件.write(str(screen))得到什么
?@rabbi76我的意思是,当我从
storage\u file.txt
读取
code.py
内部的
storage\u file.txt
中的内容时,我从技术上将
storage\u file.txt
中的数据转换为
code.py
,如果这让您感到困惑,我不知道如何正确解释“我从storage\u file.txt将数据转换为code.py”[...]"-不,你不会这样做。你所做的只是存储曲面对象的字符串表示。哇,我现在不在家,但我肯定会尝试这个,尽管我有点困惑。我将如何确切地反转这个过程?我需要使用另一个命令,如surfarray,还是只从文件中倒出值变成一个变量?我会编辑注释谢谢编辑!当我回家的时候我会尝试一下,但是你能告诉我为什么你使用self.surf而不是仅仅使用surf吗?我对类非常陌生,发现很难弄清楚它们是如何工作的。对不起,如果surf是类的一部分,你只需要self,否则就使用surf。我从m你自己的代码和它使用的一个类。2分钟的课堂教程:明白,谢谢!