SNAKE - Wie gehts weiter?

Friedfischer

Grünschnabel
Hallo, ich möchte mich kurz halten:

Habe folgendes Programm geschrieben, und möchte folgende Punkte realisieren:

1. Die Schlange soll sich immer, wenn sie etwas gefressen hat, verlängern.
2. Wie macht man Spezialfutter rein?
3. Wie "vereine" ich diesen Highscorelistencode (oder gibts bessere), mit dem Programm?
Der Name soll selbstverständlich angegeben werden können.

Wenn sonst noch jemanden etwas auffällt, bitte sagt es!

Sollte mir jemand helfen, so möge er es bitte so schreiben, dass auch ich es verstehe.
Ich danke euch vielmal im Voraus!

Hier zuerst der Code für de Highscoreliste:

C++:
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
#include <fstream>

int main()
{
	std::vector< std::pair<unsigned short, std::string> > vec_top10;
	vec_top10.push_back(std::make_pair(125, "Karl Hans Peter"));
	vec_top10.push_back(std::make_pair(652, "Detlef Soost"));
	vec_top10.push_back(std::make_pair(30, "Hans Willi"));
	vec_top10.push_back(std::make_pair(812, "Olaf Wurst"));
	vec_top10.push_back(std::make_pair(123, "Werner Schmitz"));
	vec_top10.push_back(std::make_pair(412, "Hänschen Depp"));
	vec_top10.push_back(std::make_pair(412, "Hänschen Depp"));
	vec_top10.push_back(std::make_pair(412, "Hänschen Depp"));
	vec_top10.push_back(std::make_pair(412, "Hänschen Depp"));
	vec_top10.push_back(std::make_pair(412, "Hänschen Depp"));
	std::sort(vec_top10.begin(), vec_top10.end());

	std::ofstream file_out_stream("top10.txt", std::ios::out);

	if (!file_out_stream)
        return 0;

	for (size_t item = 0; item < 10; ++item)
	{
		std::cout << vec_top10[item].first << " | " << vec_top10[item].second << std::endl;
		file_out_stream << vec_top10[item].second << " | " <<vec_top10[item].first << std::endl;
	}
   
    file_out_stream.close();
}

HIER MEIN PROGRAMM:

C++:
#include <iostream>
#include <conio>
#include <cstdio>
#include <iomanip>
#include <cdos>
#include <cstdlib>
#include <cstring>
#include <vcl>
#include <ctime>
#pragma hdrstop
#pragma argsused

//------------------------- Farbendeklaration --------------------------------

int dunkelblau   =  1,   dunkelbraun  =  6,   hellblau     = 11,
    dunkelgruen  =  2,   grau         =  7,   rot          = 12,
    dunkeltuerkis=  3,   dunkelgrau   =  8,   lilla        = 13,
    dunkelrot    =  4,   blau         =  9,   gelb         = 14,
    violett      =  5,   gruen        = 10,   weiss        = 15;

//------------------------- Variblendeklaration --------------------------------
HANDLE console_output = GetStdHandle(STD_OUTPUT_HANDLE);
time_t sekunden = time(NULL);
tm *uhr = localtime(&sekunden);

void snake_show(void);
void futtererklaerung (void);
void ungueltige_eingabe (void);
void viel_spass (void);
void futter (void);
void farbe(int x);
void DrawFrame(int x1,int y1,int x2,int y2,int line);

struct snake_pos
{
    int x_pos;
    int y_pos;
    char sign;
};

int dx=1, dy=0, x=30, y=10, zahl1, zahl2, quit=1, Punkte=0, Punkte1, speed;

void snake_move(int xdir, int ydir, snake_pos snake2[]);
snake_pos snake1[]={{x,y,'\x20'},
                    {x+1,y,'\xB1'},
                    {x+2,y,'\xB1'},
                    {x+3,y,'\xB1'},
                    {x+4,y,'\xB1'},
                    {x+5,y,'\xDB'}};

