简体   繁体   中英

How to create 2D image in Console without using any existing solutions in C#?

I'm beginner in programming

So, I want to reinvent the wheel. I'm planing to create my own 2D game like Mario etc using only Console in C#. My question is, how to create/draw pixels in Console to make an image? Or the only way is to use Unicode characters?

I know that Mario(or any other games like it) was created on assembly language (of cource without any libs like OpenGL =) ). Is it possible to create something like that on C#? I don't want to use WinForms apps, Unity, OpenGL etc etc etc

You can print only characters to the console.

Box-drawing characters allow you to create frames and other primitive geometric forms. Examples:

│ ┃ ┄ ┅ ┆ ┇ ┈ ┉ ┊ ┋ ┌ ┍ ┎ ┏ ┐ ┑ ┒ ┓ └ ┕ ┖ ┗ ┘ ┙ ┚ ┛ ├ ┝ ┞ ┟ ┠ ┡ ┢ ┣ ┤ ┥ ┦ ┧ ┨ ┩ ┪ ┫ ┬ ┭ ┮ ┯ ┰ ┱ ┲ ┳ ┴ ┵ ┶ ┷ ┸ ┹ ┺ ┻ ┼ ┽ ┾ ┿ ╀ ╁ ╂ ╃ ╄ ╅ ╆ ╇ ╈ ╉ ╊ ╋ ╌ ╍ ╎ ╏ ═ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟ ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬ ╭ ╮ ╯

With ASCII Art you print charaters with different densities of to create shadings. (Art by Normand Veilleux, detail)

                              _______
                       _,,ad8888888888bba,_
                    ,ad88888I888888888888888ba,
                  ,88888888I88888888888888888888a,
                ,d888888888I8888888888888888888888b,
               d88888PP"""" ""YY88888888888888888888b,
             ,d88"'__,,--------,,,,.;ZZZY8888888888888,
            ,8IIl'"                ;;l"ZZZIII8888888888,
           ,I88l;'                  ;lZZZZZ888III8888888,
         ,II88Zl;.                  ;llZZZZZ888888I888888,
        ,II888Zl;.                .;;;;;lllZZZ888888I8888b
       ,II8888Z;;                 `;;;;;''llZZ8888888I8888,
       II88888Z;'                        .;lZZZ8888888I888b
       II88888Z; _,aaa,      .,aaaaa,__.l;llZZZ88888888I888
       II88888IZZZZZZZZZ,  .ZZZZZZZZZZZZZZ;llZZ88888888I888,
       II88888IZZ<'(@@>Z|  |ZZZ<'(@@>ZZZZ;;llZZ888888888I88I
      ,II88888;   `""" ;|  |ZZ; `"""     ;;llZ8888888888I888
      II888888l            `;;          .;llZZ8888888888I888,
     ,II888888Z;           ;;;        .;;llZZZ8888888888I888I
     III888888Zl;    ..,   `;;       ,;;lllZZZ88888888888I888
     II88888888Z;;...;(_    _)      ,;;;llZZZZ88888888888I888,
     II88888888Zl;;;;;' `--'Z;.   .,;;;;llZZZZ88888888888I888b
     ]I888888888Z;;;;'   ";llllll;..;;;lllZZZZ88888888888I8888,
     II888888888Zl.;;"Y88bd888P";;,..;lllZZZZZ88888888888I8888I
     II8888888888Zl;.; `"PPP";;;,..;lllZZZZZZZ88888888888I88888
     II888888888888Zl;;. `;;;l;;;;lllZZZZZZZZW88888888888I88888
     `II8888888888888Zl;.    ,;;lllZZZZZZZZWMZ88888888888I88888
      II8888888888888888ZbaalllZZZZZZZZZWWMZZZ8888888888I888888,
      `II88888888888888888b"WWZZZZZWWWMMZZZZZZI888888888I888888b
       `II88888888888888888;ZZMMMMMMZZZZZZZZllI888888888I8888888
        `II8888888888888888 `;lZZZZZZZZZZZlllll888888888I8888888,
         II8888888888888888, `;lllZZZZllllll;;.Y88888888I8888888b,
        ,II8888888888888888b   .;;lllllll;;;.;..88888888I88888888b,
        II888888888888888PZI;.  .`;;;.;;;..; ...88888888I8888888888,

or outlines (example by ejm97)

 w  c(..)o   (
  \__(-)    __)
      /\   (
     /(_)___)
     w /|
      | \
     m  m

You can set the cursor position with

Console.SetCursorPosition(left, top);

and also set foreground/background colors, console size etc.

You can only print characters. Here i suggest an architecture for coding your game:

  1. Design the whole game map as a 2D integer array, rows and columns. Each value define the block, it can be empty or can be an obstacle or a coin. For example, "0" is nothing, means in this area there is no block. "1" is a block, and "2" is a coin. Here is a sample of the array:
[0][0,0,0,0,0]
[1][0,2,0,0,0]
[2][1,1,1,1,1]

This array defines the game map. In rendered output (witch i will explain) we have this:


    O
[][][][][]

The brackets are blocks that the game character can walk on them. And the "O" is the coin that character can collect it. And the first row we have nothing, as we defines it in our array. After we built our game map, we need to update frames of game.

  1. We need to update frames when player pushed a button. For that, we need a layer of program to parse array to characters, it can be a method witch take the array as parameter and return a string of output value.

  2. For the next layer, after player pushed button and the frame updated, we have to update the current position of the game character, for example, character is in "row 1" and "column 0" before player push a button. The player push the Right button, so we have to move the character to "row 1" and "column 1".

  3. As i said, when character moved by player, we need a layer to recognize the character and is he crashed to a obstacle or coin? In above example, our character reached to the coin position on the map, so in this layer of process, we have to increase player coins.

This is a very very simple example of an architecture of a console game. Of course you need to work on details and codes and create something funny:). I hope it helped;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM