我一直在努力,我正在尝试运行一些代码来显示我在Tiled中制作的tilemap,但是由于pygame窗口不断崩溃且没有响应,因此我似乎陷入了混乱。不知道我是否格式化正确,但是我却没有做太多,而且无论如何,很多评论都在这里。这是代码:
import pygame as pg
import pytmx
from settings import *
import os
import time
def collide_hit_rect(one, two):
return one.hit_rect.colliderect(two.rect)
class TiledMap:
def __init__(self, filename):
#loads .tmx tilemap, pixelalpha make sure transparency
tm = pytmx.load_pygame(filename, pixelalpha=True)
#multiplies how many tiles across the map by how many pixels each tile uses
self.width = tm.width * tm.tilewidth
self.height = tm.height * tm.tileheight
#stores all data above in variable tmxdata
self.tmxdata = tm
#draws Tiled map onto pg surface
def render(self, surface):
#stores command that finds image that goes with specific tiles by searching tmx data into ti
ti = self.tmxdata.get_tile_image_by_gid
#for all visible layers in map
for layer in self.tmxdata.visible_layers:
#if statement dependant on layer being Tile layer
if isinstance(layer, pytmx.TiledTileLayer):
#gets coordinates and gid(from .tmx) for each tile in layer
for x, y, gid, in layer:
#ti command gets images for the gid from last line
tile = ti(gid)
if tile:
#draws tile on surface
surface.blit(tile, (x * self.tmxdata.tilewidth,
y * self.tmxdata.tileheight))
def make_map(self):
#creates surface(as big as tilemap) to draw map onto
temp_surface = pg.Surface((self.width, self.height))
#render function will draw tilemap onto temp_surface
self.render(temp_surface)
return temp_surface
class Camera:
def __init__(self, width, height):
self.camera = pg.Rect(0, 0, width, height)
self.width = width
self.height = height
def apply(self, entity):
return entity.rect.move(self.camera.topleft)
#takes rectangle
def apply_rect(self, rect):
# returns rectangle moved by offset position from top left
return rect.move(self.camera.topleft)
def update(self, target):
x = -target.rect.centerx + int(WIDTH / 2)
y = -target.rect.centery + int(HEIGHT / 2)
# limit scrolling to map size
x = min(0, x) # left
y = min(0, y) # top
x = max(-(self.width - WIDTH), x) # right
y = max(-(self.height - HEIGHT), y) # bottom
self.camera = pg.rect(x, y, self.width, self.height)
class Display():
#This is the class that makes the changes that you want to display. You would add most of your changes here. """
def __init__(self):
self.displayRunning = True
self.displayWindow = pg.display.set_mode((500, 200))
self.clock = pg.time.Clock()
def update(self):
pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
pg.display.update()
def loadMap(self):
self.map = TiledMap('tilemap skeleton.tmx')
self.map_img = self.map.make_map()
self.map_rect = self.map_img.get_rect()
def displayLoop(self):
self.clock.tick()
self.update()
self.loadMap()
# Here is the start of the main driver
runDisplay = Display()
runDisplay.update()
runDisplay.loadMap()
time.sleep(60)
参考方案
将平铺地图导出为CSV文件,然后将其读取到游戏中时,可能会更容易
Python GPU资源利用 - python我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…
Python:图像处理可产生皱纹纸效果 - python也许很难描述我的问题。我正在寻找Python中的算法,以在带有某些文本的白色图像上创建皱纹纸效果。我的第一个尝试是在带有文字的图像上添加一些真实的皱纹纸图像(具有透明度)。看起来不错,但副作用是文本没有真正起皱。所以我正在寻找更好的解决方案,有什么想法吗?谢谢 参考方案 除了使用透明性之外,假设您有两张相同尺寸的图像,一张在皱纹纸上明亮,一张在白色背景上有深…
Python uuid4,如何限制唯一字符的长度 - python在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…
Python:无法识别Pip命令 - python这是我拍摄的屏幕截图。当我尝试在命令提示符下使用pip时,出现以下错误消息:pip无法识别为内部或外部命令,可操作程序或批处理文件。我已经检查了这个线程:How do I install pip on Windows?我所能找到的就是我必须将"C:\PythonX\Scripts"添加到我的类路径中,其中X代表python版本。如您在我的…
如何使用pass语句? - python我正在学习Python,并且已经到达有关pass语句的部分。我正在使用的指南将其定义为通常用作占位符的Null语句。我仍然不完全明白那是什么意思。有人可以告诉我一个简单的/基本情况,在其中使用pass语句以及为什么需要它吗? 参考方案 假设您正在使用尚未实现的某些方法设计一个新类。class MyClass(object): def meth_a(self)…