gpt4 book ai didi

python - 我的代码中不断出现错误,无法弄清楚

转载 作者:行者123 更新时间:2023-12-03 19:10:03 26 4
gpt4 key购买 nike

错误:

File "D:/Users/44749/PycharmProjects/untitled/venv/Game.py", line 271, in draw_messages
text_height = helper_text_height(Constants.FONT_MESSAGE_TEXT) TypeError: helper_text_height() takes 0 positional arguments but 1 was given
我正在尝试在屏幕上显示一些文本但是我一直收到这个错误而且我似乎无法解决它有人知道我可以做些什么来解决这个问题吗?
import pygame

#game files
import Constants


# _____ _____ _______ _____ _ _ _____ _______
# / ____|/ ____|__ __| __ \| | | |/ ____|__ __|
#| (___ | | | | | |__) | | | | | | |
# \___ \| | | | | _ /| | | | | | |
# ____) | |____ | | | | \ \| |__| | |____ | |
#|_____/ \_____| |_| |_| \_\\____/ \_____| |_|

class struc_Tile:
def __init__(self, block_path):
self.block_path = block_path #Creates a place where the player cant walk to
self.explored = False


# ____ ____ _ ______ _____ _______ _____
# / __ \| _ \ | | ____/ ____|__ __/ ____|
#| | | | |_) | | | |__ | | | | | (___
#| | | | _ < _ | | __|| | | | \___ \
#| |__| | |_) | |__| | |___| |____ | | ____) |
# \____/|____/ \____/|______\_____| |_| |_____/

class obj_Actor:
def __init__(self, x, y, name_object, sprite, creature = None, ai = None):
self.x = x #Map address
self.y = y #Map address
self.sprite = sprite

self.creature = creature
if creature:
creature.owner = self

self.ai = ai
if ai:
ai.owner = self

def draw(self):
is_visible = libtcodpy.map_is_in_fov(FOV_MAP, self.x, self.y)

if is_visible:
SURFACE_MAIN.blit(self.sprite, (self.x*Constants.CELL_WIDTH, self.y*Constants.CELL_HEIGHT))







# ____ ___ __ __ ____ ___ _ _ _____ _ _ _____ _____ ____
# / ___/ _ \| \/ | _ \ / _ \| \ | | ____| \ | | ____|_ _/ ___|
#| | | | | | |\/| | |_) | | | | \| | _| | \| | _| | | \___ \
#| |__| |_| | | | | __/| |_| | |\ | |___| |\ | |___ | | ___) |
# \____\___/|_| |_|_| \___/|_| \_|_____|_| \_|_____| |_| |____/

class com_Creature:

#Creatures have health, and can damage other objects by attacking them. can also die
def __init__(self, name_instance, hp = 10, death_function = None):

self.name_instance = name_instance
self.maxhp = hp
self.hp = hp
self.death_function = death_function



#self.x + dx]
#Where you are + Where you want to go
#Are you able to move to the palce you want to go
def move(self, dx, dy):

title_is_wall = (GAME_MAP[self.owner.x + dx][self.owner.y + dy].block_path == True)

target = map_check_for_creatures(self.owner.x + dx, self.owner.y + dy, self)

for object in GAME_OBJECTS:
if (object is not self.owner and
object.x == self.owner.x + dx and
object.y == self.owner.y + dy and
object.creature):
target = object
break

if target:
print (self.name_instance + " Attacks " + target.creature.name_instance + " for 5 damage!")
target.creature.take_damage(5)

if not title_is_wall and target is None:
self.owner.x += dx
self.owner.y += dy



def take_damage(self, damage):
self.hp -= damage
print (self.name_instance + "'s health is " + str(self.hp) + "/" + str(self.maxhp))

if self.hp <= 0:

if self.death_function is not None:
self.death_function(self.owner)
#TODO class com_Item:

#TODO lass com_Container:


# _ ___
# / \ |_ _|
# / _ \ | |
# / ___ \ | |
#/_/ \_\___|

class ai_test:

#Once per turn, execute
def take_turn(self):
self.owner.creature.move(libtcodpy.random_get_int(0, -1, 1),libtcodpy.random_get_int(0, -1, 1))

def death_monster(monster):

print (monster.creature.name_instance + " is dead ")

monster.creature = None
monster.ai = None



# __ __ _____
#| \/ | /\ | __ \
#| \ / | / \ | |__) |
#| |\/| | / /\ \ | ___/
#| | | |/ ____ \| |
#|_| |_/_/ \_\_|
def map_create():
new_map = [[ struc_Tile(False) for y in range (0, Constants.MAP_HEIGHT)] for x in range(0, Constants.MAP_WIDTH) ]

