ImportError:没有名为Block的模块

时间:2017-11-07 04:45:55

标签: python pygame importerror

作为序言,我对编程完全陌生并试图自学。目前,我对CS术语的理解有限,因此对外行术语的任何解释都将是最受欢迎的。

我正在使用programarcadegames.com来学习基础计算机科学。我创建了一个游戏,它使用pygame库收集精灵,并希望将我的代码分解为多个文件。我的档案,' Sprite Moving.py'导入block_library.py以获取Block类。但是,运行程序时,我收到错误ImportError:No Module named Block。我已经阅读了Stackoverflow,并且人们遇到的一些问题包括导入的库是相同的名称或文件不在路径中。我相信我已经检查了这些。有什么建议吗?

有些谷歌,我相信这就是你要求的 - pastebin.com/J9qkjQ7F。我最初将我的所有文件都放在一个单独的文件夹中,但是我在研究过程中看到的一个问题是该文件可能没有包含在路径中?所以我将所有相关文件转储到idlelib。

提前致谢!

Sprite Moving.py

"""
Use sprites to collect blocks.
#Tony's lab 13 that will use sprites to collect blocks and print the score.

"""
import pygame
import random
import Block

# Define some colors
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
RED   = (255,   0,   0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)


class Player(pygame.sprite.Sprite):
    """ The class is the player-controlled sprite. """

    # -- Methods
    def __init__(self, filename):
        """Constructor function"""
        # Call the parent's constructor
        super().__init__()

        # Set height, width
        self.image = pygame.image.load(filename).convert()
        self.image.set_colorkey(WHITE)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.x = 10
        self.rect.y = 25

        # -- Attributes
        # Set speed vector
        self.change_x = 0
        self.change_y = 0

    def changespeed(self, x, y):
        """ Change the speed of the player"""
        self.change_x += x
        self.change_y += y

    def update(self):
        """ Find a new position for the player"""
        self.rect.x += self.change_x
        self.rect.y += self.change_y

        if self.rect.left <= -15:
            self.rect.x = 715
        elif self.rect.left >= 715:
            self.rect.x = -15
        elif self.rect.y <= -15:
            self.rect.y = 415
        elif self.rect.y >= 415:
            self.rect.y = -15



# Initialize Pygame
pygame.init()

# Set the height and width of the screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption("Fish World")

# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'Group.'
block_list_good = pygame.sprite.Group()
block_list_bad = pygame.sprite.Group()

# This is a list of every sprite. 
# All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()

for i in range(50):
    # This represents a good block
    good_block = block_library.Block("FishBlue.png")

    # Set a random location for the block
    block.rect.x = random.randrange(20, screen_width - 20)
    block.rect.y = random.randrange(20, screen_height - 20)

    # Add the block to the list of objects
    block_list_good.add(block)
    all_sprites_list.add(block)
for i in range(50):
    #This represents a bad block
    bad_block = block_library.Block("bomb100.png")

    #Sets blocks at random coordinates
    block.rect.x = random.randrange(20, screen_width - 20)
    block.rect.y = random.randrange(20, screen_height - 20)

    #Adds block to bad list and all list
    block_list_bad.add(block)
    all_sprites_list.add(block)



# Create a RED player block
player = Player("shark.png")
all_sprites_list.add(player)

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

score = 0

# Used to blit score onto the screen
font = pygame.font.Font(None, 25)

# Load sound FXs
good_block = pygame.mixer.Sound("good_block.wav")
bad_block = pygame.mixer.Sound("bad_block.wav")


# -------- Main Program Loop -----------
while not done:
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player.changespeed(0, -3)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, 3)
            elif event.key == pygame.K_LEFT:
                player.changespeed(-3, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(3, 0)

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                player.change_y = 0
            elif event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                player.change_x = 0

    # Clear the screen
    screen.fill(WHITE)

    #Update sprite positions
    all_sprites_list.update()


    # See if the player block has collided with a good block.
    blocks_good_hit_list = pygame.sprite.spritecollide(player, block_list_good, True)

    # Check the list of collisions.
    for block in blocks_good_hit_list:
        score += 1
        good_block.play()


    # See if the player block has collided with a bad block.
    blocks_bad_hit_list = pygame.sprite.spritecollide(player, block_list_bad, True)

    # Check the list of collisions.
    for block in blocks_bad_hit_list:
        score -= 1
        bad_block.play()




    # Draw all the spites
    all_sprites_list.draw(screen)

    # Blit score onto screen
    score_stamp = font.render("Score " + str(score), True, BLACK)
    screen.blit(score_stamp, [10, 10])

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # Limit to 60 frames per second
    clock.tick(60)

pygame.quit()

block_library.py

class Block(pygame.sprite.Sprite):
    """
    This class represents the ball.
    It derives from the "Sprite" class in Pygame.
    """

    def __init__(self, filename):
        """ Constructor. Pass in the color of the block,
        and its x and y position. """

        # Call the parent class (Sprite) constructor
        super().__init__()

        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.image = pygame.image.load(filename).convert()
        self.image.set_colorkey(WHITE)

        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values
        # of rect.x and rect.y
        self.rect = self.image.get_rect()

0 个答案:

没有答案