Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
Python Pygame:精灵只绘制三个对象中的最后一个_Python_Json_Pygame - Fatal编程技术网

Python Pygame:精灵只绘制三个对象中的最后一个

Python Pygame:精灵只绘制三个对象中的最后一个,python,json,pygame,Python,Json,Pygame,我正在尝试制作一个阶段生成器,它将简化添加对象的开发,单独检查它们的冲突,等等,对于每个对象,我添加了代码 我的想法是将对象分组,并从JSON数据中检索它们的数据。虽然它似乎是通过收集和放置数据工作的,正如我所编码的,但它只想在那里绘制最后一个对象。这意味着,如果我有100个对象,它们各自的数据都是JSON格式的,那么只会绘制第100个对象 在这种情况下,我当然希望绘制所有第99个其他对象 从我在下面发布的代码中,我检查了一个print语句,我确实得到了三个对象的数据。这意味着JSON数据本身不

我正在尝试制作一个阶段生成器,它将简化添加对象的开发,单独检查它们的冲突,等等,对于每个对象,我添加了代码

我的想法是将对象分组,并从JSON数据中检索它们的数据。虽然它似乎是通过收集和放置数据工作的,正如我所编码的,但它只想在那里绘制最后一个对象。这意味着,如果我有100个对象,它们各自的数据都是JSON格式的,那么只会绘制第100个对象

在这种情况下,我当然希望绘制所有第99个其他对象

从我在下面发布的代码中,我检查了一个print语句,我确实得到了三个对象的数据。这意味着JSON数据本身不是问题所在

如果非要我猜的话,我认为问题在于个体的转化和群体的增加;StarThrizontalObjectRepeats函数可能是问题所在

这是一个最小的,可复制的代码,从我的主程序重新创建

代码:

main.py

设置.py

object.py

机修工

General.json

Object.json

对象图像:

输出:

注:

我知道新JSON文件(特别是General.JSON)的创建必须崩溃一次,然后程序才能恢复正常运行。这与我正在寻求帮助的当前问题无关。 对于输出,不响应也不能关联,因为我的主程序上没有类似的事件。 在StartThrizontalObjectRepeats函数中,当获取对象的rect时,循环遍历所有对象并获取rect,这意味着它们都具有相同的rect并且位于相同的位置

要解决此问题,请执行以下操作:

def HorizontalObjectRepeats(screen, ObjectExport):
    '''Initates the repeats of given horizontal test objects.'''
    NumberOfObjects = ReadJSONFileGeneral(1)
    for PlacementsofObjects in range(NumberOfObjects):
        StartHorizontalObjectRepeats(screen, ObjectExport, PlacementsofObjects) #changed the last parameter

def StartHorizontalObjectRepeats(screen, ObjectExport, objNum):
    '''Starts the repeat.'''
    Object = HorizontalObject(screen)

    Object.rect = ReadJSONFileObject(0, objNum) # get the nth objects rect

    ObjectExport.add(Object)
在StartThrizontalObjectRepeats函数中,当获取对象的rect时,循环遍历所有对象并获取rect,这意味着它们都具有相同的rect并且位于相同的位置

要解决此问题,请执行以下操作:

def HorizontalObjectRepeats(screen, ObjectExport):
    '''Initates the repeats of given horizontal test objects.'''
    NumberOfObjects = ReadJSONFileGeneral(1)
    for PlacementsofObjects in range(NumberOfObjects):
        StartHorizontalObjectRepeats(screen, ObjectExport, PlacementsofObjects) #changed the last parameter

def StartHorizontalObjectRepeats(screen, ObjectExport, objNum):
    '''Starts the repeat.'''
    Object = HorizontalObject(screen)

    Object.rect = ReadJSONFileObject(0, objNum) # get the nth objects rect

    ObjectExport.add(Object)

在mechanical.py中的第一个函数中,第一个函数在rangeblah中有一个for循环,它调用了第二个函数,根据您的描述,该函数在rangeblah中的for循环没有正确实现。你能得到一个单独显示的json文件吗?我会尝试从那里开始,一旦你有了正确的工作实现新功能,从那里开始尝试并想出在Mechanical.py的第一个函数中使用类和精灵。第一个函数在rangeblah中有一个for循环,它调用第二个函数,根据你的描述再次在rangeblah中for循环没有正确实现。你能得到一个单独显示的json文件吗?我会尝试从那里开始,一旦你实现了新的功能,并从那里开始尝试使用类和精灵

