字符没有出现,我也收到此消息:typeError:闪电器的无效目的地位置

发布于 2025-02-07 05:41:26 字数 2662 浏览 0 评论 0 原文

控制台:

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不太了解

Console:

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

This is my code:

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

I have been watching a Youtube tutorial to make my code for a little game I want to make and I have made it here and my code doesn't work when I try to move the character. The Character also doesn't show up. Could someone explain to me why this is happening and how to fix it? I don't know much about Python and Pygame

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼忆七猫命九 2025-02-14 05:41:26

您说的是“ 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文