У меня возникла ошибка
recursionerror: maximum recursion depts exceeded.
при создании игры на pygame и я не могу ее решить.
Ошибка: RecursionError: maximum recursion depth exceeded.
Код:
game.py
import pygame
from gun import Gun
from pygame.sprite import Group
import controlls
def run():
pygame.init()
screen = pygame.display.set_mode((700, 800))
pygame.display.set_caption("Game Space")
bg_color = (0, 0, 0)
gun = Gun(screen)
bullets = Group()
while True:
controlls.events(screen, gun, bullets)
controlls.update(bg_color, screen, gun, bullets)
gun.update_gun()
controlls.update_bullets(bullets)
run()
gun.py
import pygame
class Gun:
def __init__(self, screen):
# Инициализация пушки
self.screen = screen
self.image = pygame.image.load("image/gun.png")
self.rect = self.image.get_rect()
self.screen_rect = self.screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.center = float(self.rect.centerx)
self.rect.bottom = self.screen_rect.bottom
self.mright = False
self.mleft = False
def output(self):
# Отрисование пушки
self.screen.blit(self.image, self.rect)
def update_gun(self):
# Обновление позиции пушки
if self.mright and self.rect.right < self.screen_rect.right:
self.center += 1.5
if self.mleft and self.rect.left > 0:
self.center -= 1.5
self.rect.centerx = self.center
controlls.py
import pygame, sys
from bullet import Bullet
def events(gun, screen, bullet):
# Оброботка событий
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
gun.mright = True
elif event.key == pygame.K_LEFT:
gun.mleft = True
elif event.key == pygame.K_SPACE:
new_bullet == Bullet(screen, gun)
bullets.add(new_bullet)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
gun.mright = False
elif event.key == pygame.K_LEFT:
gun.mleft = False
def update(bg_color, screen, gun, bullets):
# Обновление экрана
screen.fill(bg_color)
for bullet in bullets.sprites():
bullet.draw_bullet()
gun.output()
pygame.display.flip()
def update_bullets(bullets):
bullets.update()
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
bullet.py
import pygame
class Bullet(pygame.sprite.Sprite()):
def __init__(self, screen, gun):
# Инициализация спрайта пули
super(Bullet, self).__init__()
self.screen = screen
self.rect = pygame.Rect(0, 0, 2, 12)
self.color = 53, 149, 140
self.speed = 1.5
self.rect.centerx = gun.rect.centerx
self.rect.top = gun.rect.top
self.y = float(self.rect.y)
def update(self):
# Перемещение пули вверх
self.y -= self.speed
self.rect.y = self.y
def draw_bullet(self):
# Рисуем пулю на экране
pygame.draw.rect(self.screen, self.color, self.rect)