pygame error: NameError: name 'Block' is not defined

Asked 2 years ago, Updated 2 years ago, 94 views

I made it by referring to https://m.blog.naver.com/cheonwb/222156028857 but there is an error.

Traceback (most recent call last):
  File "/Users/user/Desktop/BrickBreak.py", line 9, in <module>
    class Block:
  File "/Users/user/Desktop/BrickBreak.py", line 59, in Block
    PADDLE = Block((242, 242, 0), Rect(300, 700, 100, 30))
NameError: name 'Block' is not defined

Full code

#Break blocks


import sys
import math
import random
import pygame
from pygame.locals import QUIT, KEYDOWN, K_LEFT, K_RIGHT, Rect
class Block:
    """ Block, Ball, Paddle Object ""
    def __init__(self, col, rect, speed=0):
        self.col = col
        self.rect = rect
        self.speed = speed
        self.dir = random.randint(-45, 45) + 270
    def move(self):
        """ "Move the ball """"
        self.rect.centerx += math.cos(math.radians(self.dir))* self.speed
        self.rect.centery -= math.sin(math.radians(self.dir))* self.speed
    def draw(self):
        """ Draw blocks, balls, and paddles """
        if self.speed == 0:
            pygame.draw.rect(SURFACE, self.col, self.rect)
        else:
            pygame.draw.ellipse(SURFACE, self.col, self.rect)
    def tick():
        """ Processing by frame """
        global BLOCKS
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_LEFT:
                    PADDLE.rect.centerx -= 10
                elif event.key == K_RIGHT:
                    PADDLE.rect.centerx += 10
        if BALL.rect.centery < 1000:
            BALL.move()
        # Collision with the block?
        prevlen = len(BLOCKS)
        BLOCKS = [x for x in BLOCKS if not x.rect.colliderect(BALL.rect)]
        if len(BLOCKS) != prevlen:
            BALL.dir *= -1
        # Clash with the paddles?
        if PADDLE.rect.colliderect(BALL.rect):
            BALL.dir = 90 + (PADDLE.rect.centerx - BALL.rect.centerx) / PADDLE.rect.width * 80
        # Collision with the wall?
        if BALL.rect.centerx < 0 or BALL.rect.centerx > 600:
            BALL.dir = 180 - BALL.dir
        if BALL.rect.centery < 0:
            BALL.dir = -BALL.dir
            BALL.speed = 15
    pygame.init()
    pygame.key.set_repeat(5, 5)
    SURFACE = pygame.display.set_mode((600, 800))
    FPSCLOCK = pygame.time.Clock()
    BLOCKS = []
    PADDLE = Block((242, 242, 0), Rect(300, 700, 100, 30))
    BALL = Block((242, 242, 0), Rect(300, 400, 20, 20), 10)
    def main():
        """ Main routine ""
        myfont = pygame.font.SysFont(None, 80)
        mess_clear = myfont.render("Cleared!", True, (255, 255, 0))
        mess_over = myfont.render("Game Over!", True, (255, 255, 0))
        fps = 30
        colors = [(255, 0, 0), (255, 165, 0), (242, 242, 0), (0, 128, 0), (128, 0, 128), (0,0, 250)]
        for ypos, color in enumerate(colors, start = 0):
            for xpos in range(0, 5):
                BLOCKS.append(Block(color,Rect(xpos * 100 + 60, ypos * 50 + 40, 80, 30)))
        while True:
            tick()
            SURFACE.fill(0,0,0)) # Background Color
            BALL.draw()
            PADDLE.draw()
            for block in BLOCKS:
                block.draw()
            if len(BLOCKS) == 0:
                SURFACE.blit(mess_clear, (200, 400))
            if BALL.rect.centery > 800 and len(BLOCKS) > 0:
                SURFACE.blit(mess_over, (150, 400))
            pygame.display.update()
            FPSCLOCK.tick(fps)
if __name__ == '__main__':
    main()

pygame python

2022-09-20 19:04

1 Answers

The problem occurs because the part from the pygame.init() line to the if__name__ is included in the Block class definition.

Reduce the indentation of the area by one level.


2022-09-20 19:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.