0

I have two files: sprites.py and test.py. In sprites.py, I made a Player class. I imported everything into the main file (test.py) using from player import *.

sprites.py:

import pygame
from test import *

class Player(pygame.sprite.Sprite):
    # ...
    pass

test.py:

import pygame
from sprites import *

# ...

all_sprites = pygame.sprite.Group()
player = Player()                       #  <-- line 28, NameError
all_sprites.add(player)

I get this error:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    from sprites import *
  File "C:\Users\Adam\Desktop\PYthon Test\sprites.py", line 2, in <module>
    from test import *
  File "C:\Users\Adam\Desktop\PYthon Test\test.py", line 28, in <module>
    player = Player()
NameError: name 'Player' is not defined

I have already ensured that I have everything spelt correctly. Where could this error be coming from?

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
Ballo Adam
  • 105
  • 1
  • 8
  • always show full error message (Traceback) - there are other useful information. Show filenames - sometimes there is problem because there is another file with the same name and Python imports it instead of your file. All files should be in the same folder. Or maybe you have wrong indentions and you define class Player() inside function or class and then it is not available. – furas Nov 18 '17 at 18:45
  • This is the error: https://imgur.com/a/HIJVs – Ballo Adam Nov 18 '17 at 18:48
  • it seems you use `Player()` in `test.py` (in line 28) - did you import `player.py` in `test.py` ? – furas Nov 18 '17 at 19:09
  • I did import the file where the player class is located. The file is called sprites, and I have imported it using ' from sprites import * ' in test.py `from sprites import *` – Ballo Adam Nov 18 '17 at 19:16
  • in question you said that class `Player` is in `player.py`, not `sprites.py`. Are you sure class `Player` is in `sprites.py` ? – furas Nov 18 '17 at 19:18
  • Yes, I have changed that along the way and used `from sprites import *` but still get the same error – Ballo Adam Nov 18 '17 at 19:21
  • if you have class in `sprites` and you import `sprites` then you can try to use `import sprites` and `print(sprites.__file__)` to see if Python imports correct file. Or you will have to show all code - you could put all on GitHub. – furas Nov 18 '17 at 19:35
  • Here are the files: https://github.com/Frago23/frago1 – Ballo Adam Nov 18 '17 at 19:48
  • I tested it - problem is `from test import *` in `sprite.py` - importing "A in B" and "B in A" is not preferred method - it sometimes makes problem. If you have to use `screen` in `Player` then send it as argument `Player(screen)`, (`def __init__(self, screen)`) not use it directly, and then you don't need `from test import *`. – furas Nov 18 '17 at 19:53
  • BTW: `draw.rect` is for drawing, not for creating image. You have `Surface` to creat image. – furas Nov 18 '17 at 19:57
  • I think I have fixed that problem, now if I want to load an image I simply use `pygame.image.load('name')` inside `sprites` `init` and then I blit it using `self.image.blit(screen, [coords])` is that right? – Ballo Adam Nov 18 '17 at 20:14
  • if you use `group.draw()` then it uses `self.image` and `self.rect` to blit it and you don't have to do it manually. – furas Nov 18 '17 at 20:16
  • BTW: `pygame.image.load` creates `Surface` - so for test I use `Surface` directly and everyone can easily test it without image file. – furas Nov 18 '17 at 20:18

1 Answers1

1

Problem was from test import * in sprite.py - there was circular import which sometimes makes problem

My version with some other modifications.

test.py

import pygame
from sprites import *

# --- constants --- (UPPER_CASE_NAMES)

WHITE = (255,255,255)
BLACK = (0,0,0)

WIDTH = 900
HEIGHT = 450

# --- main ---

# - init -

pygame.init()
#pygame.mixer.init() # pygame.init() should init mixer too
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption('game')

# - objects -

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

# - mainloop -

clock = pygame.time.Clock()
running = True

while running:

    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
        player.handle_event(event)

    # - updates - 

    all_sprites.update()

    # - draw -

    screen.fill(WHITE)
    all_sprites.draw(screen)
    pygame.display.flip()

    # - FPS -

    clock.tick(30)

# - end -

pygame.quit()

sprites.py

import pygame

class Player(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()

        # use Surface to create image
        self.image = pygame.Surface([40,40])
        #self.image.fill((0,0,0)) # surface is black as default so it don't have to be fill with black
        # `group.draw()` needs `self.image` and `self.rect`
        self.rect = self.image.get_rect(x=450, y=225)

        self.velocity = 9
        self.gravitiy = 3
        self.speedy = 0
        self.speedx = 0
        self.on_ground = True

    def handle_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.speedx = -5
            elif event.key == pygame.K_RIGHT:
                self.speedx = 5
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                self.speedx = 0
            elif event.key == pygame.K_RIGHT:
                self.speedx = 0

    def update(self):
        self.rect.x += self.speedx
furas
  • 134,197
  • 12
  • 106
  • 148