字符没有出现,我也收到此消息:typeError:闪电器的无效目的地位置
控制台:
pygame 2.1.2 (SDL 2.0.16, Python 3.8.12)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "main.py", line 96, in <module>
redrawGameWindow()
File "main.py", line 23, in redrawGameWindow
win.blit(walkLeft,[walkCount//3], (x,y))
TypeError: invalid destination position for blit
这是我的代码:
import pygame
pygame.init()
win = pygame.display.set_mode((500,500)) # Window height and width eg: Width,Height
pygame.display.set_caption("Wall-E trash collector") #Window name
#images
walkRight = pygame.image.load("nest/player/right.png")
walkLeft = pygame.image.load("nest/player/left.png")
walkUp = pygame.image.load("nest/player/up.png")
walkDown = pygame.image.load("nest/player/down.png")
bg = pygame.image.load("nest/background.png")
def redrawGameWindow():
global walkCount
win.blit(bg, (0,0)) # This will draw our background image at (0,0)
if walkCount + 1 >= 27:
walkCount = 0
if left:
win.blit(walkLeft,[walkCount//3], (x,y))
walkCount += 1
elif right:
win.blit(walkRight,[walkCount//3], (x,y))
walkCount += 1
else:
walkCount = 0
pygame.display.update()
#game character height, width, start x and y, and speed.
x = 50
y = 50
width = 40
height = 40
vel = 5 #speed of character
left = False
right = False
up = False
down = False
walkCount = 0
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT: #if player clicks exit button, ends program
run = False # <--^
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
left = True
right = False
up = False
down = False
elif keys[pygame.K_RIGHT] and x < 500 - width - vel:
x += vel
left = False
right = True
up = False
down = False
elif keys[pygame.K_UP] and y > vel:
y -= vel
left = False
right = False
up = True
down = False
elif keys[pygame.K_DOWN] and y < 500 - height - vel:
y += vel
left = False
right = False
up = False
down = True
else: #If th character is not moving, it stays the same
left = False
right = False
up = False
down = False
walkCount = 0
#-------------------------------------------------
redrawGameWindow()
pygame.QUIT
我一直在观看YouTube教程,以制作我想制作的小游戏代码,并且在这里完成了我的代码,当我尝试移动角色时,我的代码不起作用。角色也没有出现。有人可以向我解释为什么会发生这种情况以及如何解决?我对Python和Pygame不太了解
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您说的是“ typeError:'pygame.surface'对象是不可订阅的“这是一个常见的错误,当您忘记在(x,y)中添加逗号时。希望这会有所帮助。
如果您需要更多帮助,请访问:
you said "TypeError: 'pygame.Surface' object is not subscriptable" this is a common error when you forgot to add the comma in (x,y). Hope this helps.
if you need more help go to: https://www.codegrepper.com/code-examples/python/pygame.Surface%27+object+is+not+subscriptable