2

In this 2048 game I'm making I am moving my tiles(rects) to a new slot when I press movement keys. When I press a movement key it says the correct position in console when it prints it but I can't see it on the screen. Here is the spot most likely to have the error. (I am also trying to make the movement smooth)

rect = tiles[str(k)]
    for i in range(distance):
    print(i)
    rect.x += x
    rect.y += y
    print(rect.x, rect.y)
    pygame.display.update()

If you think it has something to do with other code or you want to look at it:

#IN MAIN LOOP
#WASD
        if event.type == pygame.KEYDOWN:
            if play == 1:
                if event.key == pygame.K_w:
                    y = -10
                    v = -4
                elif event.key == pygame.K_a:
                    x = -10
                    v = -1
                elif event.key == pygame.K_s:
                    y = 10
                    v = 4
                elif event.key == pygame.K_d:
                    x = 10
                    v = 1
                #ARROWS
                elif event.key == pygame.K_UP:
                    y = -10
                    v = -4
                elif event.key == pygame.K_LEFT:
                    x = -10
                    v = -1
                elif event.key == pygame.K_DOWN:
                    y = 10
                    v = 4
                elif event.key == pygame.K_RIGHT:
                    x = 10
                    v = 1
                else:
                    continue
                for k in range(1,17):
                    if str(k) in tiles.keys():
                        print(k)
                        """COMPRESS"""
                        #Find "k's row"
                        #x
                        if x != 0:
                            if k in range(1,5):
                                row_range = range(1,5)
                            elif k in range(5,9):
                                row_range = range(5,9)
                            elif k in range(9,13):
                                row_range = range(9,13)
                            else:
                                row_range = range(13,17)
                            print(row_range)
                            #Move as far as can go in direction
                            if k+(v*3) in row_range and used_tiles[str(k+v*3)] == False:
                                m = 3
                            elif k+(v*2) in row_range and used_tiles[str(k+v*2)] == False:
                                m = 2
                            elif k+v in row_range and used_tiles[str(k+v)] == False:
                                m = 1
                            else:
                                m = 0
                        #y
                        else:
                            if k+v*3 in range(1,17) and used_tiles[str(k+v*3)] == False:
                                m = 3
                            elif k+v*2 in range(1,17) and used_tiles[str(k+v*2)] == False:
                                m = 2
                            elif k+v in range(1,17) and used_tiles[str(k+v)] == False:
                                m = 1
                            else:
                                m = 0
                        print(m,"!")
                        if m != 0:
                            distance = 13*m
                            v = v*m
                            coordsX = (tile_slots[str(k+v)])[0]
                            coordsY = (tile_slots[str(k+v)])[1]
                            #Move rect
                            rect = tiles[str(k)]
                            #rect.move_ip(coordsX, coordsY)
                            for i in range(distance):
                                print(i)
                                rect.x += x
                                rect.y += y
                                print(rect.x, rect.y)
                                pygame.display.update()
                            """COMBINE"""
                            if used_tiles_n[str(int(k+v/m))] == used_tiles_n[str(k)]:
                                used_tiles_n[str(k+v)] = int(used_tiles_n[str(k)])*2
                            else:
                                used_tiles_n[str(k+v)] = used_tiles_n[str(k)]
                            #Draw text
                            if int(used_tiles_n[str(k)]) > 4:
                                text = font4.render(str(used_tiles_n[str(k+v)]), True, tile_text_c["above4_c"])
                            else:
                                text = font4.render(str(used_tiles_n[str(k)]), True, tile_text_c["2and4_c"])
                            game_board_s.blit(text, (coordsX+38, coordsY+26))
                            #Re-set dicts
                            tiles[str(k+v)] = rect
                            used_tiles[str(k+v)] = True
                    #Update
                    screen.blit(game_board_s,(49, 200))
                pygame.display.flip() 
shoowis
  • 19
  • 4
  • 1
    please provide a [mre] – Matiiss Jul 23 '22 at 08:52
  • @Matiiss I honestly think I did though... – shoowis Jul 23 '22 at 09:04
  • I believe you but I know for a fact that the code you have provided can be shortened a lot, to only include what you ask about, there's no need for fonts, no need for tile data, no need for all the inputs, no need for a bunch of stuff, it should just be basically only the part you located as the problematic one but enough that anyone can run it without changes and get the same issue – Matiiss Jul 23 '22 at 09:15
  • Are you also using the updated rects to blit the new position of the tiles? You need to reblit the surfaces when updating the rect's position, they won't be moved automatically. – The_spider Jul 23 '22 at 09:44
  • Never try to run animations in separate loops within the application loop. These loops will hold up the application loop and your game will not respond while these loops are running. Use the application loop. See [How to run multiple while loops at a time in Pygame](https://stackoverflow.com/questions/65263318/how-to-run-multiple-while-loops-at-a-time-in-pygame/65263396#65263396) – Rabbid76 Jul 23 '22 at 10:12
  • I don't see in how all the duplicate posts provided give an answer to this question. They all go about time management and running multiple while loops at the same time. However, this post is going about objects that need to be moved, but aren't moved. It does contain nested loops, but only to implement the game's logic and not to run animations. None of these loops contains a method that adds any delay, thus they don't hold up the apllication enough to prevent any movement, nor they are designed to run at the same time. – The_spider Jul 23 '22 at 10:29
  • @The_spider `for i in range(distance):` is a loop in the application loop. This loop is intended to animate the object. This is the real problem here and hence this question is double. In any case, this question should be closed for several reasons. – Rabbid76 Jul 23 '22 at 11:07
  • @Matiiss it’s at the top – shoowis Jul 23 '22 at 22:20
  • thank you everyone for helping me. I still haven’t found a solution but i think i am getting closer. – shoowis Jul 23 '22 at 22:22

0 Answers0