0

I am trying to pull the key of every value of 1 and 2, individually, and drawing a block to the screen. So I built a function that finds the dash in the given list so that I can extract the first and second numbers in the key and then calculates where it should draw the block, but when I run it, it gives an error message.

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

This is the error message:

 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

So why is it unable to call the list?

3
  • Does this answer your question? TypeError: 'list' object is not callable in python Commented Jul 10, 2021 at 3:57
  • Because lists aren't callable. Have you done some research into what "callable" means and why lists aren't? Iterating over elements of a list is covered by the most basic list tutorials, and SO is not meant to replace tutorials. Commented Jul 10, 2021 at 3:58
  • character_list is not a function, so you can't call it like one. Commented Jul 10, 2021 at 4:09

2 Answers 2

1

Given that character_list is already iterable, you should directly use:

for character in character_list:
    ...

You cannot call the list, as the list doesn't have a __call__ method.

Sign up to request clarification or add additional context in comments.

Comments

0

Just change this line for character in character_list():. to for character in character_list:

(): Define tuples, order of operations, generator expressions, function calls, and another syntax.

example:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.