安装后,仍然存在导入错误(PYTMX)

2024-05-20 08:36:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试过看其他关于这个的帖子,但是没有任何效果,或者我没有正确地理解它。我刚刚卸载并重新安装了它现在与此消息

C:\Users\chris>pip uninstall pytmx
Uninstalling PyTMX-3.21.7:
  Would remove:
    d:\users\chris\appdata\local\programs\python\python37\lib\site-packages\pytmx-3.21.7-py3.7.egg-info
    d:\users\chris\appdata\local\programs\python\python37\lib\site-packages\pytmx\*
Proceed (y/n)? y
  Successfully uninstalled PyTMX-3.21.7

C:\Users\chris>python -m pip install pytmx
Collecting pytmx
  Using cached https://files.pythonhosted.org/packages/44/1c/be28e849a7dfcf992c91a55d7efd78f850aad25dcb483bf353b436fb338f/PyTMX-3.21.7.tar.gz
Requirement already satisfied: six in d:\users\chris\appdata\local\programs\python\python37\lib\site-packages (from pytmx) (1.12.0)
Installing collected packages: pytmx
  Running setup.py install for pytmx ... done
Successfully installed pytmx-3.21.7

C:\Users\chris>

当我在代码中使用它时,仍然会收到以下消息:

pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:\Users\chris\Desktop\Passion Project Files\Passion-Project.py", line 5, in <module>
    from pytmx.util_pygame import load_pygame
ModuleNotFoundError: No module named 'pytmx'

以下是我目前拥有的完整代码:

import random
import os
import pygame
from pygame.locals import *
from pytmx.util_pygame import load_pygame
WHITE = (255, 255, 255)
GREEN = (20, 255, 140)
GREY = (210, 210 ,210)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)

class Wall(pygame.sprite.Sprite):
  def __init__(self, x, y, width, height):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.Surface([width, height])
    self.image.fill(blue)

    self.rect = self.image.get_rect()
    self.rect.y = y
    self.rect.x = x






pygame.init()

#Link Sprite

sprite = pygame.image.load(os.path.join(image_folder, "sprite.gif"))

sprite_rect = sprite.get_rect()

sprite_rect.centerx = (width//2)
sprite_rect.centery = (height//3)

#Display and Run Code

display_width = 800
display_height = 800

pygame.display.set_caption("Smash Bros Replica")

#PyTMX Code

gameMap = pytmx.load_pygame('gamemap.tmx')

def game_loop():
    gameExit = False
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
               gameExit = True

    # Drawing Map Data On The Screen
    for layer in gameMap.visible_layers:
        for x, y, gid, in layer:
            tile = gameMap.get_tile_image_by_gid(gid)
            gameScreen.blit(tile, (x * gameMap.tilewidth,
                                   y * gameMap.tileheight))
    pygame.display.update()
    clock.tick(30)

game_loop()











#Key Inputs for Link Sprite  
keyinput = pygame.key.get_pressed()
if keyinput[pygame.K_LEFT]:
  sprite_rect.centerx -= 10
elif keyinput[pygame.K_RIGHT]:
  sprite_rect.centerx += 10
elif keyinput[pygame.K_UP]:
  sprite_rect.centery -= 10
elif keyinput[pygame.K_DOWN]:
  sprite_rect.centery += 10

  screen.blit(sprite, sprite_rect)

  clock.tick(60)

pygame.quit()

我不知道你是否需要所有的代码,但它是为一个项目,即将到期,我不知道该怎么办。我尝试过虚拟环境,但仍然没有任何效果,sudo也不起作用。我还从很多人那里看到,我无论如何都不应该使用sudo


Tags: infromrectimageimportselfforget