Why am I getting a nameerror from a variable that's in a file I imported?

37 Views Asked by At
import pygame, sys
from settings import *

#initialize pygame
pygame.init()

#variables
screen = pygame.display.set_mode((screen_width, screen_height))
run = True
clock = pygame.time.Clock()

#game loop
while run == True:

#event handler
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    screen.fill("black")
    
    pygame.display.update()
# framerate     |
    clock.tick(60)

pygame.quit()

Here's my first file. Screen_width and screen_height are both not showing as having an error, but when I try to run my code, I get this message:

Traceback (most recent call last):
  File "c:\Users\missu\.vscode\Python Files\Games(Using Pygame)\2d platformer(mario) - Clear Code\main.py", line 8, in <module>
    screen = pygame.display.set_mode((screen_width, screen_height))
                                      ^^^^^^^^^^^^
NameError: name 'screen_width' is not defined

Here's my second file(the one I imported):

level_map = [
"                             ",
"                             ",
"                             ",
" XX    XXX             XX    ",
" XX                          ",
" XXXX          XX         XX ",
" XXXX        XX              ",
" XX    X   XXXX    XX  XX    ",
"      X   XXXX    XX  XXX    ",
"   XXXX   XXXXXX  XX  XXXX   ",
"XXXXXXX   XXXXXX  XX  XXXX   "]

height = len(level_map)
tile_size = 64
screen_width = 1200
screen_height = tile_size * height

I'm following a tutorial on Clear Code on how to make a platformer game, so I tried restarting the video and checking if I had every line the same. I checked, and everything was the same. I tried importing the file different ways, and nothing I tried worked. If someone can help me, I would be so thankful.

0

There are 0 best solutions below