0

I am getting a "most likely due to a circular import" when I try running my pygame. Please help me. I don't know why I am getting this. I would really appreciate the help guys I have tried "from car import Player" to see if that helps, but I am stuck. Like I said I would appreciate the answer on how to fix this minor issue. Im pulling my hair out right now.

autopilot.py code:

import pygame
import car

pygame.init()

#screen settings
WIDTH = 1000
HEIGHT = 400

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("AutoPilot")
screen.fill((255, 255, 255))

#fps
FPS = 120
clock = pygame.time.Clock()

#load images
bg = pygame.image.load('background/street.png').convert_alpha() # background

#define game variables


#debris class
class Debris(pygame.sprite.Sprite):
    def __init__(self, scale, speed):
        pygame.sprite.Sprite.__init__(self)

        self.x = 400
        self.y = HEIGHT / 2 - 200
        self.speed = speed
        self.vy = 0
        self.on_ground = True
        self.move = True
        self.health = 100
        self.max_health = self.health
        self.alive = True

        #load debris
        self.images = []
        img = pygame.image.load('debris/cement.png').convert_alpha()
        img = pygame.transform.scale(img, (int(img.get_width()) * scale, (int(img.get_height()) * scale)))
        self.images.append(img)
        self.image = self.images[0]
        self.rect = self.image.get_rect()

    #draw debris to screen
    def draw(self):
        screen.blit(self.image,(self.x,self.y))


######################CAR/DEBRIS##########################

player = car.Player(1, 5)
debris = Debris(1, 5)

##########################################################

#groups
bullet_group = pygame.sprite.Group()
debris_group = pygame.sprite.Group()

debris_group.add(debris)

#game runs here
run = True
while run:

    #draw street
    screen.blit(bg, [0, 0])

    #update groups
    bullet_group.update()
    bullet_group.draw(screen)

    #draw debris
    debris.draw()

    #draw car
    player.draw()
    player.move()

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

        #check if key is down
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False
            if event.key == pygame.K_a:
                player.movingLeft = True
            if event.key == pygame.K_d:
                player.movingRight = True
            if event.key == pygame.K_SPACE:
                player.shoot()

        #check if key is up
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                player.movingLeft = False
            if event.key == pygame.K_d:
                player.movingRight = False

    #update the display
    pygame.display.update()
    pygame.display.flip()
    clock.tick(FPS)

pygame.quit()

bullet.py code:

import pygame
from autopilot import debris, bullet_group

#load bullets
bullets = pygame.image.load('car/bullet.png').convert_alpha()

#bullet class
class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y, direction):
        pygame.sprite.Sprite.__init__(self)

        self.speed = 5
        self.image = bullets
        self.rect = self.image.get_rect()
        self.rect.center = (x,y)
        self.direction = direction

    def update(self):
        self.rect.centery -= self.speed
        #check if bullet has gone off screen
        if self.rect.top < 1:
            self.kill()
        #check collision with cement block
        if pygame.sprite.spritecollide(debris, bullet_group, False):
            if debris.alive:
                debris.health -= 25

car.py code:

import pygame
from autopilot import bullet_group, screen

#load bullets
bullets = pygame.image.load('car/bullet.png').convert_alpha()

#player class
class Player(pygame.sprite.Sprite):
    def __init__(self, scale, speed):
        pygame.sprite.Sprite.__init__(self)

        self.speed = speed
        #self.x = x
        #self.y = y
        self.moving = True
        self.frame = 0
        self.flip = False
        self.direction = 0

        #load car
        self.images = []
        img = pygame.image.load('car/car.png').convert_alpha()
        img = pygame.transform.scale(img, (int(img.get_width()) * scale, (int(img.get_height()) * scale)))
        self.images.append(img)
        self.image = self.images[0]
        self.rect = self.image.get_rect()
        self.update_time = pygame.time.get_ticks()
        self.movingLeft = False
        self.movingRight = False
        self.rect.x = 465
        self.rect.y = 325

    #draw car to screen
    def draw(self):
        screen.blit(self.image, (self.rect.centerx, self.rect.centery))

    #move car
    def move(self):
        #reset the movement variables
        dx = 0
        dy = 0

        #moving variables
        if self.movingLeft and self.rect.x > 33:
            dx -= self.speed
            self.flip = True
            self.direction = -1
        if self.movingRight and self.rect.x < 900:
            dx += self.speed
            self.flip = False
            self.direction = 1

        #update rectangle position
        self.rect.x += dx
        self.rect.y += dy

    #shoot
    def shoot(self):
        bullet = bullets.Bullet(self.rect.centerx + 18, self.rect.y + 30, self.direction)
        bullet_group.add(bullet)

debris.py code:

import pygame
from autopilot import HEIGHT, screen

#debris class
class Debris(pygame.sprite.Sprite):
    def __init__(self, scale, speed):
        pygame.sprite.Sprite.__init__(self)

        self.x = 400
        self.y = HEIGHT / 2 - 200
        self.speed = speed
        self.vy = 0
        self.on_ground = True
        self.move = True
        self.health = 100
        self.max_health = self.health
        self.alive = True

        #load debris
        self.images = []
        img = pygame.image.load('debris/cement.png').convert_alpha()
        img = pygame.transform.scale(img, (int(img.get_width()) * scale, (int(img.get_height()) * scale)))
        self.images.append(img)
        self.image = self.images[0]
        self.rect = self.image.get_rect()

    #draw debris to screen
    def draw(self):
        screen.blit(self.image,(self.x,self.y))

My error msg: Error Message

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
J.R. BEATS
  • 93
  • 6
  • 1
    autopilot.py depends on car.py, as you can see from the import. car.py also depends on autopilot.py. You should break your code into more files, and import as little as possible in the base building blocks. – Havin Leung Oct 31 '21 at 04:37
  • Besides that... What should I do to fix this problem? – J.R. BEATS Oct 31 '21 at 04:45
  • Thanks guys for the helps. I know what to do now. :) – J.R. BEATS Oct 31 '21 at 04:52
  • 1
    Take a look at the duplicate answer shown above. It contains a better explanation than my earlier comment did, and better information on how to fix it. – Tom Karzes Oct 31 '21 at 04:53
  • Please read [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Do not post screenshots of the error message. Paste the error trace. – Rabbid76 Oct 31 '21 at 06:55

0 Answers0