简体   繁体   English

Blit 的 PyGame 无效位置

[英]PyGame Invalid Position For Blit

I am creating a small level designer in Python (using PyGame).我正在用 Python 创建一个小型关卡设计师(使用 PyGame)。 The program is supposed to just let you place down an image, change between images, export to a PNG file, and export image path and coordinates to where it was place in a text document.该程序应该只是让您放置图像,在图像之间切换,导出为 PNG 文件,并将图像路径和坐标导出到文本文档中的位置。 I have gotten all of these components to work, but I am stuck with one last component, and that is reading the text document back into PyGame, and re-placing all of the images in the correct places with the correct sprites.我已经让所有这些组件都可以工作了,但我仍然坚持使用最后一个组件,那就是将文本文档读回 PyGame,并使用正确的精灵将所有图像重新放置在正确的位置。

The way that I have it currently (Which has been rewritten and Almost works ) produces an error whenever I try to read from one of my exported files.每当我尝试从导出的​​文件之一读取时,我目前拥有它的方式(已被重写并且几乎可以正常工作)会产生错误。

The error of course is:错误当然是:

stamped_surface.blit(image, (xcrds, ycrds))
TypeError: invalid destination position for blit

Here is my code:这是我的代码:

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()

This program has a file system and certain controls to make things happen, so here they are:这个程序有一个文件系统和某些控制来使事情发生,所以它们是:

ESC KEY - Auto Exports Program For Reference And Quits Program ESC KEY - 自动导出程序供参考并退出程序

S Key - Saves Surface as PNG file. S 键 - 将 Surface 保存为 PNG 文件。

N Key - Prompts User to select new sprite to use N 键 - 提示用户选择要使用的新精灵

E Key - Exports Image To PNG with file prompt E 键 - 使用文件提示将图像导出为 PNG

O Key - Opens File With Coordinate data and image path data. O 键 - 打开带有坐标数据和图像路径数据的文件。

Image Of Root File System: https://i.imgur.com/KouhmjK.png根文件系统镜像: https : //i.imgur.com/KouhmjK.png

A Few things you should know: This program auto-saves every file position to the file that contains Coords and Image Paths.您应该知道的几件事: 该程序会自动将每个文件位置保存到包含坐标和图像路径的文件中。

The file system is relatively simple to work out by looking at the code, but if you need some assistance, please ask.通过查看代码,文件系统相对简单,但如果您需要一些帮助,请询问。

blits((source, dest, area), ...)) -> (Rect, ...) you are missing out the destination. blits((source, dest, area), ...)) -> (Rect, ...) 你错过了目的地。 Read here 在这里阅读

And if you are making use of coordinates then use square brackets [x-co,y-co] like this:如果您使用坐标,则使用方括号 [x-co,y-co] 如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM