pygame: AttributeError: 'int' object has no attribute 'health_restart'
это main
import pygame
import random
import show_text
import loading
import window
import health
pygame.init()
FPS = 60
FPSclock = pygame.time.Clock()
display_width = 1920
display_height = 1080
display = pygame.display.set_mode((display_width, display_height),
pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN)
def run_game():
game = True
health = 100
health_max = 100
while game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
display.fill(12, 16, 10)
window.button_ghost_minus("- health", (39, 176, 11), (39, 176, 64), 'font4', 50, 855, 540, 1, 'health')
window.button_ghost("restart health", (39, 176, 11), (39, 176, 64), 'font4', 50, 855, 640, health.health_restart)
health.health_bar(health, health_max, (50, 50, 50), (50, 120, 30), 100, 100, 20)
pygame.display.update()
FPSclock.tick(FPS)
это window
import pygame
import show_text
import health
pygame.init()
FPS = 60
FPSclock = pygame.time.Clock()
display_width = 1920
display_height = 1080
display = pygame.display.set_mode((display_width, display_height),
pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN)
def button_ghost_minus(text, color_text1, color_text2, font, font_size, x, y, minus, obj):
width = len(text) * font_size
height = font_size
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x < mouse[0] < x + width and y < mouse[1] < y + height:
show_text.message(text, color_text2, x + 5, y + 5, font, font_size)
if click[0] == 1:
show_text.message(text, color_text1, x + 5, y + 5, font, font_size)
if action is not None:
if obj == 'health':
health.health1 -= minus
else:
pass
else:
show_text.message(text, color_text1, x + 5, y + 5, font, font_size)
это health
import pygame
import show_text
import window
display_width = 1920
display_height = 1080
display = pygame.display.set_mode((display_width, display_height),
pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN)
pygame.init()
health1 = 100
health1_max = 100
def health_bar_rect(color_max, color, x, y, height):
global health1
global health1_max
pygame.draw.rect(display, color_max, (health1_max + 10, height + 10, x, y))
pygame.draw.rect(display, color, (health1, height, x + 5, y + 5))
def health_restart():
global health1
global health1_max
health1 = health1_max
return health1
вот это show_text
import pygame
pygame.init()
display_width = 1920
display_height = 1080
display = pygame.display.set_mode((display_width, display_height),
pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN)
def message(text, color, x, y, font, font_size): # функция(текст, цвет, x позиция, y позиция, 'шрифт', размер шрифта)
font1 = pygame.font.Font('Monaco.ttf', font_size) # 'шрифт1'
font2 = pygame.font.Font('Marske.ttf', font_size) # 'шрифт2'
font3 = pygame.font.Font('hack.ttf', font_size) # 'шрифт3'
font4 = pygame.font.Font('ArcadeInterlaced.ttf', font_size) # 'шрифт4'
font_none = pygame.font.Font(None, font_size) # 'любое число или текст в кавычках'
if font == 'font1': # Для того, чтобы выбирать шрифты
show = font1.render(text, True, (color))
elif font == 'font2':
show = font2.render(text, True, (color))
elif font == 'font3':
show = font3.render(text, True, (color))
elif font == 'font4':
show = font4.render(text, True, (color))
else:
show = font_none.render(text, True, (color))
display.blit(show, [x, y]) # Выводит текст на экран в нужную позицию на экране
а это ошибка
window.button_ghost("restart health", (39, 176, 11), (39, 176, 64), 'font4', 50, 855, 640, health.health_restart)
AttributeError: 'int' object has no attribute 'health_restart'
Сразу скажу я разделила игру на модули, а вот когда сделала жизни то хотела затестить, но вылезла вот такая ошибка ПОМОГИТЕ 。・゚゚*(>д<)*゚゚・。
Ответы (1 шт):
У вас ошибка AttributeError: 'int' object has no attribute 'health_restart' в main.py. Из-за одинаковых имен импортированного модуля health.py и переменной health, смотрите:
import health
...
health = 100
...
window.button_ghost("restart health", (39, 176, 11), (39, 176, 64), 'font4', 50, 855, 640, health.health_restart)
В health -- у вас число, а конкретно health = 100, т.е. код у вас пытался сделать 100.health_restart. Нужно решить проблему с коллизией имен модуля и переменных.
Поэтому нужно исправить коллизии названия в main.py, например:
import pygame
import random
import show_text
import loading
import window
import health
pygame.init()
FPS = 60
FPSclock = pygame.time.Clock()
display_width = 1920
display_height = 1080
display = pygame.display.set_mode((display_width, display_height),
pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN)
def run_game():
game = True
health_current = 100
health_max = 100
while game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
display.fill(12, 16, 10)
window.button_ghost_minus("- health", (39, 176, 11), (39, 176, 64), 'font4', 50, 855, 540, 1, 'health')
window.button_ghost("restart health", (39, 176, 11), (39, 176, 64), 'font4', 50, 855, 640, health.health_restart)
health.health_bar(health_current, health_max, (50, 50, 50), (50, 120, 30), 100, 100, 20)
pygame.display.update()
FPSclock.tick(FPS)
PS.
А насчет классов... Можно завести класс Health, добавить ему свойства и методы.
Накидал простенький пример:
display = ...
class Health:
def __init__(self, value: int = 100, max_value: int = 100):
self.value = value
self.max_value = max_value
def restart(self):
self.value = self.max_value
def draw(self, color_max, color, x, y, height):
pygame.draw.rect(display, color_max, (self.max_value + 10, height + 10, x, y))
pygame.draw.rect(display, color, (self.value, height, x + 5, y + 5))
def __repr__(self):
return f'<Health value={self.value} max_value={self.max_value}>'
health = Health()
print(health)
# <Health value=100 max_value=100>
health.value -= 10
print(health)
# <Health value=90 max_value=100>
health.restart()
print(health)
# <Health value=100 max_value=100>
health = Health(value=50, max_value=150)
print(health)
# <Health value=50 max_value=150>