🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

i need to fix these three errors

Started by
4 comments, last by Tom Sloper 3 years, 7 months ago
import pygame, sys

     # Setup pygame/window ---------------------------------------- #
     mainClock = pygame.time.Clock()
     from pygame.locals import *

  SCREEN_WIDTH = 1143
  SCREEN_HEIGHT = 650

pygame.image.load(r"C:\Users\ga-sa\Downloads\iniciar.png")


class Circuito(pygame.sprite.Sprite):

   def __init__(self):
    pygame.sprite.Sprite.__init__(self)

    self.images = [pygame.image.load(r'C:\Users\ga-sa\Downloads\00.png').convert_alpha(),
                   pygame.image.load(r'C:\Users\ga-sa\Downloads\01.png').convert_alpha(),
                   pygame.image.load(r"C:\Users\ga-sa\Downloads\02.png").convert_alpha(),
                   pygame.image.load(r'C:\Users\ga-sa\Downloads\03.png').convert_alpha()]

    self.current_image = 0

    self.image = pygame.image.load(r'C:\Users\ga-sa\Downloads\00.png').convert_alpha()
    self.rect = self.image.get_rect()
    self.rect[0] = 500
    self.rect[1] = 10

def update(self):
    self.current_image = (self.current_image + 1) % 4
    self.image = self.images[self.current_image]


pygame.init()
pygame.display.set_caption('game base')
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
iniciar = pygame.image.load(r"C:\Users\ga-sa\Downloads\iniciar.png")
config = pygame.image.load(r"C:\Users\ga-sa\Downloads\config.png")
nivel = pygame.image.load(r"C:\Users\ga-sa\Downloads\nivel.png")
sair = pygame.image.load(r"C:\Users\ga-sa\Downloads\sair.png")

BACKGROUND = pygame.image.load(r'C:\Users\ga-sa\Downloads\honeycomb.png')
BACKGROUND = pygame.transform.scale(BACKGROUND, (SCREEN_WIDTH, SCREEN_HEIGHT))

BACKGROUND_FASE = pygame.image.load(r"C:\Users\ga-sa\Downloads\FaseSemX_1.png")
BACKGROUND_FASE = pygame.transform.scale(BACKGROUND_FASE, (SCREEN_WIDTH, SCREEN_HEIGHT))

font = pygame.font.SysFont(None, 20)


def draw_text(text, font, color, surface, x, y):
   textobj = font.render(text, 1, color)
   textrect = textobj.get_rect()
   textrect.topleft = (x, y)
  surface.blit(textobj, textrect)


  click = False


def main_menu():
    while True:

    screen.fill((0, 0, 0))
    draw_text('main menu', font, (255, 255, 255), screen, 20, 20)

    mx, my = pygame.mouse.get_pos()

    button_1 = pygame.Rect(400, 50, 800, 200)
    button_2 = pygame.Rect(450, 500, 720, 180)
    if button_1.collidepoint((mx, my)):
        if click:
            game()
    if button_2.collidepoint((mx, my)):
        if click:
            options()

    pygame.draw.rect(screen, (255, 0, 0), button_1)
    pygame.draw.rect(screen, (255, 0, 0), button_2)

    click = False
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                click = True

    screen.blit(BACKGROUND, (0, 0))
    screen.blit(iniciar, (400, 50))
    screen.blit(config, (450, 500))
    screen.blit(nivel, (400, 280))
    screen.blit(sair, (400, 700))
    pygame.display.update()
    mainClock.tick(60)


