"Die Matrix" programmieren

Neue Frage »

Auf diesen Beitrag antworten »
MAC_BOSS "Die Matrix" programmieren

Meine Frage:
Hi

Ich versuche schon seit einiger Zeit eine "Matrix"(Film!!, nichts mathematisches) in C++ zu programmieren. hab mir schon ein paar Videos dazu angesehen, aber die haben leider den Quellcode nicht mehr.

So sollte es in etwa dann aussehen:
www.youtube.com/watch?v=rkC8b9tJGh8

Mein Problem ist vor allem, das die Zeichen immer von links nach rechts und nicht von oben nach unten gezeichnet werden. (Es sieht im Video zumindest so aus.). Wie kann ich das verwirklichen



Vielen Dank im Voraus



Meine Ideen:
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <windows.h>

using namespace std;

int main(){
system("color 0A");
char* signs[5]={"10 10 1 1 1 1" ,"10 10 0 01 01", " 1 00 1 110101 1 1 11 0", "1 000 10 101000 001 111", "11 0 11 0"};
int i = 0;
while(1){
i = rand() % 5;
Sleep(50);
cout << signs[i] << '\t';
}
getch();
}
 
Auf diesen Beitrag antworten »
texelmax RE: "Die Matrix" programmieren

Ich hätte da ne Idee:

#include <iostream>
#include <vector>
#include <windows.h>
#include <cstdio>
#include <algorithm>

using namespace std;

const int max_x = 80; //console width
const int max_y = 24; //console height
const int col1 = 10; //color code for higlighted char
const int col2 = 2; //color code for normal char
const int col3 = 0; //color code for background
const int c_min = 32; //char minimum ascii code
const int c_max = 126; //char maximum ascii code

const inline char rc(int m1, int m2) //random char
{
return (rand()%(m2-m1))+m1;
}

void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void setcol(int fore, int back)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), fore + back*16);
}

void clrscr()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };

hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return;

/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;

/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;

/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;

/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
}

void At(char chr, int x, int y) //print char at x-y pos in console
{
if (y>=0 && y<=max_y)
{
gotoxy(x,y);
printf("%c",chr);
}
}

class FallChar //falling char
{
public:
int X, Y;
char code;
int speed;
FallChar();
};

FallChar::FallChar()
{
X=rand()%max_x;
Y=0;
code=rc(c_min,c_max);
speed=rand()%3+1;
}

int main()
{
vector<FallChar> chars;
FallChar* cchar;

while (1)
{
chars.push_back(FallChar());
chars.push_back(FallChar());
chars.push_back(FallChar());
random_shuffle(chars.begin(), chars.end()); //shuffle to make flashing more equally distributed
for (int i=0; i<chars.size(); i++)
{
cchar = &chars[i];
if (cchar->Y!=max_y+4)
{
setcol(col1,col3);
At(cchar->code, cchar->X, cchar->Y);
At(rc(c_min,c_max), cchar->X, cchar->Y-1);
setcol(col2,col3);
At(rc(c_min,c_max), cchar->X, cchar->Y-2);
At(rc(c_min,c_max), cchar->X, cchar->Y-3);
At(rc(c_min,c_max), cchar->X, cchar->Y-4);
cchar->Y+=cchar->speed;
}
else
{
chars.erase(chars.begin()+i);
i--;
}
}
Sleep(30);
clrscr();
}
return 0;
}
Auf diesen Beitrag antworten »
KjubE

Ich kenne mich mit c++ leider nicht aus. Aber in C# würde der Quellcode so aussehen:

#
using System;
#