返回数据[HorizontalObjectsData]。getstrRepeats在这里,由于使用了“get”方法,你似乎从json文件而不是列表中得到了一个结果。如果不是,请随意忽略relevant@Kingsley是的,他们有,一切都在这里StartHorizontalObjectRepeats@KaanE. 这就是我如何为每个不同的水平对象单独设置rect,除非有更好的解决方案。返回数据[HorizontalObjectsData].getstrRepeats由于使用了“get”方法,您似乎从json文件而不是列表中获得了一个结果。如果不是,请随意忽略relevant@Kingsley是的,他们有,一切都在这里StartHorizontalObjectRepeats@KaanE. 这就是我如何为每个不同的水平对象单独设置rect,除非有更好的解决方案。您的答案与我期望的结果一致。非常感谢,因为这解决了我的问题!你的答案与我期望的结果一致。非常感谢,因为这解决了我的问题!对不起,你能再澄清一下你的陈述吗?据我所知,你的解决方案听起来与大卡哈纳的类似。上面的答案是我的意思,他的更简洁。对不起,你能再次澄清你的说法吗?据我所知,你的解决方案听起来很像大卡哈纳的解决方案。上面的答案就是我的意思,他的更简洁,哈哈
import pygame
from pygame.sprite import Sprite

class HorizontalObject(Sprite):
    def __init__(self, screen):
        super().__init__()

        self.screen = screen
        self.screen_rect = self.screen.get_rect()

        self.image = pygame.image.load('img_test_object.png')
        self.rect = self.image.get_rect()
import pygame
import json
from object import HorizontalObject

def HorizontalObjectRepeats(screen, ObjectExport):
    '''Initates the repeats of given horizontal test objects.'''
    NumberOfObjects = ReadJSONFileGeneral(1)
    for PlacementsofObjects in range(NumberOfObjects):
        StartHorizontalObjectRepeats(screen, ObjectExport, NumberOfObjects)

def StartHorizontalObjectRepeats(screen, ObjectExport, NumberOfObjects):
    '''Starts the repeat.'''
    Object = HorizontalObject(screen)

    for i in range(NumberOfObjects):
        Object.rect = ReadJSONFileObject(0, i)

    ObjectExport.add(Object)
    print(ObjectExport)

def StageBuilderTemplate(templateKeycode):
    '''Template data when starting out the stage builder.'''
    dictObjects = {"HorizontalObjectsData": {0: (50, 50, 308, 76), 1: (100, 100, 308, 76), 2: (150, 150, 308, 76)}}
    dictGeneral = {"BackgroundColor": (255, 0, 255), "HorizontalObjects": len(dictObjects["HorizontalObjectsData"].keys())}

    if templateKeycode == 0:
        return dictGeneral        
    elif templateKeycode == 1:
        return dictObjects

def CreateJSONFile(jsonCreationKeycode):
    '''Dumps data to the specified json based on the sented keycode.'''
    if jsonCreationKeycode == 0:
        with open('General.json', 'w') as f:
            json.dump(StageBuilderTemplate(0), f)
    elif jsonCreationKeycode == 1:
        with open('Object.json', 'w') as f:
            json.dump(StageBuilderTemplate(1), f)     

def ReadJSONFileObject(keycode, Repeats):
    '''Attempts to read Object.json file. It will create Object.json
    if it does not exist.

    After reading and extracting the entire data from Object.json,
    it will sent on specific data based on the sented keycode.'''
    try:
        with open("Object.json", 'r') as f:
            data = json.load(f)

            if keycode == 0:
                return data["HorizontalObjectsData"].get(str(Repeats))
    except FileNotFoundError:
        CreateJSONFile(1)

def ReadJSONFileGeneral(jsonGeneralKeycode):
    '''Attempts to read General.json file. It will create General.json
    if it does not exist.

    After reading and extracting the entire data from General.json,
    it will sent on specific data based on the sented keycode.'''
    try:
        with open("General.json", 'r') as f:
            data = json.load(f)

            if jsonGeneralKeycode == 0:
                return data.get('BackgroundColor')
            elif jsonGeneralKeycode == 1:
                return data.get('HorizontalObjects')
    except FileNotFoundError:
            CreateJSONFile(0)

def Refresh(Settings, Objects):
    '''Updates the screen'''
    Settings.screen.fill(Settings.background)
    Objects.draw(Settings.screen)
    pygame.display.flip()
{"BackgroundColor": [255, 0, 255], "HorizontalObjects": 3}
{"HorizontalObjectsData": {"0": [50, 50, 308, 76], "1": [100, 100, 308, 76], "2": [150, 150, 308, 76]}}
def HorizontalObjectRepeats(screen, ObjectExport):
    '''Initates the repeats of given horizontal test objects.'''
    NumberOfObjects = ReadJSONFileGeneral(1)
    for PlacementsofObjects in range(NumberOfObjects):
        StartHorizontalObjectRepeats(screen, ObjectExport, PlacementsofObjects) #changed the last parameter

def StartHorizontalObjectRepeats(screen, ObjectExport, objNum):
    '''Starts the repeat.'''
    Object = HorizontalObject(screen)

    Object.rect = ReadJSONFileObject(0, objNum) # get the nth objects rect

    ObjectExport.add(Object)