1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How would I change a specific position in a 2D grid in python

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 00:22.

  1. Stack

    Stack Membro Participativo

    Ok I am writing a tetris game in python an I'm trying to make the piece land and go to the board. When I run the game and let the piece fall to the bottom, it creates lines that span the entire board up to the 2nd layer below the top of the piece, I have no idea what's happening, I was wondering if anyone else could help with this?

    I've tried a lot of different approaches but most of them resulted in errors. One of the approaches that has not resulted in an error is

    # pieces.py
    O = [
    [0, 0, 0, 0],
    [0, 0, 1, 1],
    [0, 0, 1, 1],
    [0, 0, 0, 0],
    ]
    I = [
    [0, 0, 0, 0],
    [2, 2, 2, 2],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    ]
    S = [
    [0, 0, 3, 0],
    [0, 0, 3, 3],
    [0, 0, 0, 3],
    [0, 0, 0, 0],
    ]
    Z = [
    [0, 0, 0, 4],
    [0, 0, 4, 4],
    [0, 0, 4, 0],
    [0, 0, 0, 0],
    ]
    L = [
    [0, 0, 0, 0],
    [0, 5, 5, 5],
    [0, 5, 0, 0],
    [0, 0, 0, 0],
    ]
    J = [
    [0, 0, 0, 0],
    [0, 6, 0, 0],
    [0, 6, 6, 6],
    [0, 0, 0, 0],
    ]
    T = [
    [0, 0, 0, 7],
    [0, 0, 7, 7],
    [0, 0, 0, 7],
    [0, 0, 0, 0],
    ]
    piecesList = [O, I, S, Z, L, J, T]


    # main.py
    def placePiece():
    global holdingPieceSprite, holdingPiece, holdingPieceOffset, holdingPieceFallTime, holdingPieceFallDelay
    for x in range(4):
    for y in range(4):
    if not holdingPieceSprite[x][y] == 0:
    board[math.floor(x + holdingPieceOffset[0])][math.floor(y + holdingPieceOffset[1])] = holdingPieceSprite[x][y]
    print(board)
    resetHeldPiece()


    Nothing has worked yet, this is the closest I've gotten. If any more context is needed, I can provide that here's all the scripts:

    import pygame
    import random
    import pieces as p
    import math

    def hex_to_rgb(hex):
    hex = hex[1:]
    return tuple(int(hex[i:i+2], 16) for i in (0, 2, 4))

    tile_size = 16
    grid_size = [10, 20]
    screen = pygame.display.set_mode((tile_size * (grid_size[0] + 12), tile_size * (grid_size[1] + 6)))
    pygame.display.set_caption('PYtris')
    running = True
    clock = pygame.time.Clock()
    holdingPiece = False
    holdingPieceSprite = [[0]*4]*4
    holdingPieceOffset = [(grid_size[0]/2)-2, 0]
    holdingPieceFallTime = 10
    holdingPieceFallDelay = 10
    def resetHeldPiece():
    holdingPiece = False
    holdingPieceSprite = [[0]*4]*4
    holdingPieceOffset = [math.floor((grid_size[0]/2)-2), 0]
    holdingPieceFallTime = 10
    holdingPieceFallDelay = 10
    img = pygame.image.load("images/tetris_piece_1.png").convert()
    red = hex_to_rgb("#FF0000")
    orange = hex_to_rgb("#FF7700")
    yellow = hex_to_rgb("#FFFF00")
    green = hex_to_rgb("#0000FF")
    blue = hex_to_rgb("#00FF00")
    purple = hex_to_rgb("#FF7700")
    white = hex_to_rgb("#FFFFFF")
    black = hex_to_rgb("#000000")
    hexes = ["#FF0000", "#FF7700", "#FFFF00", "#0000FF", "#00FF00", "#FF0044", "#00FFFF", "#FF00F0", "#000000", "#FFFFFF"]
    background_hexes = ["#AA0000", "#AA2200", "#AAAA00", "#0000AA", "#00AA00", "#AA0022", "#00AAAA", "#AA00A0", "#AAAAAA"]
    tiles = [8]
    for i in range(7):
    tiles.append(i + 1)
    tiles.append(9)
    print(tiles)
    for i in tiles:
    print(hexes)
    board = [[0]*(grid_size[1]+1)]*(grid_size[0]+1)
    background_colour = hex_to_rgb("#AFABCD")
    screen.fill(background_colour)

    def resetBag():
    bag = p.piecesList
    random.shuffle(bag)
    return bag
    def draw():
    for x in range(grid_size[0]):
    for y in range(grid_size[1]):
    screen.blit(tintTile(hex_to_rgb(hexes[tiles[board[x][y]]])), ((x+6)*tile_size, (y+3)*tile_size))
    for x in range(4):
    for y in range(4):
    if not holdingPieceSprite[x][y] == 0:
    screen.blit(tintTile(hex_to_rgb(hexes[tiles[holdingPieceSprite[x][y]]])), (((x+6)+holdingPieceOffset[0])*tile_size, ((y+3)+holdingPieceOffset[1])*tile_size))
    def placePiece():
    for x in range(4):
    for y in range(4):
    if not holdingPieceSprite[x][y] == 0:
    board[math.floor(x + holdingPieceOffset[0])][math.floor(y + holdingPieceOffset[1])] = holdingPieceSprite[x][y]
    print(board)
    resetHeldPiece()

    bag = resetBag()

    def gameTick():
    if not holdingPiece:
    holdingPiece = True
    holdingPieceSprite = bag[0]
    bag.pop(0)

    draw()
    holdingPieceFallTime -= 1
    if holdingPieceFallTime <= 0:
    holdingPieceFallTime = holdingPieceFallDelay
    holdingPieceOffset[1] += 1
    if holdingPieceOffset[1] >= 17:
    placePiece()
    pygame.display.flip()


    def tintTile(color: tuple):
    img_copy = img.copy()
    img_copy.fill(color, special_flags=pygame.BLEND_RGB_MULT)
    return img_copy

    def initGameBoard():
    for x in range(grid_size[0] - 1):
    for y in range(grid_size[1] - 1):
    screen.blit(tintTile(tiles[0]), ((x+6)*tile_size, (y+3)*tile_size))

    def initGame():
    for x in range((grid_size[0] + 12)):
    for y in range((grid_size[1] + 6)):
    screen.blit(tintTile(hex_to_rgb(random.choice(background_hexes))), (x*tile_size, y*tile_size))
    initGameBoard()

    initGame()
    # game loop
    while running:

    # for loop through the event queue
    for event in pygame.event.get():

    # Check for QUIT event
    if event.type == pygame.QUIT:
    running = False
    clock.tick(30)
    gameTick()

    Continue reading...

Compartilhe esta Página