0

I started making Tetris in Pygame using OOP and importing other files to the main. Firstly, everything worked, but after I closed everything, open it again and run it, It gives me an error:

ImportError: cannot import name block

I don't know why that happens, because all files are together in a folder. This is my main:

import pygame
from block import Block
from shape import Shape

block = Block(290,0)
shape = Shape(290,0)
pygame.init()

screen = pygame.display.set_mode((500,800))
pygame.display.set_caption('Tetris')

turning = False
running = True

color = 'green'

clock = pygame.time.Clock()

while running:
    clock.tick(5)
    screen.fill((0,0,0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                block.Left()
                turning = True
            if event.key == pygame.K_RIGHT:
                block.Right()
                turning = True

    if not turning:
        block.yvel += 20

    block.draw(color, screen)

    turning = False

    pygame.display.update()

This is the file i want to import:

import pygame
from shapes import Shape

class Block(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super(Block, self).__init__()
        self.xvel = x
        self.yvel = y

        self.orange = pygame.image.load(["orange.png"])
        self.green = pygame.image.load(["green.png"])
        self.blue = pygame.image.load(["blue.png"])
        self.red = pygame.image.load(["red.png"])
        self.pink = pygame.image.load(["pink.png"])
        self.cyan = pygame.image.load(["cyan.png"])
        self.yellow = pygame.image.load(["yellow.png"])

    def Left(self):
        self.xvel -= 20

    def Right(self):
        self.xvel += 20

    def draw(self, color, screen):
        if color == 'orange':
            self.image = self.orange
        if color == 'blue':
            self.image = self.blue
        if color == 'green':
            self.image = self.green
        if color == 'pink':
            self.image = self.pink
        if color == 'cyan':
            self.image = self.cyan
        if color == 'yellow':
            self.image = self.yellow
        if color == 'red':
            self.image = self.red

        screen.blit(self.image, (self.xvel, self.yvel))

And the second one ( you don't have to read this. if i solve the first problem im sure ill handle the second):

import pygame
from block import Block

class Shape(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super(Shape, self).__init__()
        self.x = x
        self.y = y

    def shape1(self):
        self.shapes = [(x,y), (x+20,y), (x-20,y)]

    def shape2(self):
        self.shapes = [(x,y), (x,y+20), (x-20,y)]

    def shape3(self):
        self.shapes = [(x,y), (x,y+20), (x+20,y),(x-20,y)]

    def shape4(self):
        self.shapes = [(x,y), (x+20,y)]

    def shape5(self):
        self.shapes = [(x,y), (x,y+20), (x-20,y), (x-20, y-20)]
Stqrosta
  • 352
  • 2
  • 10

1 Answers1

0

Okay well one thing is that you import :

from shapes import Shape

in one file and in the other

from shape import Shape

so you remove : from shapes import Shape from Block.py

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
ananyamous
  • 161
  • 1
  • 9