Pygame 参数 2 必须是 pygame.surface 而不是 str
Pygame argument 2 must be pygame.surface not str
到目前为止我的代码,我遇到的问题是,当我从 selection 输入精灵时,我收到一条错误消息,指出参数必须是表面而不是字符串。我想知道如何解决这个问题。我想要做的是让用户 select 从以下 SP1R、SP1O 等中选择一个精灵。但是在我输入其中一个精灵后,程序不接受它并给出以下错误:
Traceback (most recent call last):
File "C:\Users\User\Documents\Pygame\pygame1.py", line 90, in <module>
drawGrid()
File "C:\Users\User\Documents\Pygame\pygame1.py", line 60, in drawGrid
win.blit (UserSprite, (500, 500))
TypeError: argument 1 must be pygame.Surface, not str
import pygame #Imports pygame
pygame.init() #Initialises pygame
win = pygame.display.set_mode((700, 700)) #Sets window size
pygame.display.set_caption("T1") #Sets title to T1
SP1R = pygame.image.load('Sprite 1 Red.png')
SP1O = pygame.image.load('Sprite 1 Orange.png')
SP1Y = pygame.image.load('Sprite 1 Yellow.png')
SP1G = pygame.image.load('Sprite 1 Green.png')
SP1B = pygame.image.load('Sprite 1 Blue.png')
SP1PU = pygame.image.load('Sprite 1 Purple.png')
SP1PI = pygame.image.load('Sprite 1 Pink.png')
SP2R = pygame.image.load('Sprite 2 Red.png')
SP2O = pygame.image.load('Sprite 2 Orange.png')
SP2Y = pygame.image.load('Sprite 2 Yellow.png')
SP2G = pygame.image.load('Sprite 2 Green.png')
SP2B = pygame.image.load('Sprite 2 Blue.png')
SP2PU = pygame.image.load('Sprite 2 Purple.png')
SP2PI = pygame.image.load('Sprite 2 Pink.png')
SP3R = pygame.image.load('Sprite 3 Red.png')
SP3O = pygame.image.load('Sprite 3 Orange.png')
SP3Y = pygame.image.load('Sprite 3 Yellow.png')
SP3G = pygame.image.load('Sprite 3 Green.png')
SP3B = pygame.image.load('Sprite 3 Blue.png')
SP3PU = pygame.image.load('Sprite 3 Purple.png')
SP3PI = pygame.image.load('Sprite 3 Pink.png')
x = 300 #x character position set to 50
y = 520 #y character position set to 50
width = 64 #width set to 40
height = 64 #height set to 60
vel = 10 #Character speed set to 5
white = (255, 255, 255)
left = False
right = False
UserSprite = input("""Choose a sprite the options are
SP1R, SP1O, SP1Y, SP1G, SP1B, SP1PU, SP1PI
SP2R, SP2O, SP2Y, SP2G, SP2B, SP2PU, SP2PI
SP3R, SP3O, SP3Y, SP3G, SP3B, SP3PU, SP3PI""")
while UserSprite != (SP1R or SP1O or SP1Y or SP1G or SP1B or SP1PU or SP1PI or SP2R or SP2O or SP2Y or SP2G or SP2B or SP2PU or SP3PI or SP3R or SP3O or SP3Y or SP3G or SP3B or SP3PU or SP3PI):
print ("Nope")
break
def drawGrid():
win.fill((0, 0, 0))
if left:
win.blit(walkLeft)#draws the following sprites
elif right:
win.blit(walkRight)
pygame.display.update() #updates the display by doing the above
pygame.draw.line(win, white, [50,50], [50, 600], 5)
pygame.draw.line(win, white, [50,50], [600, 50], 5)
pygame.draw.line(win, white, [600,600], [600, 50], 5)
pygame.draw.line(win, white, [50,600], [600, 600], 5)
pygame.draw.line(win, white, [50,450], [600, 450], 5)
pygame.display.update()
win.blit (UserSprite, (500, 500))
run = True #sets run to true
while run: #While loop for above
pygame.time.delay(100) #Sets a delay to prevent crashing
for event in pygame.event.get(): #For loop for event
if event.type == pygame.QUIT: #If user quits window
run = False #Do not run
keys = pygame.key.get_pressed()#Sets keybinds
if keys[pygame.K_a] and x>55:
x -= vel
left = True
right = False
elif keys[pygame.K_d] and x <575:
x += vel
right = True
left = False
if keys[pygame.K_w] and y >455:
y -= vel
up = True
down = False
elif keys[pygame.K_s] and y <565:
y += vel
down = True
up = False
drawGrid()
输入后
UserSprite = input("""Choose a sprite [...]""")
变量UserSprite
的内容是一个字符串。您必须找到具有名称的变量,该变量存储在变量 UserSprite
.
中
当您不带任何参数调用它时,您可以通过内置函数 vars()
获取包含局部变量的字典。这意味着 vars()["SP1R"]
将 return 对象 SP1R
。
SpriteName = input("""Choose a sprite [...]""")
UserSprite = vars()[SpriteName] if SpriteName in vars() else None
到目前为止我的代码,我遇到的问题是,当我从 selection 输入精灵时,我收到一条错误消息,指出参数必须是表面而不是字符串。我想知道如何解决这个问题。我想要做的是让用户 select 从以下 SP1R、SP1O 等中选择一个精灵。但是在我输入其中一个精灵后,程序不接受它并给出以下错误:
Traceback (most recent call last): File "C:\Users\User\Documents\Pygame\pygame1.py", line 90, in <module> drawGrid() File "C:\Users\User\Documents\Pygame\pygame1.py", line 60, in drawGrid win.blit (UserSprite, (500, 500)) TypeError: argument 1 must be pygame.Surface, not str
import pygame #Imports pygame
pygame.init() #Initialises pygame
win = pygame.display.set_mode((700, 700)) #Sets window size
pygame.display.set_caption("T1") #Sets title to T1
SP1R = pygame.image.load('Sprite 1 Red.png')
SP1O = pygame.image.load('Sprite 1 Orange.png')
SP1Y = pygame.image.load('Sprite 1 Yellow.png')
SP1G = pygame.image.load('Sprite 1 Green.png')
SP1B = pygame.image.load('Sprite 1 Blue.png')
SP1PU = pygame.image.load('Sprite 1 Purple.png')
SP1PI = pygame.image.load('Sprite 1 Pink.png')
SP2R = pygame.image.load('Sprite 2 Red.png')
SP2O = pygame.image.load('Sprite 2 Orange.png')
SP2Y = pygame.image.load('Sprite 2 Yellow.png')
SP2G = pygame.image.load('Sprite 2 Green.png')
SP2B = pygame.image.load('Sprite 2 Blue.png')
SP2PU = pygame.image.load('Sprite 2 Purple.png')
SP2PI = pygame.image.load('Sprite 2 Pink.png')
SP3R = pygame.image.load('Sprite 3 Red.png')
SP3O = pygame.image.load('Sprite 3 Orange.png')
SP3Y = pygame.image.load('Sprite 3 Yellow.png')
SP3G = pygame.image.load('Sprite 3 Green.png')
SP3B = pygame.image.load('Sprite 3 Blue.png')
SP3PU = pygame.image.load('Sprite 3 Purple.png')
SP3PI = pygame.image.load('Sprite 3 Pink.png')
x = 300 #x character position set to 50
y = 520 #y character position set to 50
width = 64 #width set to 40
height = 64 #height set to 60
vel = 10 #Character speed set to 5
white = (255, 255, 255)
left = False
right = False
UserSprite = input("""Choose a sprite the options are
SP1R, SP1O, SP1Y, SP1G, SP1B, SP1PU, SP1PI
SP2R, SP2O, SP2Y, SP2G, SP2B, SP2PU, SP2PI
SP3R, SP3O, SP3Y, SP3G, SP3B, SP3PU, SP3PI""")
while UserSprite != (SP1R or SP1O or SP1Y or SP1G or SP1B or SP1PU or SP1PI or SP2R or SP2O or SP2Y or SP2G or SP2B or SP2PU or SP3PI or SP3R or SP3O or SP3Y or SP3G or SP3B or SP3PU or SP3PI):
print ("Nope")
break
def drawGrid():
win.fill((0, 0, 0))
if left:
win.blit(walkLeft)#draws the following sprites
elif right:
win.blit(walkRight)
pygame.display.update() #updates the display by doing the above
pygame.draw.line(win, white, [50,50], [50, 600], 5)
pygame.draw.line(win, white, [50,50], [600, 50], 5)
pygame.draw.line(win, white, [600,600], [600, 50], 5)
pygame.draw.line(win, white, [50,600], [600, 600], 5)
pygame.draw.line(win, white, [50,450], [600, 450], 5)
pygame.display.update()
win.blit (UserSprite, (500, 500))
run = True #sets run to true
while run: #While loop for above
pygame.time.delay(100) #Sets a delay to prevent crashing
for event in pygame.event.get(): #For loop for event
if event.type == pygame.QUIT: #If user quits window
run = False #Do not run
keys = pygame.key.get_pressed()#Sets keybinds
if keys[pygame.K_a] and x>55:
x -= vel
left = True
right = False
elif keys[pygame.K_d] and x <575:
x += vel
right = True
left = False
if keys[pygame.K_w] and y >455:
y -= vel
up = True
down = False
elif keys[pygame.K_s] and y <565:
y += vel
down = True
up = False
drawGrid()
输入后
UserSprite = input("""Choose a sprite [...]""")
变量UserSprite
的内容是一个字符串。您必须找到具有名称的变量,该变量存储在变量 UserSprite
.
中
当您不带任何参数调用它时,您可以通过内置函数 vars()
获取包含局部变量的字典。这意味着 vars()["SP1R"]
将 return 对象 SP1R
。
SpriteName = input("""Choose a sprite [...]""")
UserSprite = vars()[SpriteName] if SpriteName in vars() else None