繁体   English   中英

Python“列表”对象不可调用

[英]Python 'list' object not callable

我试图分别拉动 1 和 2 的每个值的键,并在屏幕上绘制一个块。 所以我构建了一个函数,在给定的列表中找到破折号,这样我就可以提取键中的第一个和第二个数字,然后计算它应该在哪里绘制块,但是当我运行它时,它给出了一条错误消息。

import pygame
import time
import os
import random
pygame.init()

screen = pygame.display.set_mode((1350, 700))
pygame.display.set_caption("Tetris")
ticks = 0
seconds = 1
block_states = {}
for a in range(1, 11):
    for b in range(1, 25):
        block_states[str(a)+"-"+str(b)] = 0
block_states['1-1'] = 2
block_states['7-9'] = 2
block_states['8-2'] = 1
# 0 is empty, 1 is locked block, 2 is falling block
block_types = ["Line-block", "T-block", "S-block", "Z-block", "Square-block"]
up_next = []


def find_character_position(character_list, desired_character):
    count = 0
    for character in character_list():
        if character == desired_character:
            return character
        count += 1


def find_key(dictionary, val):
    count = 0
    key_list = list(dictionary.keys())
    results = []
    for value in dictionary.values():
        if value == val:
            results.append(key_list[count])
        count += 1
    return results


def write_to_screen(text, red, green, blue, location_x, location_y):
    font = pygame.font.SysFont(None, 24)
    img = font.render(text, True, (red, green, blue))
    screen.blit(img, (location_x, location_y))


def find_abs_path(file_name):
    return str(os.getcwd()) + '/' + file_name


def write_instructions():
    write_to_screen("A and D to move left and right.", 255, 255, 255, 20, 20)
    write_to_screen("S for soft drop and W for hard drop.", 255, 255, 255, 20, 40)
    write_to_screen("Q and E for counter-clockwise and clockwise rotation.", 255, 255, 255, 20, 60)
    write_to_screen("Press C to hold.", 255, 255, 255, 20, 80)
    write_to_screen("I to show instructions again.", 255, 255, 255, 20, 100)
    write_to_screen("Esc to close the program", 255, 255, 255, 20, 120)
    pygame.display.update()
    time.sleep(10)


# Grid is 10 x 20 blocks
grid = pygame.image.load('tetris_grid.png')
block = pygame.image.load('block.png')
block = pygame.transform.scale(block, (32, 32))
square = pygame.image.load('square.png')
square = pygame.transform.scale(square, (32, 32))
screen.blit(grid, (525, 30))
origin_point = (x, y) = (527, 663.505984)
pygame.display.update()
write_instructions()
for i in range(0, 6):
    up_next.append(block_types[random.randint(0, 4)])

while True:
    new_keys = []
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
    screen.fill((0, 0, 0))
    screen.blit(grid, (525, 30))
    keys = find_key(block_states, 2)
    other_keys = find_key(block_states, 1)
    if not keys == []:
        locked_blocks_below = 0
        for a in range(0, len(keys)):
            current_key = list(keys[a])
            key_position = find_character_position(current_key, "-")
            if len(str(keys[a])) == 3:
                second_number = int(current_key[2])
                first_number = int(current_key[0])
            elif len(str(keys[a])) == 4 and key_position == 1:
                second_number = int(str(current_key[2]) + str(current_key[3]))
                first_number = int(current_key[0])
            elif len(str(keys[a])) == 4 and key_position == 2:
                second_number = int(current_key[3])
                first_number = int(str(current_key[0]) + str(current_key[1]))
            else:
                second_number = int(str(current_key[3]) + str(current_key[4]))
                first_number = int(str(current_key[0]) + str(current_key[1]))
            x_coordinate = x - ((first_number - 1) * 33.5)
            y_coordinate = y - ((second_number - 1) * 33.5)
            screen.blit(block, (x_coordinate, y_coordinate))
    if not other_keys == []:
        locked_blocks_below = 0
        for a in range(0, len(other_keys)):
            current_key = list(other_keys[a])
            key_position = find_character_position(current_key, "-")
            if len(str(other_keys[a])) == 3:
                second_number = int(current_key[2])
                first_number = int(current_key[0])
            elif len(str(other_keys[a])) == 4 and key_position == 1:
                second_number = int(str(current_key[2]) + str(current_key[3]))
                first_number = int(current_key[0])
            elif len(str(other_keys[a])) == 4 and key_position == 2:
                second_number = int(current_key[3])
                first_number = int(str(current_key[0]) + str(current_key[1]))
            else:
                second_number = int(str(current_key[3]) + str(current_key[4]))
                first_number = int(str(current_key[0]) + str(current_key[1]))
            x_coordinate = x - ((first_number - 1) * 33.5)
            y_coordinate = y - ((second_number - 1) * 33.5)
            screen.blit(square, (x_coordinate, y_coordinate))

    pygame.display.update()
    time.sleep(seconds)
    ticks += 1

这是错误消息:

 File "/home/jon/PycharmProjects/pythonProject2/Projects/Games/PseudoTetris/tetris_graphics.py", line 92, in <module>
    key_position = find_character_position(current_key, "-")
  File "/home/jon/PycharmProjects/pythonProject2/Projects/Games/PseudoTetris/tetris_graphics.py", line 25, in find_character_position
    for character in character_list():
TypeError: 'list' object is not callable

Process finished with exit code 1

那么为什么它无法调用列表呢?

鉴于character_list已经是可迭代的,你应该直接使用:

for character in character_list:
    ...

您不能调用该列表,因为该列表没有__call__方法。

只需for character in character_list():这一行更改for character in character_list(): for character in character_list:

() :定义元组、操作顺序、生成器表达式、函数调用和其他语法。

例子:

character_list = [1,2,3] #list
for i in character_list:
    print(i)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM