我得到的错误是
File "C:\Users\Nicholas\PycharmProjects\MortalKombat\Fighting.py", line 149, in main
allsprites.draw(screen)
File "C:\Python32\lib\site-packages\pygame\sprite.py", line 475, in draw
self.spritedict[spr] = surface_blit(spr.image, spr.rect)
TypeError: argument 1 must be pygame.Surface, not str
之前我得到的视频系统没有初始化错误,并发布了一个问题,导致我在这个程序中导致我的主程序中出现错误的问题。
我修复了这个错误但是当我将player1,player2和记分牌添加到pygame.sprites.Group()时出现了上面的错误。 Fighting.py的代码如下。我在做
allsprites.draw(screen)
我认为那个屏幕
screen = pygame.display.set_mode((900,400))
是一个pygame.Surface参数,但我不确定str的含义。
import pygame
class Scoreboard(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.lives = 5
self.score = 0
self.timeRem = 60
self.font = pygame.font.SysFont("None", 50)
self.RED = (255, 0, 0)
self.GREEN = (0, 255, 0)
def update(self):
self.text = ("%d" % self.timeRem)
if self.timeRem > 20:
self.image = self.font.render(self.text, 1, self.GREEN)
else:
self.image = self.font.render(self.text, 1, self.RED)
self.rect = self.image.get_rect()
self.rect.center = (450,25)
class Player1(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.player = "none"
self.player1Txt = open("Player1", "r")
self.image = self.player1Txt.read()
self.player1Txt.close()
self.player = pygame.image.load(self.image + "/walking0000.png")
self.player = self.player.convert()
self.player = pygame.transform.scale(self.player, (100,100))
self.rect = self.player.get_rect()
self.rect.center = (150,100)
self.counter = 0
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
if Player2.rect.centerx > self.rect.centerx:
self.counter +=1
self.player = pygame.image.load(self.image + "/walking000" + str(self.counter) + ".png")
self.player = self.player.convert()
self.player = pygame.transform.scale(self.player, (100,100))
self.rect.centerx += 5
elif Player2.rect.centerx < self.rect.centerx:
self.counter = 7
self.player = pygame.image.load(self.image + "/walking000" + str(self.counter) + ".png")
self.player = self.player.convert()
self.player = pygame.transform.scale(self.player, (100,100))
self.rect.centerx += 5
self.counter -= 1
elif Player2.rect.centerx == self.rect.centerx:
self.counter +=1
self.player = pygame.image.load(self.image + "/walking000" + str(self.counter) + ".png")
self.player = self.player.convert()
self.player = pygame.transform.scale(self.player, (100,100))
self.rect.centerx += 5
class Player2(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.player = "none"
self.player2Txt = open("Player2", "r")
self.image = self.player2Txt.read()
self.player2Txt.close()
self.player = pygame.image.load(self.image + "/walking0000.png")
self.player = self.player.convert()
self.player = pygame.transform.scale(self.player, (100,100))
self.rect = self.player.get_rect()
self.rect.center = (150,100)
self.counter = 0
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
if Player1.rect.centerx > self.rect.centerx:
self.counter +=1
self.player = pygame.image.load(self.image + "/walking000" + str(self.counter) + ".png")
self.player = self.player.convert()
self.player = pygame.transform.scale(self.player, (100,100))
self.rect.centerx += 5
elif Player1.rect.centerx < self.rect.centerx:
self.counter = 7
self.player = pygame.image.load(self.image + "/walking000" + str(self.counter) + ".png")
self.player = self.player.convert()
self.player = pygame.transform.scale(self.player, (100,100))
self.rect.centerx += 5
self.counter -= 1
elif Player1.rect.centerx == self.rect.centerx:
self.counter +=1
self.player = pygame.image.load(self.image + "/walking000" + str(self.counter) + ".png")
self.player = self.player.convert()
self.player = pygame.transform.scale(self.player, (100,100))
self.rect.centerx += 5
def main():
pygame.init()
screen = pygame.display.set_mode((900, 400))
background = pygame.image.load("arena.png")
background = pygame.transform.scale(background, screen.get_size())
scoreboard = Scoreboard()
player1 = Player1()
player2 = Player2()
allsprites = pygame.sprite.Group()
allsprites.add(scoreboard)
allsprites.add(player1)
allsprites.add(player2)
counter = 0
counter2 = 0
clock = pygame.time.Clock()
keepGoing = True
while keepGoing:
clock.tick(30)
counter +=1
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
if counter%30 == 0:
counter2 += 1
if counter2 >= 60:
scoreboard.timeRem = 0
else:
scoreboard.timeRem -= 1
screen.blit(background, (0, 0))
allsprites.update()
allsprites.draw(screen)
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()
答案 0 :(得分:1)
我看一下,我认为这段代码导致错误:
self.player1Txt = open("Player1", "r")
self.image = self.player1Txt.read()
self.player1Txt.close()
和
self.player2Txt = open("Player2", "r")
self.image = self.player2Txt.read()
self.player2Txt.close()
现在我不知道你为什么要让程序读取文本文件,但问题是在创建self.image时你必须以这样的格式进行:
self.image = pygame.Surface([width, height])
如果你想使用图像文件,你也可以这样做:
self.image = pygame.transform.scale(player_image, (width, height))
因为我认为这是您尝试对文本文件进行的操作。
我希望这有帮助!