Okay, det var ikke helt det jeg ville! Men prøv lige at kigge på denne kode nu! Det virker næsten, der er kun en ting tilbage som ikke gider helt! Kopier koden til din compiler og se hvad der sker:
Kig især på "spil_snake" og "hi_score" funktionerne!
#include <windows.h>
#include <fstream>
#include <iostream>
#include <conio.h> //indeholder kbhit
using namespace std;
void gotoxy (int x, int y)
{
COORD cur = {x,y}; // cur er en koordinat
SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), cur); // sætter cursor efter valg
}
int fullscreen ()
{
#define CONSOLE_FULLSCREEN_MODE 1
typedef BOOL (*F)(HANDLE hConsoleOutput,DWORD dwFlags,COORD *lpNewScreenBufferDimensions);
HINSTANCE dll;
F SetConsoleDisplayMode;
HANDLE h;
COORD size = { 80, 25 };
h = GetStdHandle(STD_OUTPUT_HANDLE);
dll = LoadLibrary("kernel32.dll");
SetConsoleDisplayMode = (F)GetProcAddress(dll,"SetConsoleDisplayMode");
SetConsoleDisplayMode(h,CONSOLE_FULLSCREEN_MODE,&size);
return 0;
}
void farvetekst(string Tekst, int X, int Y, WORD farve)
{
HANDLE UdHandle; //Lav en handle som giver os tilladelse til at skrive
COORD position = {X, Y}; //Lav et koordinatsetup
UdHandle = GetStdHandle(STD_OUTPUT_HANDLE); //Skaf en UD-handle til skærmen
SetConsoleTextAttribute(UdHandle, farve); //Indstil konsol
SetConsoleCursorPosition(UdHandle, position); //Indstil vores skriveposition
cout << Tekst;
SetConsoleTextAttribute(UdHandle, farve);
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
}
void tegn_ramme()
{
int tegn_x;
int tegn_y;
for(tegn_x = 5; tegn_x < 65; tegn_x++)
{farvetekst(" " , tegn_x, 5, BACKGROUND_GREEN);}
for(tegn_x = 6; tegn_x < 65; tegn_x++)
{farvetekst(" ", tegn_x, 38, BACKGROUND_GREEN);}
for(tegn_y = 6; tegn_y < 39; tegn_y++)
{farvetekst(" ", 5, tegn_y, BACKGROUND_GREEN);}
for(tegn_y = 6; tegn_y < 39; tegn_y++)
{farvetekst(" ", 64, tegn_y, BACKGROUND_GREEN);}
}
string navn;
int point = 0;
void spil_snake()
{
int flyt;
int slange_x = 10;
int slange_y = 35;
char svar;
char slange = 177;
char slange_hoved = 229;
int kage_x;
int kage_y;
if(kbhit())
{
flyt = getche();
}
//-finder ud af hvor kagen skal være-
kage_x = 6 + rand()%58; // inden for rammen
kage_y = 6 + rand()%32; // inden for rammen
while (flyt != 27)
{
Sleep(60);
system("cls");
tegn_ramme();
if(kbhit())
{
flyt = getche();
}
//-pil højre-
if(flyt == 77)
{
slange_x++;
gotoxy(10,35); farvetekst("", slange_x, slange_y, FOREGROUND_GREEN);
cout << slange << slange_hoved;
}
//-pil venstre-
if(flyt == 75)
{
slange_x--;
farvetekst("", slange_x, slange_y, FOREGROUND_GREEN);
cout << slange_hoved << slange;
}
//-pil op-
if(flyt == 72)
{
slange_y--;
farvetekst("", slange_x, slange_y, FOREGROUND_GREEN);
cout << slange_hoved << endl;
gotoxy(slange_x, slange_y+1); cout << slange;
gotoxy(slange_x, slange_y+1); cout << slange;
}
//-pil ned-
if(flyt == 80)
{
slange_y++;
farvetekst("", slange_x, slange_y, FOREGROUND_GREEN);
cout << slange;
gotoxy(slange_x, slange_y+1); cout << slange_hoved;
}
//-pause-
if(flyt == 112)
{
gotoxy(10,35); farvetekst("", slange_x, slange_y, FOREGROUND_GREEN);
cout << slange << slange << slange << slange_hoved;
}
//------------------------------point system------------------------------------
//-tegner kage-
gotoxy (kage_x, kage_y);
cout << "*";
if (kage_x == slange_x)
{
if (kage_y == slange_y)
{
kage_x = 6 + rand()%58; // inden for rammen
kage_y = 6 + rand()%32; // inden for rammen
point = point + 2;
Beep (400, 100);
}
}
farvetekst("Score: ", 30, 3, FOREGROUND_BLUE);
cout << point << endl;
//-------------------------hi-score------------------------------------------------
//-død ved grænserne x og y-
if(slange_x == 4 || slange_x == 64 || slange_y == 5 || slange_y == 38)
{
int min = 2;
if(point > min)
{
Beep(300, 50);
Beep(400, 50);
Beep(500, 50);
Beep(600, 50);
Beep(700, 50);
Beep(800, 50);
gotoxy(5,39);
cout << "Congrogulations, you are on the hi-score list now." << endl;
gotoxy(5,40);
cout << "Please enter your name: ";
cin >> navn;
point = min;
ofstream output("data.txt"); //opret ofstream objekt
output << navn //indsætter data
<< ' '
<< point;
gotoxy(5,41); cout << "Hi-score saved\\n";
}
fstream filestr;
filestr.open ("data.txt", fstream::in | fstream::out | fstream::app);
ifstream infile("data.txt");
infile >> navn >> point; //læs data fra fil
if(min > point)
{
Beep(300, 50);
Beep(400, 50);
Beep(500, 50);
Beep(600, 50);
Beep(700, 50);
Beep(800, 50);
gotoxy(5,39);
cout << "Congrogulations, you are on the hi-score list now." << endl;
gotoxy(5,40);
cout << "Please enter your name: ";
cin >> navn;
point = min;
ofstream output("data.txt"); //opret ofstream objekt
output << navn //indsætter data
<< ' '
<< point;
gotoxy(5,41); cout << "Hi-score saved\\n";
}
else
{
gotoxy(5,40); cout << "You lost!" << endl;
Beep(900, 100);
}
do
{
gotoxy(5,42); cout << "Press m to return to main ";
do
{
cin >> svar;
if (svar == 'm')
{
flyt = 27;
system("CLS");
}
} while (svar != 'm');
} while (svar != 'm');
}
//--------------------------------------------------------------------------------
} // while-løkken lukkes
}
void hi_score()
{
char ramme = 254;
int ramme_x;
int ramme_y;
for(ramme_x = 4; ramme_x < 60; ramme_x++)
{farvetekst("=", ramme_x, 4, FOREGROUND_GREEN);}
for(ramme_x = 4; ramme_x < 60; ramme_x++)
{farvetekst("=", ramme_x, 10, FOREGROUND_GREEN);}
for(ramme_y = 4; ramme_y < 11; ramme_y++)
{SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
gotoxy(3, ramme_y); cout << ramme << endl;
}
for(ramme_y = 4; ramme_y < 11; ramme_y++)
{gotoxy(60, ramme_y); cout << ramme << endl;}
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
fstream filestr;
filestr.open ("data.txt", fstream::in | fstream::out | fstream::app);
ifstream infile("data.txt");
infile >> navn >> point; //læs data fra det
gotoxy(5,5); cout << "1. " << navn << " " << point << endl; //udskriver data
gotoxy(5,6); cout << "2. " << endl;
gotoxy(5,7); cout << "3. " << endl;
gotoxy(5,8); cout << "4. " << endl;
gotoxy(5,9); cout << "5. " << endl;
gotoxy(0,40);cout << " " << endl;
filestr.close();
}
void instruc()
{
char ramme = 254;
int ramme_x;
int ramme_y;
for(ramme_x = 4; ramme_x < 60; ramme_x++)
{gotoxy(ramme_x, 4); cout << "=" << endl;}
for(ramme_x = 4; ramme_x < 60; ramme_x++)
{gotoxy(ramme_x, 10); cout << "=" << endl;}
for(ramme_y = 4; ramme_y < 11; ramme_y++)
{gotoxy(3, ramme_y); cout << ramme << endl;}
for(ramme_y = 4; ramme_y < 11; ramme_y++)
{gotoxy(60, ramme_y); cout << ramme << endl;}
gotoxy(5,5); cout << "To move, use the arrow keys." << endl;
gotoxy(5,6); cout << "Move the snake over to the cookies" << endl;
gotoxy(5,7); cout << "Each cookie gives 2 points." << endl;
gotoxy(5,8); cout << "Take care from the walls, you'll die if you toch them!" << endl;
gotoxy(5,9); cout << "Pres Enter to view main menu again" << endl;
cout << "\\n\\n\\n\\n\\n\\n" << endl;
}
void menu (int position)
{
if (position == 1)
{
farvetekst("New game", 10, 10, FOREGROUND_GREEN | BACKGROUND_BLUE);
farvetekst("Hi-score list", 10, 11, FOREGROUND_GREEN);
farvetekst("Instructions", 10, 12, FOREGROUND_GREEN);
farvetekst("Exit", 10, 13, FOREGROUND_GREEN);
}
else if (position == 2)
{
farvetekst("New game", 10, 10, FOREGROUND_GREEN);
farvetekst("Hi-score list", 10, 11, FOREGROUND_GREEN | BACKGROUND_BLUE);
farvetekst("Instructions", 10, 12, FOREGROUND_GREEN);
farvetekst("Exit", 10, 13, FOREGROUND_GREEN);
}
else if (position == 3)
{
farvetekst("New game", 10, 10, FOREGROUND_GREEN);
farvetekst("Hi-score list", 10, 11, FOREGROUND_GREEN);
farvetekst("Instructions", 10, 12, FOREGROUND_GREEN | BACKGROUND_BLUE);
farvetekst("Exit", 10, 13, FOREGROUND_GREEN);
}
else if (position == 4)
{
farvetekst("New game", 10, 10, FOREGROUND_GREEN);
farvetekst("Hi-score list", 10, 11, FOREGROUND_GREEN);
farvetekst("Instructions", 10, 12, FOREGROUND_GREEN);
farvetekst("Exit", 10, 13, FOREGROUND_GREEN | BACKGROUND_BLUE);
}
}
int main()
{
int exit = 0;
while (exit == 0)
{
srand(time(NULL));
fullscreen();
int opgave;
int gentag;
int valgt_menu = 0;
int position = 1;
//-reager på input-
menu(1);
do
{
int tast = 0;
gotoxy (0,40); // for at få cursor væk
if (kbhit()) //hvis der er keyboard input. kbhit = keybord hit
{
tast = getche (); //modtag input
}
//-ned pil-----
if (tast == 80)
{
position++;
menu(position);
if (position > 4)
{
position = 4;
}
}
//-op pil-----
if (tast == 72)
{
position--;
menu(position);
if (position < 1)
{
position = 1;
}
}
valgt_menu = tast;
}
while(valgt_menu != 13);
system("CLS");
//-valg af menu-
int tastet;
//-position == 1-----
if (position == 1)
{
spil_snake();
}
//-position 2-----
if (position == 2)
{
hi_score();
system("pause");
if (kbhit())
{
tastet = getche ();
}
if (tastet == 13)
{
system("pause");
system("CLS");
main();
}
}
if (position == 3)
{
instruc();
if (kbhit())
{
tastet = getche ();
}
if (tastet == 13)
{
system("pause");
system("CLS");
main();
}
}
if (position == 4)
{
exit = 1;
}
}//while løkken lukkes
return 0;
}
Variablen "min" står for minimum. Det er meningen at "min" skal være den sidste score man har fået, så når man får en ny, skal den gamle erstattes med den nye. Men den sidder fast ved 2. Gad vide hvorfor. Man skal jo ikke komme på hi-score listen hvis man har fået under 2 point!
Indlæg senest redigeret d. 23.04.2006 20:22 af Bruger #8784