首页 > 解决方案 > PyGame Blit 的位置无效

问题描述

我正在用 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

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

通过查看代码来计算文件系统相对简单,但如果您需要一些帮助,请询问。

标签: pythonpygamepygame-surface

解决方案


blits((source, dest, area), ...)) -> (Rect, ...) 你错过了目的地。在这里阅读

如果您使用坐标,则使用方括号 [x-co,y-co],如下所示:

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

推荐阅读