• + 0 comments

    Learn about a code that sets up a basic version of a game by initializing the board with 'O's, randomly placing bombs ('B's), and then alternating between placing bombs and detonating them every two seconds until the timer runs out. Read on this code does not include player movements or enemy AI, it serves as a useful starting point for building a simple version of the game. So, if you're interested in creating a basic game that involves bomb placement and detonation, keep reading to explore this code and learn more.

    import time
    
    def print_board(board):
        for row in board:
            print("".join(row))
    
    def initialize_board(n, m):
        board = []
        for i in range(n):
            row = []
            for j in range(m):
                row.append('O')
            board.append(row)
        return board
    
    def place_bombs(board):
        for i in range(len(board)):
            for j in range(len(board[0])):
                if board[i][j] == 'O':
                    board[i][j] = 'B'
    
    def detonate_bombs(board):
        for i in range(len(board)):
            for j in range(len(board[0])):
                if board[i][j] == 'B':
                    board[i][j] = '.'
                    if i > 0:
                        board[i-1][j] = '.'
                    if i < len(board)-1:
                        board[i+1][j] = '.'
                    if j > 0:
                        board[i][j-1] = '.'
                    if j < len(board[0])-1:
                        board[i][j+1] = '.'
    
    n, m, t = map(int, input().split())
    board = initialize_board(n, m)
    place_bombs(board)
    print_board(board)
    time.sleep(t)
    
    for i in range(2, t+1):
        if i % 2 == 0:
            place_bombs(board)
        else:
            detonate_bombs(board)
        print_board(board)
        time.sleep(1)