NameError: name 'Bullet' is not defined
Я создаю игру по типу шутера с видом сверху в учебнике ускоренного курса Python и получаю ошибку имени, в которой говорится, что NameError: name 'Bullet' is not defined. сама Bullet создана как отдельный файл entities.py, в общем при стрельбе у меня выходит данная ошибка, я совсем еще новичок в программировании. Вот мой код на данный момент: #main.py
import pygame
from sys import exit
from gui import *
from entities import *
from player import Player
#...
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("SnowDrop Combine Strike")
clock = pygame.time.Clock()
background = pygame.image.load("data/images/ground.png").convert()
'''
#US_RIFLEMAN_M4
class US_Rifleman_m4(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.hp = UNIT_HP
self.ammo = UNIT_AMMO
self.pos = pygame.math.Vector2(UNIT_START_X, UNIT_START_Y)
self.image = pygame.transform.rotozoom(
pygame.image.load("data/BLUEFOR/1 пехота/Т1 Регулярная пехота/Стрелок/T1_us_M4.png").convert_alpha(), 0,
UNIT_SIZE)
self.base_unit_image = self.image
self.hitbox_rect = self.base_unit_image.get_rect(center=self.pos)
self.rect = self.hitbox_rect.copy()
self.shoot = False
self.reloading = False
self.shoot_cooldown = 0
self.reload_cooldown = 0
self.direction = pygame.math.Vector2()
self.speed = UNIT_SPEED
def get_vector_distance(self, vector_1, vector_2):
return (vector_1 - vector_2).magnitude()
def die(self):
self.hp = 0
self.kill()
def hunt_player(self):
player_vector = pygame.math.Vector2(player.hitbox_rect.center)
unit_vector = pygame.math.Vector2(self.rect.center)
distance = self.get_vector_distance(player_vector, unit_vector)
if distance > 0:
self.direction = (player_vector - unit_vector).normalize()
else:
self.direction = pygame.math.Vector2()
self.velocity = self.direction * self.speed
self.pos += self.velocity
self.rect.centerx = self.pos.x
self.rect.centery = self.pos.y
def update(self):
self.hunt_player()'''
class Camera(pygame.sprite.Group):
def __init__(self):
super().__init__()
self.offset = pygame.math.Vector2()
self.floor_rect = background.get_rect(topleft=(0, 0))
def custom_draw(self):
self.offset.x = player.rect.centerx - (WIDTH // 2)
self.offset.y = player.rect.centery - (HEIGHT // 2)
# draw the floor
floor_offset_pos = self.floor_rect.topleft - self.offset
screen.blit(background, floor_offset_pos)
for sprite in all_sprites_group:
offset_pos = sprite.rect.topleft - self.offset
screen.blit(sprite.image, offset_pos)
all_sprites_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()
tres_sprites = pygame.sprite.Group()
tiles_group = pygame.sprite.Group()
camera = Camera()
player = Player()
#us_rifleman = US_Rifleman_m4()
all_sprites_group.add(player)
#all_sprites_group.add(us_rifleman)
#roflanishe
pygame.mixer.music.load("data/Music/AEG.mp3")
pygame.mixer.music.set_volume(0.05)
pygame.mixer.music.play(-1)
while True:
#us_rifleman.update()
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.mixer.music.stop()
screen.blit(background, (0, 0))
all_sprites_group.draw(screen)
all_sprites_group.update()
camera.custom_draw()
player_health_bar.hp = player.hp
player_health_bar.draw(screen)
player_ammo_bar.ammo = player.ammo
player_ammo_bar.draw(screen)
pygame.display.update()
clock.tick(FPS)
if player.ammo == 0:
player.reload()
#player.py
import pygame, math
from entities import *
from settings import *
pygame.mixer.init()
reload_sound = pygame.mixer.Sound("data/ogg/rf/reload.ogg")
reload_sound.set_volume(0.3)
shoot_sound = pygame.mixer.Sound("data/ogg/rf/ak74.ogg")
shoot_sound.set_volume(0.1) # adjust volume to your liking
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.hp = PLAYER_HP
self.ammo = PLAYER_AMMO
self.pos = pygame.math.Vector2(PLAYER_START_X, PLAYER_START_Y)
self.image = pygame.transform.rotozoom(
pygame.image.load("data/REDFOR/1пехота/Т3 Спецназ/Командир/T3_rus_val4.png").convert_alpha(), 0,
PLAYER_SIZE)
self.base_player_image = self.image
self.hitbox_rect = self.base_player_image.get_rect(center=self.pos)
self.rect = self.hitbox_rect.copy()
self.speed = PLAYER_SPEED
self.shoot = False
self.reloading = False
self.shoot_cooldown = 0
self.reload_cooldown = 0
# для поворотов мышей кароче
def player_rotation(self):
self.mouse_coords = pygame.mouse.get_pos() # используем координаты мышки, т.е ее положение
self.x_change_mouse_player = (self.mouse_coords[0] - (WIDTH // 2))
self.y_change_mouse_player = (self.mouse_coords[1] - (HEIGHT // 2))
self.angle = math.degrees(math.atan2(self.y_change_mouse_player, self.x_change_mouse_player))
self.image = pygame.transform.rotate(self.base_player_image, -self.angle)
self.rect = self.image.get_rect(center=self.hitbox_rect.center)
# Управление
def keyboard_input(self):
self.velocity_x = 0
self.velocity_y = 0
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.velocity_y = -self.speed
if keys[pygame.K_s]:
self.velocity_y = self.speed
if keys[pygame.K_d]:
self.velocity_x = self.speed
if keys[pygame.K_a]:
self.velocity_x = -self.speed
if keys[pygame.K_r]:
self.reload()
if self.velocity_x != 0 and self.velocity_y != 0: # это движение по диагонали
self.velocity_x /= math.sqrt(2)
self.velocity_y /= math.sqrt(2)
if pygame.mouse.get_pressed() == (1, 0, 0):
self.shoot = True
self.is_shooting()
else:
self.shoot = False
def is_shooting(self):
if self.shoot_cooldown == 0 and self.ammo > 0 and not self.reloading:
self.shoot_cooldown = SHOOT_COOLDOWN
spawn_bullet_pos = self.pos
self.bullet = Bullet(spawn_bullet_pos[0], spawn_bullet_pos[1], self.angle)
bullet_group.add(self.bullet)
all_sprites_group.add(self.bullet)
shoot_sound.play()
self.ammo -= 1
elif self.ammo == 0:
print("Перезаряжаюсь!")
def reload(self):
if not self.reloading:
self.shoot = False
self.reloading = True
self.reload_cooldown = 180 # 3 секунды
reload_sound.play()
print("Перезаряжаюсь!")
def move(self):
self.pos += pygame.math.Vector2(self.velocity_x, self.velocity_y)
self.hitbox_rect.center = self.pos
self.rect.center = self.hitbox_rect.center
def die(self):
self.hp = 0
self.kill()
def update(self):
self.keyboard_input()
self.move()
self.player_rotation()
if self.reloading:
self.reload_cooldown -= 1
if self.reload_cooldown <= 0:
self.reloading = False
self.ammo = PLAYER_AMMO
print("Перезарядился!")
if self.shoot_cooldown > 0:
self.shoot_cooldown -= 1
'''
def shoot(self):
if self.shoot_cooldown == 0 and self.ammo > 0 and not self.reloading:
self.shoot_cooldown = SHOOT_COOLDOWN
spawn_bullet_pos = self.pos
self.bullet = Bullet(spawn_bullet_pos[0], spawn_bullet_pos[1], self.angle)
bullet_group.add(self.bullet)
all_sprites_group.add(self.bullet)
shoot_sound.play()
self.ammo -= 1
'''
#entities.py
from main import bullet_group, all_sprites_group, player
from settings import *
import pygame, math, random
from player import Player
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, angle):
super().__init__()
self.image = pygame.image.load("data/PNG/Infantry/bala.png").convert_alpha()
self.image = pygame.transform.rotozoom(self.image, 0, BULLET_SCALE)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.x = x
self.y = y
self.angle = angle
self.speed = BULLET_SPEED
self.x_vel = math.cos(self.angle * (2 * math.pi / 360)) * self.speed
self.y_vel = math.sin(self.angle * (2 * math.pi / 360)) * self.speed
self.bullet_lifetime = BULLET_LIFETIME
self.damage = BULLET_DAMAGE
self.spawn_time = pygame.time.get_ticks()
def bullet_ambient(self):
self.x += self.x_vel
self.y += self.y_vel
self.rect.x = int(self.x)
self.rect.y = int(self.y)
if pygame.time.get_ticks() - self.spawn_time > self.bullet_lifetime:
self.kill()
def update(self):
self.bullet_ambient()
'''
# проверка коллизий с all_sprites_group
if pygame.sprite.spritecollide(self, all_sprites_group, False):
for sprite in all_sprites_group:
if isinstance(sprite, Player) or isinstance(sprite, US_Rifleman_m4):
sprite.hp -= self.damage
if sprite.hp <= 0:
sprite.die()
self.kill()
break
'''