[Python] 2048 - Both number tiles are not moving as intended [duplicate]

Discussão em 'Python' iniciado por Stack, Março 3, 2025.

  1. Stack

    Stack Membro Participativo

    I am pretty new to pygame, after creating a few projects, I moved on try and make '2048'. So far, it's been going well, but now I have another problem:

    import pygame
    import random
    import time
    pygame.init()

    screen = pygame.display.set_mode((500, 500))
    pygame.display.set_caption("2048")

    grid = pygame.sprite.Group()
    t = time.time()
    gridlist = []
    gridlist_index = []
    Tilegroup = pygame.sprite.Group()
    sg_index = []
    sg = 0
    x_pos = 0
    y_pos = 0
    x_grid = 100
    y_grid = 100
    x_list = []
    y_list = []
    xy_dict = []
    empty_img = pygame.image.load("emptyBlock.svg")
    tiles = [pygame.image.load("2.svg"),
    pygame.image.load("4.svg"),
    pygame.image.load("8.svg"),
    pygame.image.load("16.svg"),
    pygame.image.load("64.svg"),
    pygame.image.load("128.svg"),
    pygame.image.load("256.svg")]


    def createlist():
    for f in range(4):
    x_list.append(x_grid + (100 * f))
    print(x_list, "x_list")


    def anim_move(x, y):
    print(" ")


    def find_sprite(inputx, inputy):
    for required in range(len(Tilegroup.sprites())):
    if Tilegroup.sprites()[required].rect.x == inputx and Tilegroup.sprites()[required].rect.y == inputy:
    break
    return required


    def move(dir):
    print('iop')
    strip = [0, 0, 0, 0]
    holder = 0
    if dir == 'u':
    strip = [0, 0, 0, 0]
    for h in range(4):
    for b in range(4):
    if gridlist[b + (h * 4)] == 0:
    print(b, ' =b',h, ' =h')
    else:
    element_index = gridlist[b + (h * 4)]
    gridlist[b + (h * 4)] = 0
    print(b, h, "b and h")
    move_sprite = Tilegroup.sprites()[find_sprite(b*100-56, h*100-57)]
    move_sprite.rect.y = ((strip + 1) * 100 + 57) - 100
    gridlist[b + strip * 4] = element_index
    strip += 1

    print(gridlist, strip)
    if dir == 'd':
    strip = [3, 3, 3, 3]
    x_index = 3
    y_index = 3
    for h in range(4):
    for b in range(4):
    if gridlist[x_index + (y_index * 4)] == 0:
    print(0)
    else:
    element_index = gridlist[x_index + (y_index * 4)]
    gridlist[x_index + (y_index * 4)] = 0
    move_sprite = Tilegroup.sprites()[find_sprite(b*100-56, h*100-57)]
    move_sprite.rect.y = ((strip[y_index] + 1)*100 + 57) - 100
    gridlist[x_index + strip[x_index] * 4] = element_index
    strip[x_index] -= 1
    x_index -= 1
    print(gridlist, strip)
    x_index = 3
    y_index -= 1
    if dir == 'l':
    strip = [0, 0, 0, 0]
    for h in range(4):
    for b in range(4):
    if gridlist[b + (h * 4)] == 0:
    print(0)
    else:
    element_index = gridlist[b + (h * 4)]
    gridlist[b + (h * 4)] = 0
    print("done")
    move_sprite = Tilegroup.sprites()[find_sprite(b*100-56, h*100-57)]
    move_sprite.rect.x = ((strip)*100 + 56)
    print(b, strip)
    gridlist[strip[h] + h * 4] = element_index
    strip[h] += 1
    print(gridlist, strip)
    if dir == 'r':
    x_index = 3
    y_index = 3
    strip = [3, 3, 3, 3]
    for h in range(4):
    x_index = 3
    for b in range(4):
    if gridlist[x_index + (y_index * 4)] == 0:
    # non
    a = 0
    else:
    element_index = gridlist[x_index + (y_index * 4)]
    print(x_index, y_index, " xy")
    gridlist[x_index + (y_index * 4)] = 0
    print(x_index, strip, " strip")
    print(strip[y_index], len(gridlist))
    move_sprite = find_sprite(b*100-56, h*100-57)

    Tilegroup.sprites()[move_sprite].rect.x = ((strip[y_index] + 1)*100 + 57) - 100

    gridlist[(strip[y_index] + y_index * 4)] = element_index
    strip[y_index] -= 1
    print(gridlist, strip)
    x_index -= 1
    y_index -= 1


    class Tile(pygame.sprite.Sprite):
    def __init__(self, image, x, y, tx):
    pygame.sprite.Sprite.__init__(self)
    self.image = image
    self.rect = image.get_rect()
    self.rect.center = (x, y)
    self.tx = self.rect.x - 56


    class Empty(pygame.sprite.Sprite):
    def __init__(self, image, x, y):
    pygame.sprite.Sprite.__init__(self)
    self.image = image
    self.rect = image.get_rect()
    self.rect.center = (x, y)


    for y in range(4):
    for x in range(4):
    emptyblock = Empty(empty_img, x_grid, y_grid)
    grid.add(emptyblock)
    x_grid += 100
    gridlist.append(0)
    x_grid = 100
    y_grid += 100


    def spawnblock():
    x_pos = random.choice(x_list)
    y_pos = random.choice(x_list)
    while gridlist[x_list.index(x_pos) + x_list.index(y_pos) * 4] != 0:
    x_pos = random.choice(x_list)
    y_pos = random.choice(x_list)
    sg_index = gridlist
    gridlist_index.clear()
    gridlist_index.append(x_list.index(x_pos))
    gridlist_index.append(x_list.index(y_pos))
    if random.randint(1, 10) <= 9:
    gridlist[gridlist_index[0] + (gridlist_index[1] * 4)] = 2
    sg_index[gridlist_index[0] + (gridlist_index[1] * 4)] = len(Tilegroup.sprites()) + 1
    tile = Tile(tiles[0], x_pos, y_pos, len(Tilegroup.sprites()))
    else:
    gridlist[gridlist_index[0] + (gridlist_index[1] * 4)] = 4
    sg_index[gridlist_index[0] + (gridlist_index[1] * 4)] = len(Tilegroup.sprites()) + 1
    tile = Tile(tiles[1], x_pos, y_pos, len(Tilegroup.sprites()))
    print(sg_index, "sg_index")
    Tilegroup.add(tile)
    print(Tilegroup.sprites()[len(Tilegroup.sprites()) - 1].tx, '= x',Tilegroup.sprites()[len(Tilegroup.sprites()) - 1].rect.y )


    createlist()
    spawnblock()
    spawnblock()


    run = True

    while run:
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    run = False
    if event.type == pygame.KEYDOWN:
    print(event.key, "event key")
    if event.key == pygame.K_UP:
    move('u')
    if event.key == pygame.K_DOWN:
    move('d')
    if event.key == pygame.K_LEFT:
    move('l')
    if event.key == pygame.K_RIGHT:
    move('r')

    screen.fill((195, 190, 170))
    grid.draw(screen)
    Tilegroup.draw(screen)
    pygame.display.update()



    The code given above (poor readability, ik) works as intended except in the movement scripts, only one of my tile sprites is moving, while the other seems to be in the same position. When running, the tiles display properly on the screen, but when moving them using the arrow keys, only one of them is moving. Is it because the same sprite is appended to the tile group twice, or am I missing something?

    Tried to code all tiles on the screen to move, just like in the game. Expected both of them to move, but only one shows movement.

    def move(dir):
    print('iop')
    strip = [0, 0, 0, 0]
    holder = 0
    if dir == 'u':
    strip = [0, 0, 0, 0]
    for h in range(4):
    for b in range(4):
    if gridlist[b + (h * 4)] == 0:
    print(b, ' =b',h, ' =h')
    else:
    element_index = gridlist[b + (h * 4)]
    gridlist[b + (h * 4)] = 0
    print(b, h, "b and h")
    move_sprite = Tilegroup.sprites()[find_sprite(b*100-56, h*100-57)]
    move_sprite.rect.y = ((strip + 1) * 100 + 57) - 100
    gridlist[b + strip * 4] = element_index
    strip += 1

    print(gridlist, strip)
    if dir == 'd':
    strip = [3, 3, 3, 3]
    x_index = 3
    y_index = 3
    for h in range(4):
    for b in range(4):
    if gridlist[x_index + (y_index * 4)] == 0:
    print(0)
    else:
    element_index = gridlist[x_index + (y_index * 4)]
    gridlist[x_index + (y_index * 4)] = 0
    move_sprite = Tilegroup.sprites()[find_sprite(b*100-56, h*100-57)]
    move_sprite.rect.y = ((strip[y_index] + 1)*100 + 57) - 100
    gridlist[x_index + strip[x_index] * 4] = element_index
    strip[x_index] -= 1
    x_index -= 1
    print(gridlist, strip)
    x_index = 3
    y_index -= 1
    if dir == 'l':
    strip = [0, 0, 0, 0]
    for h in range(4):
    for b in range(4):
    if gridlist[b + (h * 4)] == 0:
    print(0)
    else:
    element_index = gridlist[b + (h * 4)]
    gridlist[b + (h * 4)] = 0
    print("done")
    move_sprite = Tilegroup.sprites()[find_sprite(b*100-56, h*100-57)]
    move_sprite.rect.x = ((strip)*100 + 56)
    print(b, strip)
    gridlist[strip[h] + h * 4] = element_index
    strip[h] += 1
    print(gridlist, strip)
    if dir == 'r':
    x_index = 3
    y_index = 3
    strip = [3, 3, 3, 3]
    for h in range(4):
    x_index = 3
    for b in range(4):
    if gridlist[x_index + (y_index * 4)] == 0:
    # non
    a = 0
    else:
    element_index = gridlist[x_index + (y_index * 4)]
    print(x_index, y_index, " xy")
    gridlist[x_index + (y_index * 4)] = 0
    print(x_index, strip, " strip")
    print(strip[y_index], len(gridlist))
    move_sprite = find_sprite(b*100-56, h*100-57)

    Tilegroup.sprites()[move_sprite].rect.x = ((strip[y_index] + 1)*100 + 57) - 100

    gridlist[(strip[y_index] + y_index * 4)] = element_index
    strip[y_index] -= 1
    print(gridlist, strip)
    x_index -= 1
    y_index -= 1


    Given above is the movement function. Up next is the method I used to find the right sprite (since all sprite names are pretty much the same)

    def find_sprite(inputx, inputy):
    for required in range(len(Tilegroup.sprites())):
    if Tilegroup.sprites()[required].rect.x == inputx and Tilegroup.sprites()[required].rect.y == inputy:
    break
    return required

    Continue reading...

Compartilhe esta Página