Pygame - TypeError: invalid rect assigment
Pygame - TypeError: invalid rect assigment
所以我正在尝试制作一款平台游戏。由于我是 pygame 图书馆的新手,所以我正在观看视频教程 (https://www.youtube.com/playlist?list=PL8ui5HK3oSiGXM2Pc2DahNu1xXBf7WQh-)。而我运行出了问题。我检查了文档、教程,甚至在这里发现有人问了同样的问题。但没有任何效果。那么我做错了什么,我该如何修复呢?
玩家class:
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self,pos):
super().__init__()
self.image = pygame.Surface((32,32))
self.image.fill((0,0,255))
self.rect = self.image.get_rect(topleft = pos)
这是我现在正在绘制玩家的关卡代码。我会改的
import pygame
from tiles import Tile
from settings import tileSize
from player import Player
class Level:
#setup
def __init__(self, levelData, surface):
self.displaySurface = surface
self.setupLevel(levelData)
self.worldShift = 0
def setupLevel(self,layout):
self.tiles = pygame.sprite.Group()
self.player = pygame.sprite.GroupSingle()
for rowIndex, row in enumerate(layout):
for colIndex, col in enumerate(row):
x = colIndex * tileSize
y = row * tileSize
if col == 'X':
tile = Tile((x,y),tileSize)
self.tiles.add(tile)
if col == 'P':
playerSprite = Player((x,y))
self.player.add(playerSprite)
def run(self):
#level tiles
self.tiles.update(self.worldShift)
self.tiles.draw(self.displaySurface)
#player
self.player.draw(self.displaySurface)
这是错误信息:
Hello from the pygame community. https://www.pygame.org/contribute.htmlTraceback (most recent call last):
File "d:\AdkoaMisko-game\main\window.py", line 10, in <module>
level = Level(levelMap,screen)
File "d:\AdkoaMisko-game\main\level.py", line 10, in __init__
self.setupLevel(levelData)
File "d:\AdkoaMisko-game\main\level.py", line 25, in setupLevel
playerSprite = Player((x,y))
File "d:\AdkoaMisko-game\main\player.py", line 8, in __init__
self.rect = self.image.get_rect(topleft = pos)
TypeError: invalid rect assignment
row
是列的列表。该行的索引是 rowIndex
:
y = row * tileSize
y = rowIndex * tileSize
所以我正在尝试制作一款平台游戏。由于我是 pygame 图书馆的新手,所以我正在观看视频教程 (https://www.youtube.com/playlist?list=PL8ui5HK3oSiGXM2Pc2DahNu1xXBf7WQh-)。而我运行出了问题。我检查了文档、教程,甚至在这里发现有人问了同样的问题。但没有任何效果。那么我做错了什么,我该如何修复呢?
玩家class:
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self,pos):
super().__init__()
self.image = pygame.Surface((32,32))
self.image.fill((0,0,255))
self.rect = self.image.get_rect(topleft = pos)
这是我现在正在绘制玩家的关卡代码。我会改的
import pygame
from tiles import Tile
from settings import tileSize
from player import Player
class Level:
#setup
def __init__(self, levelData, surface):
self.displaySurface = surface
self.setupLevel(levelData)
self.worldShift = 0
def setupLevel(self,layout):
self.tiles = pygame.sprite.Group()
self.player = pygame.sprite.GroupSingle()
for rowIndex, row in enumerate(layout):
for colIndex, col in enumerate(row):
x = colIndex * tileSize
y = row * tileSize
if col == 'X':
tile = Tile((x,y),tileSize)
self.tiles.add(tile)
if col == 'P':
playerSprite = Player((x,y))
self.player.add(playerSprite)
def run(self):
#level tiles
self.tiles.update(self.worldShift)
self.tiles.draw(self.displaySurface)
#player
self.player.draw(self.displaySurface)
这是错误信息:
Hello from the pygame community. https://www.pygame.org/contribute.htmlTraceback (most recent call last):
File "d:\AdkoaMisko-game\main\window.py", line 10, in <module>
level = Level(levelMap,screen)
File "d:\AdkoaMisko-game\main\level.py", line 10, in __init__
self.setupLevel(levelData)
File "d:\AdkoaMisko-game\main\level.py", line 25, in setupLevel
playerSprite = Player((x,y))
File "d:\AdkoaMisko-game\main\player.py", line 8, in __init__
self.rect = self.image.get_rect(topleft = pos)
TypeError: invalid rect assignment
row
是列的列表。该行的索引是 rowIndex
:
y = row * tileSize
y = rowIndex * tileSize