我正在尝试创建一个类,以便在希望变大时让我的程序更容易制作。我有类Tilemap,它创建了一个pygame精灵。我试着调用这个精灵,然后把它搞定。但是当我这样做时,它声称它不是表面,而是说它是Tilemap类。错误是:
Traceback (most recent call last):
File "F:\Desktop Files\Python\Pygame RPG Type Game\RPG TEST 2.py", line 166, in <module>
main()
File "F:\Desktop Files\Python\Pygame RPG Type Game\RPG TEST 2.py", line 60, in main
map_surface.blit(tile_surface,(x*TILE_WIDTH,y*TILE_HEIGHT))
TypeError: argument 1 must be pygame.Surface, not Tilemap
我用于所有这些的代码如下
错误代码:
player = Player(locationPlayer)
grasstile = Tilemap(locationGrass)
watertile = Tilemap(locationWater)
waterbeach = Tilemap(locationBeach)
TILE_WIDTH = 32
TILE_HEIGHT = 32
tilemap = [
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[watertile, watertile, watertile, watertile, watertile, watertile, watertile, watertile, watertile]
]
total_level_width = len(tilemap[0]) * 32
total_level_height = len(tilemap)*32
camera = Camera(simple_camera ,total_level_width, total_level_height)
map_surface = pygame.Surface(( len(tilemap[0])*TILE_WIDTH, len(tilemap)*TILE_HEIGHT))
for y,row in enumerate(tilemap):
for x,tile_surface in enumerate(row):
map_surface.blit(tile_surface,(x*TILE_WIDTH,y*TILE_HEIGHT))
map_surface = pygame.transform.scale(map_surface, (1200, 800))
类别:
class Tilemap(pygame.sprite.Sprite):
def __init__(self, fileloc):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(fileloc).convert()
self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect()
感谢您抽出时间回答这个问题,请注意我对Pygame很新,这是代码。再次感谢您
答案 0 :(得分:1)
map_surface.blit(tile_surface,(x*TILE_WIDTH,y*TILE_HEIGHT))
pygame.Surface.blit将pygame.Surface
作为第一个参数,而您提供的tile_surface
是Tilemap
的实例。