#Creates the actual path block so if its set to tru you cant walk there if its set to false you can walk there
#Also this is where you create new walls floors etc etc
new_map[10][10].block_path = True
new_map[10][15].block_path = True

for x in range(Constants.MAP_WIDTH):
new_map[x][0].block_path = True
new_map[x][Constants.MAP_HEIGHT-1].block_path = True

for y in range(Constants.MAP_HEIGHT):
new_map[0][y].block_path = True
new_map[Constants.MAP_WIDTH-1][y].block_path = True

map_make_fov(new_map)

return new_map

def map_check_for_creatures(x, y, exclude_object = None):

target = None

if exclude_object:

for object in GAME_OBJECTS:
if (object is not exclude_object and
object.x == x and
object.y == y and
object.creature):
target = object
break

if target:
return target
else:

for object in GAME_OBJECTS:
if (object is not exclude_object and
object.x == x and
object.y == y and
object.creature):
target = object
break

if target:
return target

def map_make_fov(incoming_map):
global FOV_MAP

FOV_MAP = libtcodpy.map_new(Constants.MAP_WIDTH, Constants.MAP_HEIGHT)

for y in range(Constants.MAP_HEIGHT):
for x in range(Constants.MAP_WIDTH):
libtcodpy.map_set_properties(FOV_MAP,x ,y,
not incoming_map[x][y].block_path, not incoming_map[x][y].block_path)


def map_calculate_fov():
global FOV_CALCULATE

if FOV_CALCULATE:
FOV_CALCULATE = False
libtcodpy.map_compute_fov(FOV_MAP, PLAYER.x, PLAYER.y, Constants.TORCH_RADIUS, Constants.FOV_LIGHT_WALLS,
Constants.FOV_ALGO)


# ____ ____ ___ _____ _ _ ____
#| _ \| _ \ / \ \ / /_ _| \ | |/ ___|
#| | | | |_) | / _ \ \ /\ / / | || \| | | _
#| |_| | _ < / ___ \ V V / | || |\ | |_| |
#|____/|_| \_\/_/ \_\_/\_/ |___|_| \_|\____|

def draw_game():

global SURFACE_MAIN

#Clear the surface -- if you dont clear it, it will just draw over the last surface
SURFACE_MAIN.fill(Constants.COLOR_DEFAULT_BG)



#Draw the map
draw_map(GAME_MAP)


#Draw all Objects
for obj in GAME_OBJECTS:
obj.draw()

draw_debug()
draw_messages()

#Update the display
pygame.display.flip()

def draw_map(map_to_draw):

for x in range(0, Constants.MAP_WIDTH):
for y in range(0, Constants.MAP_HEIGHT):

is_visible = libtcodpy.map_is_in_fov(FOV_MAP, x, y)

if is_visible:

map_to_draw[x][y].explored = True

if map_to_draw[x][y].block_path == True:
#draw wall
SURFACE_MAIN.blit(Constants.S_WALL, ( x*Constants.CELL_WIDTH, y*Constants.CELL_HEIGHT))
else:
#draw floor
SURFACE_MAIN.blit(Constants.S_FLOOR, (x * Constants.CELL_WIDTH, y*Constants.CELL_HEIGHT))

elif map_to_draw[x][y].explored:
if map_to_draw[x][y].block_path == True:
# draw wall
SURFACE_MAIN.blit(Constants.S_WALLEXPLORED, (x * Constants.CELL_WIDTH, y * Constants.CELL_HEIGHT))
else:
# draw floor
SURFACE_MAIN.blit(Constants.S_FLOOREXPLORED, (x * Constants.CELL_WIDTH, y * Constants.CELL_HEIGHT))

def draw_debug():

draw_text(SURFACE_MAIN, "fps:" + str(int(CLOCK.get_fps())) , (0,0), Constants.COLOR_WHITE, Constants.COLOR_BLACK)

def draw_messages():

to_draw = GAME_MESSAGES

text_height = helper_text_height(Constants.FONT_MESSAGE_TEXT)

start_y = Constants.MAP_HEIGHT*Constants.CELL_HEIGHT - (Constants.NUM_MESSAGES * text_height)

i = 0

for message, color in to_draw:

draw_text(SURFACE_MAIN, message, (0, start_y + (i * text_height), 0), color, Constants.COLOR_BLACK)

i += 1