def game():
running = True
while running:
    screen.fill((0, 0, 0))
    mx, my = pygame.mouse.get_pos()
    button_3 = pygame.Rect(300, 100, 1300, 700)
    if button_3.collidepoint((mx, my)):
        if click:
            begin()

    draw_text('game', font, (255, 255, 255), screen, 20, 20)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False

    # set the pygame window name

    SCREEN_WIDTH = 1500
    SCREEN_HEIGHT = 750
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    clock = pygame.time.Clock()

    background = pygame.image.load(r'C:\Users\ga-sa\Downloads\honeycomb.png')
    background = pygame.transform.scale(BACKGROUND, (SCREEN_WIDTH, SCREEN_HEIGHT))

    img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
    img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsXOR.png")
    img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsNOT.png")
    img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsAND.png")

    images = [img1, img2, img3, img4]

    current_image = -1
    img_rects = [images[i].get_rect(topleft=(20 + 80 * i, 20)) for i in range(len(images))]
    img_angles = [0 for _ in range(len(images))]

    lines = []
    line_start = None

    LeftButton = 0
    while 1:
        clock.tick(60)
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                pygame.quit()
                exit(0)

            if e.type == pygame.MOUSEBUTTONDOWN:
                mouse_rect = pygame.Rect(e.pos, (1, 1))
                current_image = mouse_rect.collidelist(img_rects)
                if line_start:
                    lines.append((line_start, e.pos))
                    line_start = None
                else:
                    line_start = e.pos

            if e.type == pygame.MOUSEMOTION:
                if e.buttons[LeftButton]:
                    rel = e.rel
                    if 0 <= current_image < len(images):
                        img_rects[current_image].x += rel[0]
                        img_rects[current_image].y += rel[1]

        keys = pygame.key.get_pressed()
        if 0 <= current_image < len(img_angles):
            if keys[pygame.K_RIGHT]:
                img_angles[current_image] -= 1
            if keys[pygame.K_LEFT]:
                img_angles[current_image] += 1

        screen.blit(background, (0, 0))

        for line in lines:
            pygame.draw.line(screen, pygame.Color('lawngreen'), *line)
        if line_start:
            pygame.draw.line(screen, pygame.Color('lawngreen'), line_start, pygame.mouse.get_pos())

        for i in range(len(images)):
            rotated_image = pygame.transform.rotate(images[i], img_angles[i])
            rotated_rect = rotated_image.get_rect(center=img_rects[i].center)
            screen.blit(rotated_image, rotated_rect)

        pygame.display.flip()
def begin():
running = True
while running:
    screen.fill((0, 0, 0))

    draw_text('begin', font, (255, 255, 255), screen, 20, 20)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.type == KEYDOWN:
                running = False

    pygame.display.update()
    mainClock.tick(60)


def options():
running = True
while running:
    screen.fill((0, 0, 0))

    draw_text('options', font, (255, 255, 255), screen, 20, 20)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False

    pygame.display.update()
    mainClock.tick(60)


main_menu()
the errors are :  File "C:/Users/ga-sa/PycharmProjects/pythonProject/venv/twstw.py", line 224, in

main_menu() File "C:/Users/ga-sa/PycharmProjects/pythonProject/venv/twstw.py", line 74, in main_menu

game() File "C:/Users/ga-sa/PycharmProjects/pythonProject/venv/twstw.py", line 107, in game

screen.fill((0, 0, 0)) UnboundLocalError: local variable 'screen' referenced before assignment
Advertisement

It's right there in the error message.

I get that it looks like you assign screen here, but the code - at least as it appears here, has several indentation issues.

I'm guessing screen isn't actually assigned when you need it, for instance you call main_menu before game.
Isn't game supposed to init stuff to use in the menu, or is game a single session gated by a menu?

the game init by the menu , so what i can do to fix this?

Set a breakpoint on the line going

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

Note that you have this and several other lines in the global scope here as well as within game().
This reeks of copy-paste. Don't repeat yourself.

Next, set a breakpoint on the line going

screen.fill((0, 0, 0))
(within main_menu - hopefully there aren't any indentation issues here, although it sure looks like it.)

See which line gets hit first, sort it out by making sure screen is assigned first.

Also, this is likely the wrong forum - this should be in the For Beginners -section.

This is not a Game Design question. Game Design and Programming are not the same. Moving to For Beginners.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement