PyGame Blit的无效位置

时间:2018-11-27 07:18:30

标签: python pygame pygame-surface

我正在使用Python创建一个小型设计器(使用PyGame)。 该程序只允许您放置图像,在图像之间进行更改,导出为PNG文件,以及将图像路径和坐标导出到文本文件中的放置位置。我已经使所有这些组件都能正常工作,但是我只能使用最后一个组件,那就是将文本文档读回PyGame,并使用正确的sprite将所有图像重新放置在正确的位置。

当我尝试从导出的​​文件之一中读取数据时,我目前拥有它的方式(已被重写且几乎可以使用)会产生错误。

错误当然是:

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

您应该了解的几件事: 该程序将每个文件位置自动保存到包含“坐标”和“图像路径”的文件中。

通过查看代码,文件系统相对简单,但是如果需要帮助,请询问。

1 个答案:

答案 0 :(得分:2)

blits((source,dest,area),...))->(Rect,...)您错过了目的地。阅读here

如果您要使用坐标,请使用方括号[x-co,y-co] 像这样:

block.blit(image,[0,0])