//-------------------------- Unterprogramme ------------------------------------
void DrawFrame(int x1,int y1,int x2,int y2, int line)
{
   int temp;
   if (x1>x2)
   {
      temp=x1;
      x1=x2;
      x2=temp;
   }
   if (y1>y2)
   {
      temp=y1;
      y1=y2;
      y2=temp;
   }

   if (line==2)
   {
      gotoxy(x1,y1);
      cout<<'\xC9'<<setw(x2-x1)<<setfill('\xCD')<<'\xBB';
      gotoxy(x1,y2);
      cout<<'\xC8'<<setw(x2-x1)<<setfill('\xCD')<<'\xBC';
      for (int i=y1+1;i<=y2-1;i++)
      {
         gotoxy(x1,i);
         cout<<'\xBA';
         gotoxy(x2,i);
         cout<<'\xBA';
      }
   }
}


void snake_show(void)
{
    for(int i=0; i<6;i++)
    {
        gotoxy(snake1[i].x_pos,snake1[i].y_pos);
        cout<<snake1[i].sign;
    }

}

void snake_move(int xdir, int ydir, snake_pos snake2[])
{
    snake2[5].x_pos+=xdir;
    snake2[5].y_pos+=ydir;
    for(int i=0; i<5; i++)
    {
        snake2[i].x_pos=snake2[i+1].x_pos;
        snake2[i].y_pos=snake2[i+1].y_pos;
    }
}

void futtererklaerung (void)
{

    gotoxy(9,10);
    farbe(rot);
    cout <<"Geben Sie ihren Schwierigkeitsgrad ein (1=Anfaenger ... 5=Profi): ";
    farbe(weiss);
                                                                Sleep(800);
    gotoxy(26,14); cout <<"Stufe 1 ...  2 Punkte je Futter";    Sleep(500);
    gotoxy(26,15); cout <<"Stufe 2 ...  4 Punkte je Futter";    Sleep(500);
    gotoxy(26,16); cout <<"Stufe 3 ...  6 Punkte je Futter";    Sleep(500);
    gotoxy(26,17); cout <<"Stufe 4 ...  8 Punkte je Futter";    Sleep(500);
    gotoxy(26,18); cout <<"Stufe 5 ... 10 Punkte je Futter";
    gotoxy(9,10);
    farbe(rot);
    cout <<"Geben Sie ihren Schwierigkeitsgrad ein (1=Anfaenger ... 5=Profi): ";
}

void ungueltige_eingabe (void)
{
    clrscr();
    DrawFrame(16,4,68,8,2);
    farbe(gruen);
    gotoxy(18,6);
    cout <<"Ungueltige Eingabe ! Das Programm wird beendet...";
    sleep(3);
}

void viel_spass (void)
{
        gotoxy(33,6);           cout <<"  Viel Spass!  ";
        gotoxy(33,8);           cout <<"Start in 3 sec.";
        gotoxy(33,8); sleep(1); cout <<"Start in 2 sec.";
        gotoxy(33,8); sleep(1); cout <<"Start in 1 sec.";
                      sleep(1);
        gotoxy(33,8);           cout <<"               ";
        gotoxy(33,6);           cout <<"               ";
}

void futter (void)
{
    randomize();
    zahl1 = random(76)+2;
    zahl2 = random(20)+2;
    gotoxy(zahl1,zahl2);
    farbe(rot);
    cout<<'\xe';
}

void farbe(int x)
{
    SetConsoleTextAttribute(console_output, x);
}

//--------------------------------Hauptprogramm---------------------------------

