【发布时间】:2018-11-27 07:18:30
【问题描述】:
我正在用 Python 创建一个小型关卡设计器(使用 PyGame)。 该程序应该只允许您放置图像,在图像之间切换,导出为 PNG 文件,并将图像路径和坐标导出到它在文本文档中的位置。我已经让所有这些组件正常工作,但我被最后一个组件困住了,那就是将文本文档读回 PyGame,并使用正确的精灵将所有图像重新放置在正确的位置。
每当我尝试从一个导出的文件中读取时,我目前的方式(已重写并且几乎可以工作)会产生错误。
错误当然是:
stamped_surface.blit(image, (xcrds, ycrds))
TypeError: invalid destination position for blit
这是我的代码:
import pygame as pg
import threading
import time
import pygame
from random import *
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfile
image_file = "../res/ExampleProject/TankGame/TankGameImg/tileGrass_transitionE.png"
f = open("../Saves/Backups/FailSafe.txt", "a+")
f.write("""
#################################################
# PyEngine #
# FailSafe #
# File #
# By MouseBatteries #
#################################################
""")
pg.init()
xcrds = 17
ycrds = 13
black = (0,0,0)
sw = 1280
sh = 720
screen = pg.display.set_mode((sw, sh))
pg.display.set_caption('thing')
image = pg.image.load(image_file).convert()
start_rect = image.get_rect()
image_rect = start_rect
running = True
stamped_surface = pg.Surface((sw, sh))
while running:
event = pg.event.poll()
keyinput = pg.key.get_pressed()
# Escape Program
if keyinput[pg.K_ESCAPE]:
fname = "../Saves/Design_complete.png"
pg.image.save(stamped_surface, fname)
print("File saved at {} ".format(fname))
quit()
#Save Work In Project File
if keyinput[pg.K_s]:
fname = "../Saves/LevelSave.png"
pg.image.save(stamped_surface, fname)
print("File saved at {} ".format(fname))
#Open New Selectable
if keyinput[pg.K_n]:
image_file = askopenfilename()
image = pg.image.load(image_file).convert()
print("Placable Updated!")
if keyinput[pg.K_e]:
fname = "../Saves/Export.png"
pg.image.save(stamped_surface, fname)
print("File saved at {} ".format(fname))
pg.quit()
#Recreate Terrain From File
if keyinput[pg.K_o]:
fileDest = askopenfilename()
openFile = open(fileDest, "r")
for line in openFile:
li = line.strip()
if li.startswith("Pec:"): #pec stands for "PyEngineCoords"
reimgpath = (line.rstrip())
nopecimgpath = reimgpath.replace("Pec:", "")
print(nopecimgpath)
image = pg.image.load(nopecimgpath).convert()
pg.display.update()
if li.startswith("Crdsx:"):
xposcrds = (line.rstrip())
xcrds = xposcrds.replace("Crdsx:", "")
x = int(xcrds)
print(x)
pg.display.update()
if li.startswith("Crdsy:"):
yposcrds = (line.rstrip())
ycrds = yposcrds.replace("Crdsy:", "")
y = int(ycrds)
print(y)
pg.display.update()
stamped_surface.blit(image, (xcrds, ycrds))
elif event.type == pg.QUIT:
running = False
elif event.type == pg.MOUSEMOTION:
image_rect = start_rect.move(event.pos)
elif event.type == pg.MOUSEBUTTONDOWN:
stamped_surface.blit(image, event.pos)
print("Image Placed!")
print(image_file, event.pos)
f.write("\nPec:" + image_file + "\nCrdsx:")
print(event.pos)
xpos_str = str(pg.mouse.get_pos()[0])
ypos_str = str(pg.mouse.get_pos()[1])
f.write(xpos_str)
f.write("\nCrdsy:")
f.write(ypos_str)
f.flush()
screen.fill(black)
screen.blit(stamped_surface, (0, 0))
screen.blit(image, image_rect)
pg.display.flip()
这个程序有一个文件系统和某些控制来让事情发生,所以他们在这里:
ESC KEY - 自动导出程序供参考和退出程序
S 键 - 将 Surface 保存为 PNG 文件。
N 键 - 提示用户选择要使用的新精灵
E 键 - 使用文件提示将图像导出为 PNG
O 键 - 使用坐标数据和图像路径数据打开文件。
根文件系统的图像: https://i.imgur.com/KouhmjK.png
你应该知道的几件事: 该程序会自动将每个文件位置保存到包含坐标和图像路径的文件中。
文件系统通过查看代码比较简单,但如果您需要帮助,请询问。
【问题讨论】:
-
使用
print在抛出异常的行之前打印出xcrds和ycrds。然后,当抛出异常时,你可以看到哪些值是错误的。 -
@sloth 所有值对我来说似乎都是正确的。i.imgur.com/BcNb3sH.png
标签: python pygame pygame-surface