问题描述
我正在尝试将字符串转换为类似的函数
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName wiki.bakji.kr
SSLEngine On
SSLProxyEngine On
RequestHeader set Front-End-Https "On"
ProxyRequests off
ProxyPass / https://localhost:3000
ProxyPassReverse / https://localhost:3000
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
然后在主循环下我想像A_rect = "pygame.Rect(50,50,100,100)"
一样显示它
但它不会需要一个字符串。有什么办法我可以隐瞒。
我实际上正在使用json文件,因此我的python文件如下:
pygame.draw.rect(win,(200,0),a_rect)
这是我的主要jsonv.json文件:
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,(posx,height))
image.set_colorkey((0,0))
return image
def terminate():
pygame.quit()
sys.exit()
def draw():
wind.fill((100,100))
for i in data['Room-1']:
#print(data['Room-1'][str(i)]['w'])
#pygame.draw.rect(wind,(255,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,img_data['water']['w'],img_data['water']['h'],spsh),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()
在这里,我想将rect替换为{
"Room-1" : {
"rect0" : {
"w": 500,"h": 500,"x": 4,"y": 4
},"rect1" : {
"w": 500,"x": 24,"rect2" : {
"w": 500,"x": 44,"rect3" : {
"w": 500,"x": 64,"rect4" : {
"w": 500,"y": 24
},"rect5" : {
"w": 500,"rect6" : {
"w": 500,"rect7" : {
"w": 500,"rect8" : {
"w": 500,"y": 44
},"rect9" : {
"w": 500,"rect10" : {
"w": 500,"rect11" : {
"w": 500,"y": 44
}
}
}
那么有什么方法可以将字符串转换为函数?
任何帮助将不胜感激。
解决方法
尝试使用exec命令。 例如,如果您有一个字符串temp_string =“ print('hello world')” 而您想将此字符串作为python中的命令执行 那么您应该使用exec()函数作为
exec(temp_string)
因此您的情况应该是exec(“ pygame.Rect(50,50,100,100)”)
我认为应该可以。