"Die Matrix" programmieren |
MAC_BOSS
Grünschnabel
Dabei seit: 16.03.2010
Beiträge: 1
|
|
|
16.03.2010 19:04 |
|
|
texelmax
Grünschnabel
Dabei seit: 12.11.2010
Beiträge: 1
|
|
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;
}
|
|
12.11.2010 10:59 |
|
|
KjubE
Grünschnabel
Dabei seit: 11.01.2011
Beiträge: 3
|
|
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;
#
}
#
}
#
}
|
|
11.01.2011 09:57 |
|
|
Blackarro
Eroberer
Dabei seit: 02.01.2011
Beiträge: 62
Herkunft: Deutschland
|
|
|
11.01.2011 19:42 |
|
|
Ibn Batuta
Mitglied
Dabei seit: 02.01.2011
Beiträge: 26
|
|
Was soll dein Code bringen, Blackarro? Das hat ja nichts mit dem Thema zu tun.
Ibn Batuta
|
|
11.01.2011 22:12 |
|
|
|