int main(int argc, char* argv[])
{
    clrscr();

    do
    {
    clrscr();
    _setcursortype(_NOCURSOR);
    snake1[5].x_pos=34;     snake1[5].y_pos=10;
    farbe(gruen);
    DrawFrame(21,5,61,7,2);
    gotoxy(22,6);
    cout <<"HERZLICH WILLKOMMEN im Spiel von Snake!";
    futtererklaerung();
    cin>>speed;

    if(speed <=0 || speed>=6)
    {
    ungueltige_eingabe();
    return 0;
    }

    if(speed==1){speed=200;}
    if(speed==2){speed=150;}
    if(speed==3){speed=100;}
    if(speed==4){speed=70;}
    if(speed==5){speed=50;}

    clrscr();

    do
    {
        farbe(gelb);
        DrawFrame(1,1,80,22,2);
        farbe(rot);

        viel_spass();

        farbe(weiss);
        gotoxy(2,23);  cout <<"Anfang:  "<<uhr->tm_hour<<":"<<uhr->tm_min<<":"<<uhr->tm_sec;
        gotoxy(20,23); cout <<"Uhr";

        futter();

        for(int k=0; k<1; k++)
        {
            farbe(gelb);
            DrawFrame(1,1,80,22,2);
            k--;
            Sleep(speed);

            if(kbhit())
            {
                switch(getch())
                {
                    case 77: if(dx!=-1){dx=1;  dy=0;}  break;
                    case 75: if(dx!=1) {dx=-1; dy=0;}  break;
                    case 72: if(dy!=1) {dx=0;  dy=-1;} break;
                    case 80: if(dy!=-1){dx=0;  dy=1;}  break;
                }
            }
            time_t sekunden = time(NULL);
            tm *uhr = localtime(&sekunden);
            farbe(weiss);
            gotoxy(2,25);
            cout <<"Datum:   "<<uhr->tm_wday+15<<"."<<uhr->tm_mon+1<<"."<<uhr->tm_year+1900;
            farbe(gruen);
            gotoxy(1,25);
            snake_move(dx,dy,snake1);     snake_show();

            

            if(snake1[5].x_pos>=80 | snake1[5].x_pos<=1 | snake1[5].y_pos>=22 | snake1[5].y_pos<=1)
            {
                gotoxy(30,9);
                farbe(gruen);
                cout<<"G A M E   O V E R  !";
                quit=2;
                farbe(weiss);
                break;
            }

            farbe(hellblau);
            gotoxy(60,24); cout <<"SCORE:  "<<Punkte;

            if(snake1[5].x_pos==zahl1 && snake1[5].y_pos==zahl2)
            {
                futter();
                cout<<'\007';

                if(speed==200){Punkte1=2;}
                if(speed==150){Punkte1=4;}
                if(speed==100){Punkte1=6;}
                if(speed==70){Punkte1=8;}
                if(speed==50){Punkte1=10;}

                Punkte=Punkte+Punkte1;
            }
        }
    }
    while(quit==1);
    farbe(hellblau); gotoxy(33,11); cout<<"S C O R E : "<<Punkte;
                     gotoxy(60,24); cout <<"                  ";

    gotoxy(zahl1,zahl2);
    cout <<" ";

    farbe(weiss);
    gotoxy(4,13); cout<<"Druecken Sie 2 fuer ein neues Spiel und 1 fuer das Beenden des Programmes !";
    gotoxy(2,24); cout <<"Ende:    "<<uhr->tm_hour<<":"<<uhr->tm_min<<":"<<uhr->tm_sec;
    gotoxy(20,24);cout <<"Uhr";
    gotoxy(30,15);cout<<"Ihre Eingabe bitte: ";

    cin>>quit;
    dx=1;dy=0;
    clrscr();
    gotoxy(2,23);  cout <<"Anfang:  "<<uhr->tm_hour<<":"<<uhr->tm_min<<":"<<uhr->tm_sec;
    gotoxy(20,23); cout <<"Uhr";
    Punkte=0;
    }
    while(quit!=1);
    clrscr();
    gotoxy(20,10); farbe(rot); cout <<"D a n k e   f u e r s   S p i e l e n   !";
    gotoxy(28,12); farbe(rot); cout <<"(c) 2007, by Grill Mario";
    sleep(4);
}
//------------------------------------------------------------------------------
 
Erst ein mal folgendes: Geduld mein junger Padawan. Du kannst nich innerhalb von 4 Stundne eine Antwort erwarten. 1 Tag mal mindestens Wartezeit. Dann noch mal nachfragen.

Zu deinem Problem: Ich bin leider nur ein Fanatischer Basicer ( ^^ ) aber ich kann dir mal in etwa erklären wie ich ein Snake spiel konzipieren würde: Die Schlange (also jedes schlangenglied) ist ein Wet aus einem Array. In dem Array sind für jedes Glied X und Y Position gespeichert. in jedem Durchlauf werden die Werte vom ende des Arrays eine Position nach hinten verschoben also z.B.: Teil[3]=Teil[2], Teil[2]=Teil[1].nur dem Kopf wird immer ein neuer Wert zugewiesen für die neue Position.

Beim Fressen eines gegenstandes wird dem Array ein Element angehangen.

Das ist so die Theoretische umsetzung wie sie mir vorschweben würde. Du musst es jetzt nur noch in C/C++ übersetzen.

Ich hoffe das hilft :/

Mfg

Christopher
 
Zurück