#
namespace m7tr1x
#
{
#
class Program
#
{
#
static void Main(string[ ] args)
#
{
#
Console.Title = "tH3 M7tr1x 3ff3<t";
#
Console.ForegroundColor = ConsoleColor.DarkGreen;
#
Console.WindowLeft = Console.WindowTop = 0;
#
Console.WindowHeight = Console.BufferHeight = Console.LargestWindowHeight;
#
Console.WindowWidth = Console.BufferWidth = Console.LargestWindowWidth;
#
#if readkey
#
Console.WriteLine("H1T 7NY K3Y T0 C0NT1NU3 =/");
#
Console.ReadKey();
#
#endif
#
Console.CursorVisible = false;
#
int width, height;
#
int[ ] y;
#
int[ ] l;
#
Initialize(out width, out height, out y, out l);
#
int ms;
#
while ( true )
#
{
#
DateTime t1 = DateTime.Now;
#
MatrixStep(width, height, y, l);
#
ms = 10 - (int)( (TimeSpan)( DateTime.Now - t1 ) ).TotalMilliseconds;
#
if ( ms > 0 )
#
System.Threading.Thread.Sleep(ms);
#
if ( Console.KeyAvailable )
#
if ( Console.ReadKey().Key == ConsoleKey.F5 )
#
Initialize(out width, out height, out y, out l);
#
}
#
}
#

#
static bool thistime = false;
#

#
private static void MatrixStep(int width, int height, int[ ] y, int[ ] l)
#
{
#
int x;
#
thistime = !thistime;
#
for ( x = 0 ; x < width ; ++x )
#
{
#
if ( x % 11 == 10 )
#
{
#
if ( !thistime )
#
continue;
#
Console.ForegroundColor = ConsoleColor.White;
#
}
#
else
#
{
#
Console.ForegroundColor = ConsoleColor.DarkGreen;
#
Console.SetCursorPosition(x, inBoxY(y[x] - 2 - ( l[x] / 40 * 2 ), height));
#
Console.Write(R);
#
Console.ForegroundColor = ConsoleColor.Green;
#
}
#
Console.SetCursorPosition(x, y[x]);
#
Console.Write(R);
#
y[x] = inBoxY(y[x] + 1, height);
#
Console.SetCursorPosition(x, inBoxY(y[x] - l[x], height));
#
Console.Write(' ');
#
}
#
}
#

#
private static void Initialize(out int width, out int height, out int[ ] y, out int[ ] l)
#
{
#
int h1;
#
int h2 = ( h1 = ( height = Console.WindowHeight ) / 2 ) / 2;
#
width = Console.WindowWidth - 1;
#
y = new int[width];
#
l = new int[width];
#
int x;
#
Console.Clear();
#
for ( x = 0 ; x < width ; ++x )
#
{
#
y[x] = r.Next(height);
#
l[x] = r.Next(h2 * ( ( x % 11 != 10 ) ? 2 : 1 ), h1 * ( ( x % 11 != 10 ) ? 2 : 1 ));
#
}
#
}
#

#
static Random r = new Random();
#
static char R
#
{
#
get
#
{
#
int t = r.Next(10);
#
if ( t <= 2 )
#
return (char)( '0' + r.Next(10) );
#
else if ( t <= 4 )
#
return (char)( 'a' + r.Next(27) );
#
else if ( t <= 6 )
#
return (char)( 'A' + r.Next(27) );
#
else
#
return (char)( r.Next(32, 255) );
#
}
#
}
#

#
public static int inBoxY(int n, int height)
#
{
#
n = n % height;
#
if ( n < 0 )
#
return n + height;
#
else
#
return n;
#
}
#
}
#
}
Auf diesen Beitrag antworten »
Blackarro RE: "Die Matrix" programmieren

Hier stand mal was...

//Edit 3FBN: Schön von http://www.c-plusplus.de/forum/p1759315#1759315 kopiert -.-
 
Auf diesen Beitrag antworten »
Ibn Batuta

Was soll dein Code bringen, Blackarro? Das hat ja nichts mit dem Thema zu tun.


Ibn Batuta
 
Neue Frage »
Antworten »


Verwandte Themen

Die Beliebtesten »
Die Größten »
Die Neuesten »