def draw_text(display_surface, text_to_display, T_coords, text_color, back_color = None):
### this function takes in some text, and displays it on the referenced surface.

text_surface, text_rect = helper_text_objects(text_to_display, text_color, back_color)

text_rect.topleft = T_coords

display_surface.blit(text_surface, text_rect)


# _ _ _
# | | | | | |
# | |__| | ___| |_ __ ___ _ __
# | __ |/ _ \ | '_ \ / _ \ '__|
# | | | | __/ | |_) | __/ |
# |_| |_|\___|_| .__/ \___|_|
# | |
# |_|

def helper_text_objects(incoming_text, incoming_color, incoming_bg):

if incoming_bg:
Text_surface = Constants.FONT_DEBUG_MESSAGE.render(incoming_text, False, incoming_color, incoming_bg)

else:
Text_surface = Constants.FONT_DEBUG_MESSAGE.render(incoming_text, False, incoming_color)

return Text_surface, Text_surface.get_rect()

def helper_text_height():

font_object = font.render("a", False, (0, 0, 0))
font_rect = font_object.get_rect()

return font_rect.height



# _____ __ __ ______
# / ____| /\ | \/ | ____|
#| | __ / \ | \ / | |__
#| | |_ | / /\ \ | |\/| | __|
#| |__| |/ ____ \| | | | |____
# \_____/_/ \_\_| |_|______|

def game_main_loop():

game_quit = False

while not game_quit:

#player action definition
player_action = "no-action"

#handle player input
player_action = game_handle_keys()

map_calculate_fov()

if player_action == "QUIT":
game_quit = True

elif player_action != "no-action":
for obj in GAME_OBJECTS:
if obj.ai:
obj.ai.take_turn()


#Draw game
draw_game()

CLOCK.tick(Constants.GAME_FPS)
#Quit game
pygame.quit()
exit()


def game_initialize():

global SURFACE_MAIN, GAME_MAP, PLAYER, ENEMY, GAME_OBJECTS, FOV_CALCULATE, CLOCK, GAME_MESSAGES

pygame.init()

CLOCK = pygame.time.Clock()

SURFACE_MAIN = pygame.display.set_mode( (Constants.MAP_WIDTH*Constants.CELL_WIDTH,
Constants.MAP_HEIGHT*Constants.CELL_HEIGHT ) )

GAME_MAP = map_create()

GAME_MESSAGES = []

game_message("test message", Constants.COLOR_WHITE)

FOV_CALCULATE = True

creature_com1 = com_Creature("Human")

PLAYER = obj_Actor(1, 1, "Human", Constants.S_PLAYER, creature = creature_com1,)


creature_com2 = com_Creature("Thief", death_function = death_monster)

ai_com = ai_test()

ENEMY = obj_Actor(15, 15, "Bandit", Constants.S_ENEMY,
creature = creature_com2, ai = ai_com)
#Where the enemy is placed


GAME_OBJECTS = [PLAYER, ENEMY]

def game_handle_keys():
global FOV_CALCULATE
#Get player input
events_list = pygame.event.get()

# Process input
for event in events_list:
if event.type == pygame.QUIT:
return "QUIT"

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
PLAYER.creature.move(0, -1)
FOV_CALCULATE = True
return "player-moved"

if event.key == pygame.K_DOWN:
PLAYER.creature.move(0, 1)
FOV_CALCULATE = True
return "player-moved"

if event.key == pygame.K_RIGHT:
PLAYER.creature.move(1, 0)
FOV_CALCULATE = True
return "player-moved"

if event.key == pygame.K_LEFT:
PLAYER.creature.move(-1, 0)
FOV_CALCULATE = True
return "player-moved"
return "no-action"

def game_message(game_msg, msg_color):

GAME_MESSAGES.append((game_msg, msg_color))


#EXECUTION CODE
if __name__ == "__main__":
game_initialize()
game_main_loop()

最佳答案

错误说明了,函数 helper_text_height不接受任何参数:

def helper_text_height():
...
但是你用 draw_messages 中的参数调用了它功能:
text_height = helper_text_height(Constants.FONT_MESSAGE_TEXT)
也许你想做的是在函数的定义中:
def helper_text_height():

font_object = font.render("a", False, (0, 0, 0))
font_rect = font_object.get_rect()

return font_rect.height
您想添加一个参数 font像这样:
def helper_text_height(font):

font_object = font.render("a", False, (0, 0, 0))
font_rect = font_object.get_rect()

return font_rect.height

关于python - 我的代码中不断出现错误,无法弄清楚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62505163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com