【问题标题】:How do I change python pygame function from string to function in python?如何将 python pygame 函数从字符串更改为 python 中的函数?
【发布时间】:2021-02-14 04:36:33
【问题描述】:

我正在尝试将字符串转换为类似的函数 A_rect = "pygame.Rect(50, 50, 100, 100)" 然后在主循环下我想像pygame.draw.rect(win, (200, 0, 0), a_rect)一样显示它 但它不会带一个字符串。有什么办法可以隐藏它。 我实际上使用的是json文件,所以我的python文件如下:

import pygame
from pygame.locals import *
import sys
import json

pygame.init()

wind_h, wind_w = 800, 600

wind = pygame.display.set_mode((wind_h, wind_w))
pygame.display.set_caption("No Touch")

spsh = pygame.image.load("Carpets 4.png")

jsonv = open("JSONV.json", "r")
imgs = open("imgs.json", "r")

img_data = json.load(imgs)
data = json.load(jsonv)

def get_image(posx, posy, width, height, sprite_sheet):
    """Extracts image from sprite sheet"""
    image = pygame.Surface([width, height])
    image.blit(sprite_sheet, (0, 0), (posx, posy, width, height))
    image.set_colorkey((0, 0, 0))
    return image

def terminate():
    pygame.quit()
    sys.exit()


def draw():
    wind.fill((100, 100, 100))
    for i in data['Room-1']:
        #print(data['Room-1'][str(i)]['w'])
        #pygame.draw.rect(wind, (255, 0, 0), pygame.Rect(data['Room-1'][str(i)]['x'], data['Room-1'][str(i)]['y'], data['Room-1'][str(i)]['w'], data['Room-1'][str(i)]['h']))
        wind.blit(get_image(0, 0, img_data['water']['w'], img_data['water']['h'], spsh), pygame.Rect(data['Room-1'][str(i)]['x'], data['Room-1'][str(i)]['y'], data['Room-1'][str(i)]['w'], data['Room-1'][str(i)]['h']))
        #wind.blit(spsh, pygame.Rect(2, 2, 4, 4))

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            terminate()


    draw()
    pygame.display.flip()

这是我的主要 jsonv.json 文件:

{
"Room-1" : {
    "rect0" : {
        "w": 500,
        "h": 500,
        "x": 4,
        "y": 4
    },
    "rect1" : {
        "w": 500,
        "h": 500,
        "x": 24,
        "y": 4
    },
    "rect2" : {
        "w": 500,
        "h": 500,
        "x": 44,
        "y": 4
    },
    "rect3" : {
        "w": 500,
        "h": 500,
        "x": 64,
        "y": 4
    },
    "rect4" : {
        "w": 500,
        "h": 500,
        "x": 4,
        "y": 24
    },
    "rect5" : {
        "w": 500,
        "h": 500,
        "x": 24,
        "y": 24
    },
    "rect6" : {
        "w": 500,
        "h": 500,
        "x": 44,
        "y": 24
    },
    "rect7" : {
        "w": 500,
        "h": 500,
        "x": 64,
        "y": 24
    },
    "rect8" : {
        "w": 500,
        "h": 500,
        "x": 4,
        "y": 44
    },
    "rect9" : {
        "w": 500,
        "h": 500,
        "x": 24,
        "y": 44
    },
    "rect10" : {
        "w": 500,
        "h": 500,
        "x": 44,
        "y": 44
    },
    "rect11" : {
        "w": 500,
        "h": 500,
        "x": 64,
        "y": 44
    }

  }
}

这里我想用"rect1": "pygame.Rect(500, 500, 4, 4)"替换rect 那么有没有办法将字符串转换为函数呢? 任何帮助将不胜感激。

【问题讨论】:

  • 尝试用eval()转换它。
  • 好的,试试@HenryTjhia,谢谢

标签: python json string pygame


【解决方案1】:

尝试使用 exec 命令。 例如,如果你有一个字符串 temp_string = "print('hello world')" 并且您想在 python 中将此字符串作为命令执行 那么你应该使用 exec() 函数作为

执行(临时字符串)

所以在你的情况下它应该是 exec("pygame.Rect(50, 50, 100, 100)")

我认为应该可以。

【讨论】:

    猜你喜欢
    • 2012-10-09
    • 2014-09-18
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2021-12-24
    • 2015-04-30
    • 1970-01-01
    相关资源
    最近更新 更多