c++ console Spiel Problem
-
Hallo..
Ich bin am verzweifeln.. wer lust hat und etwas Zeit investieren möchte der kann mir gerne helfen und den code durchlesen um folgenden Fehler zu finden:Die Objekte (obstacles) kommen an zufälliger Stelle von oben an und gehen nach unten.
Wenn der Schuss (bei eingabe durch Spacetaste) an selber Stelle wie mit einem der Objekte
ist, geht das Objekt sofort aus dem Sichtfeld und fängt von neu wieder an.Wenn ich jedoch an bestimmten Positionen mit dem Schiff schieße, denkt das Programm, es sei ein Objekt im weg obwohl es ganz woanders ist. Dieses wird dann auch verschwinden.
Die Positionen von der aus das Programm denkt, es sei etwas im Weg, variieren je nachdem wie ich das Aussehen des Objektes ändere. Egal ob char ascii oder ein Zeichen. Habe die jz als int gemacht aber dort auch keine Besserung.
Sry für den langen code.#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <string.h>
#include <stdio.h>
using namespace std;void clearscreen(); //used to prevent flicker
int main()
{
char map[60][80];//loads the map with spaces and borders
for(int i=0; i < 55; ++i) {
for(int j=0; j < 75; ++j) {map[i][j] = ' ';
}
}int y = 52, x = 36; //the cars coordinates
srand(time(0));
//the obstacles coordiantes
int points = 0; //points that the player has earned
int speedship = 20; //determines the speed of the obstacles (and the car)
int speedobstacles=100;
//the cash coordinates
int cashcheck = 0; //balances when the cash spawns
int cashpickedup = 0;
int laserspeed = 10;
int l= 53, s = 36;
int laser = 0;
int f = 0;
int u = 0;
int G = 0;
int F = 0;
int t;
int zahl;
int a = 0, b = rand() % 74 + 1;
int g = 0, h = rand() % 74 + 1;
int m = 0, n = rand() % 74 + 1;
int zahl2 = 0;
bool startup = true;int obstacle = 'a';
int obstacle2 = 'b';
int obstacle1 = 'c';//the game loop
for (int G=0; G<=10; G++)
{for(;;) {
clearscreen();
//places the car at its default location
map[y][x-1] = 'I';
map[y][x+1] = 'I';
map[y][x-2] = 'I';
map[y][x-3] = '-';
map[y][x-4] = '-';
map[y][x-5] = 'i';
map[y][x+2] = '-';
map[y][x+3] = '-';
map[y][x+4] = 'i';
map[y-1][x+1] = '\';
map[y-1][x-1] = 'I';
map[y+1][x+1] = 'T';
map[y+1][x-1] = '-';
map[y+1][x-2] = 'T';
map[y+1][x-3] = '\';
map[y+1][x+2] = '/';
map[y-1][x-2] = 'I';
map[y-2][x-1] = 'I';
map[y-1][x-2] = '/';//generates the obstacles
map[a+1][b-1] = ' ';
map[a+1][b+1] = ' ';
map[a+2][b] = ' ';
++a;map[a+1][b-1] = obstacle1;
map[a+1][b+1] = obstacle1;
map[a+2][b] = obstacle1;map[m+1][n-1] = ' ';
map[m+1][n+1] = ' ';
map[m+2][n] = ' ';
++m;map[m+1][n-1] = obstacle;
map[m+1][n+1] = obstacle;
map[m+2][n] = obstacle;map[g+1][h-1] = ' ';
map[g+1][h+1] = ' ';
map[g+2][h] = ' ';
++g;map[g+1][h-1] = obstacle2;
map[g+1][h+1] = obstacle2;
map[g+2][h] = obstacle2;if(a > 53)
{
a = 0;
b = rand() % 74 + 1;}
if(m > 53)
{
m = 0;
n = rand() % 74 + 1;}
if(g > 53)
{
g = 0;
h = rand() % 74 + 1;}
//displays the map
for(int i=0; i < 55; ++i) {
for(int j=0; j < 75; ++j) {
cout << map[i][j];
if(j >= 74) {
cout << endl;
}
}
}
cout << " Points: " << points;
//does so the game starts after a key is pressed
if(startup) {
_getch();
startup = false;
}
//moves the car to the leftif(GetAsyncKeyState(VK_LEFT)) {
if(map[y][x-4] == obstacle) {
goto lost;
}
else if(map[y][x-4] != '|') {
map[y][x-1] = ' ';
map[y][x+1] = ' ';
map[y][x-2] = ' ';
map[y][x-3] = ' ';
map[y][x-4] = ' ';
map[y][x-5] = ' ';
map[y][x+2] = ' ';
map[y][x+3] = ' ';
map[y][x+4] = ' ';
map[y-1][x+1] = ' ';
map[y-1][x-1] = ' ';
map[y+1][x+1] = ' ';
map[y+1][x-1] = ' ';
map[y+1][x-2] = ' ';
map[y+1][x-3] = ' ';
map[y+1][x+2] = ' ';
map[y-1][x-2] = ' ';
map[y-2][x-1] = ' ';
map[y-1][x-2] = ' ';
if (laser == 0)
{map [l-2] [s] = ' ';
map [l-2] [s-1] = ' ';s -=3;
}
x -=3;
map[y][x-1] = 'I';
map[y][x+1] = 'I';
map[y][x-2] = 'I';
map[y][x-3] = '-';
map[y][x-4] = '-';
map[y][x-5] = 'i';
map[y][x+2] = '-';
map[y][x+3] = '-';
map[y][x+4] = 'i';
map[y-1][x+1] = '\';
map[y-1][x-1] = 'I';
map[y+1][x+1] = 'T';
map[y+1][x-1] = '-';
map[y+1][x-2] = 'T';
map[y+1][x-3] = '\';
map[y+1][x+2] = '/';
map[y-1][x-2] = 'I';
map[y-2][x-1] = 'I';
map[y-1][x-2] = '/';}
}
//moves the car to the right
if(GetAsyncKeyState(VK_RIGHT)) {
if(map[y][x+5] == obstacle) {
goto lost;
}
else if(map[y][x+5] != '|' ) {
map[y][x-1] = ' ';
map[y][x+1] = ' ';
map[y][x-2] = ' ';
map[y][x-3] = ' ';
map[y][x-4] = ' ';
map[y][x-5] = ' ';
map[y][x+2] = ' ';
map[y][x+3] = ' ';
map[y][x+4] = ' ';
map[y-1][x+1] = ' ';
map[y-1][x-1] = ' ';
map[y+1][x+1] = ' ';
map[y+1][x-1] = ' ';
map[y+1][x-2] = ' ';
map[y+1][x-3] = ' ';
map[y+1][x+2] = ' ';
map[y-1][x-2] = ' ';
map[y-2][x-1] = ' ';
map[y-1][x-2] = ' ';
if (laser == 0)
{map [l-2] [s] = ' ';
map [l-2] [s-1] = ' ';
s+=3;
}
x +=3;
map[y][x-1] = 'I';
map[y][x+1] = 'I';
map[y][x-2] = 'I';
map[y][x-3] = '-';
map[y][x-4] = '-';
map[y][x-5] = 'i';
map[y][x+2] = '-';
map[y][x+3] = '-';
map[y][x+4] = 'i';
map[y-1][x+1] = '\';
map[y-1][x-1] = 'I';
map[y+1][x+1] = 'T';
map[y+1][x-1] = '-';
map[y+1][x-2] = 'T';
map[y+1][x-3] = '\';
map[y+1][x+2] = '/';
map[y-1][x-2] = 'I';
map[y-2][x-1] = 'I';
map[y-1][x-2] = '/';}
}if(GetAsyncKeyState(VK_UP)) {
{
goto lost;
}
map[y][x-1] = ' ';
map[y][x+1] = ' ';
map[y][x-2] = ' ';
map[y][x-3] = ' ';
map[y][x-4] = ' ';
map[y][x-5] = ' ';
map[y][x+2] = ' ';
map[y][x+3] = ' ';
map[y][x+4] = ' ';
map[y-1][x+1] = ' ';
map[y-1][x-1] = ' ';
map[y+1][x+1] = ' ';
map[y+1][x-1] = ' ';
map[y+1][x-2] = ' ';
map[y+1][x-3] = ' ';
map[y+1][x+2] = ' ';
map[y-1][x-2] = ' ';
map[y-2][x-1] = ' ';
map[y-1][x-2] = ' ';
if (laser == 0)
{map [l-2] [s] = ' ';
map [l-2] [s-1] = ' ';l-=3;
}
y -=3;
map[y][x-1] = 'I';
map[y][x+1] = 'I';
map[y][x-2] = 'I';
map[y][x-3] = '-';
map[y][x-4] = '-';
map[y][x-5] = 'i';
map[y][x+2] = '-';
map[y][x+3] = '-';
map[y][x+4] = 'i';
map[y-1][x+1] = '\';
map[y-1][x-1] = 'I';
map[y+1][x+1] = 'T';
map[y+1][x-1] = '-';
map[y+1][x-2] = 'T';
map[y+1][x-3] = '\';
map[y+1][x+2] = '/';
map[y-1][x-2] = 'I';
map[y-2][x-1] = 'I';
map[y-1][x-2] = '/';}
}if(GetAsyncKeyState(VK_DOWN)) {
goto lost;
}
map[y][x-1] = ' ';
map[y][x+1] = ' ';
map[y][x-2] = ' ';
map[y][x-3] = ' ';
map[y][x-4] = ' ';
map[y][x-5] = ' ';
map[y][x+2] = ' ';
map[y][x+3] = ' ';
map[y][x+4] = ' ';
map[y-1][x+1] = ' ';
map[y-1][x-1] = ' ';
map[y+1][x+1] = ' ';
map[y+1][x-1] = ' ';
map[y+1][x-2] = ' ';
map[y+1][x-3] = ' ';
map[y+1][x+2] = ' ';
map[y-1][x-2] = ' ';
map[y-2][x-1] = ' ';
map[y-1][x-2] = ' ';
if (laser == 0)
{map [l-2] [s] = ' ';
map [l-2] [s-1] = ' ';l+=3;
}
y +=3;
map[y][x-1] = 'I';
map[y][x+1] = 'I';
map[y][x-2] = 'I';
map[y][x-3] = '-';
map[y][x-4] = '-';
map[y][x-5] = 'i';
map[y][x+2] = '-';
map[y][x+3] = '-';
map[y][x+4] = 'i';
map[y-1][x+1] = '\';
map[y-1][x-1] = 'I';
map[y+1][x+1] = 'T';
map[y+1][x-1] = '-';
map[y+1][x-2] = 'T';
map[y+1][x-3] = '\';
map[y+1][x+2] = '/';
map[y-1][x-2] = 'I';
map[y-2][x-1] = 'I';
map[y-1][x-2] = '/';}
}if(GetAsyncKeyState(VK_SPACE)) //Schießen
{
for (f=0; f<=53; f++)
{if ( map [l-f] [s] == obstacle || map [l-f] [s-1] == obstacle)
{map [m-1] [n-1] ='';
map [m-1] [n+1] = '';
map [m-1] [n] = '';
map[m] [n] = '';
map[m+1][n-1] = '';
map[m+1][n+1] = '';
map[m+2][n] = '*';
m = 53;
}if ( map [l-f] [s] == obstacle2 || map [l-f] [s-1] == obstacle2)
{map [g-1] [h-1] ='';
map [g-1] [h+1] = '';
map [g-1] [h] = '';
map[g] [h] = '';
map[g+1][h-1] = '';
map[g+1][h+1] = '';
map[g+2][h] = '*';g=53;
}
if ( map [l-f] [s] == obstacle1 || map [l-f] [s-1] == obstacle1)
{map [a-1] [b-1] ='';
map [a-1] [b+1] = '';
map [a-1] [b] = '';
map[a] [b] = '';
map[a+1][b-1] = '';
map[a+1][b+1] = '';
map[a+2][b] = '*';
a = 53;
}map [l-f] [s] = '|';
map [l-f] [s-1] = '|';}
F=1;
t=1;}
if (F==1)
{++G;
if (G==2){
for (f=0; f<=53; f++)
{map [l-f] [s] = ' ';
map [l-f] [s-1] = ' ';
map [l-f] [s+1] = ' ';
map [l-f] [s-2] = ' ';
map [l-f] [s+2] = ' ';
map [l-f] [s-3] = ' ';
map [l-f] [s+3] = ' ';
map [l-f] [s-4] = ' ';
map [l-f] [s+4] = ' ';
map [l-f] [s-5] = ' ';
map [l-f] [s+5] = ' ';
map [l-f] [s-6] = ' ';}
G=0;
}
if (G==0)
{F=0;
}
}//checks if the car crashed
lost:
cout << "\n\nDu hast verloren!\n" << endl;
cin.get();
return 0;
}
//checks if the player picked up cash++points;
//speeds up the obstacles each time the player gets another 100 points
if(points == 100 || points == 200 || points == 300 || points == 400) {}
Sleep(speedship);
}
}return 0;
}
void clearscreen()
{
HANDLE hOut;
COORD Position;hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Position.X = 0;
Position.Y = 0;
SetConsoleCursorPosition(hOut, Position);
}
-
[code] #include <iostream> #include <Windows.h> #include <cstdlib> #include <ctime> #include <conio.h> #include <string.h> #include <stdio.h> using namespace std; void clearscreen(); //used to prevent flicker int main() { char map[60][80]; //loads the map with spaces and borders for(int i=0; i < 55; ++i) { for(int j=0; j < 75; ++j) { map[i][j] = ' '; } } int y = 52, x = 36; //the cars coordinates srand(time(0)); //the obstacles coordiantes int points = 0; //points that the player has earned int speedship = 20; //determines the speed of the obstacles (and the car) int speedobstacles=100; //the cash coordinates int cashcheck = 0; //balances when the cash spawns int cashpickedup = 0; int laserspeed = 10; int l= 53, s = 36; int laser = 0; int f = 0; int u = 0; int G = 0; int F = 0; int t; int zahl; int a = 0, b = rand() % 74 + 1; int g = 0, h = rand() % 74 + 1; int m = 0, n = rand() % 74 + 1; int zahl2 = 0; bool startup = true; int obstacle = 'a'; int obstacle2 = 'b'; int obstacle1 = 'c'; //the game loop for (int G=0; G<=10; G++) { for(;;) { clearscreen(); //places the car at its default location map[y][x] = 'I'; map[y][x-1] = 'I'; map[y][x+1] = 'I'; map[y][x-2] = 'I'; map[y][x-3] = '-'; map[y][x-4] = '-'; map[y][x-5] = 'i'; map[y][x+2] = '-'; map[y][x+3] = '-'; map[y][x+4] = 'i'; map[y-1][x+1] = '\\'; map[y-1][x-1] = 'I'; map[y+1][x+1] = 'T'; map[y+1][x-1] = '-'; map[y+1][x] = '-'; map[y+1][x-2] = 'T'; map[y+1][x-3] = '\\'; map[y+1][x+2] = '/'; map[y-1][x-2] = 'I'; map[y-2][x-1] = 'I'; map[y-1][x] = 'I'; map[y-2][x] = 'I'; map[y-1][x-2] = '/'; //generates the obstacles map[a+1][b-1] = ' '; map[a+1][b+1] = ' '; map[a+2][b] = ' '; ++a; map[a+1][b-1] = obstacle1; map[a+1][b+1] = obstacle1; map[a+2][b] = obstacle1; map[m+1][n-1] = ' '; map[m+1][n+1] = ' '; map[m+2][n] = ' '; ++m; map[m+1][n-1] = obstacle; map[m+1][n+1] = obstacle; map[m+2][n] = obstacle; map[g+1][h-1] = ' '; map[g+1][h+1] = ' '; map[g+2][h] = ' '; ++g; map[g+1][h-1] = obstacle2; map[g+1][h+1] = obstacle2; map[g+2][h] = obstacle2; if(a > 53) { a = 0; b = rand() % 74 + 1; } if(m > 53) { m = 0; n = rand() % 74 + 1; } if(g > 53) { g = 0; h = rand() % 74 + 1; } //displays the map for(int i=0; i < 55; ++i) { for(int j=0; j < 75; ++j) { cout << map[i][j]; if(j >= 74) { cout << endl; } } } cout << " Points: " << points; //does so the game starts after a key is pressed if(startup) { _getch(); startup = false; } //moves the car to the left if(GetAsyncKeyState(VK_LEFT)) { if(map[y][x-4] == obstacle) { goto lost; } else if(map[y][x-4] != '|') { map[y][x] = 'I'; map[y][x-1] = ' '; map[y][x+1] = ' '; map[y][x-2] = ' '; map[y][x-3] = ' '; map[y][x-4] = ' '; map[y][x-5] = ' '; map[y][x+2] = ' '; map[y][x+3] = ' '; map[y][x+4] = ' '; map[y-1][x+1] = ' '; map[y-1][x-1] = ' '; map[y+1][x+1] = ' '; map[y+1][x-1] = ' '; map[y+1][x] = ' '; map[y+1][x-2] = ' '; map[y+1][x-3] = ' '; map[y+1][x+2] = ' '; map[y-1][x-2] = ' '; map[y-2][x-1] = ' '; map[y-1][x] = ' '; map[y-2][x] = ' '; map[y-1][x-2] = ' '; if (laser == 0) { map [l-2] [s] = ' '; map [l-2] [s-1] = ' '; s -=3; } x -=3; map[y][x] = 'I'; map[y][x-1] = 'I'; map[y][x+1] = 'I'; map[y][x-2] = 'I'; map[y][x-3] = '-'; map[y][x-4] = '-'; map[y][x-5] = 'i'; map[y][x+2] = '-'; map[y][x+3] = '-'; map[y][x+4] = 'i'; map[y-1][x+1] = '\\'; map[y-1][x-1] = 'I'; map[y+1][x+1] = 'T'; map[y+1][x-1] = '-'; map[y+1][x] = '-'; map[y+1][x-2] = 'T'; map[y+1][x-3] = '\\'; map[y+1][x+2] = '/'; map[y-1][x-2] = 'I'; map[y-2][x-1] = 'I'; map[y-1][x] = 'I'; map[y-2][x] = 'I'; map[y-1][x-2] = '/'; } } //moves the car to the right if(GetAsyncKeyState(VK_RIGHT)) { if(map[y][x+5] == obstacle) { goto lost; } else if(map[y][x+5] != '|' ) { map[y][x] = 'I'; map[y][x-1] = ' '; map[y][x+1] = ' '; map[y][x-2] = ' '; map[y][x-3] = ' '; map[y][x-4] = ' '; map[y][x-5] = ' '; map[y][x+2] = ' '; map[y][x+3] = ' '; map[y][x+4] = ' '; map[y-1][x+1] = ' '; map[y-1][x-1] = ' '; map[y+1][x+1] = ' '; map[y+1][x-1] = ' '; map[y+1][x] = ' '; map[y+1][x-2] = ' '; map[y+1][x-3] = ' '; map[y+1][x+2] = ' '; map[y-1][x-2] = ' '; map[y-2][x-1] = ' '; map[y-1][x] = ' '; map[y-2][x] = ' '; map[y-1][x-2] = ' '; if (laser == 0) { map [l-2] [s] = ' '; map [l-2] [s-1] = ' '; s+=3; } x +=3; map[y][x] = 'I'; map[y][x-1] = 'I'; map[y][x+1] = 'I'; map[y][x-2] = 'I'; map[y][x-3] = '-'; map[y][x-4] = '-'; map[y][x-5] = 'i'; map[y][x+2] = '-'; map[y][x+3] = '-'; map[y][x+4] = 'i'; map[y-1][x+1] = '\\'; map[y-1][x-1] = 'I'; map[y+1][x+1] = 'T'; map[y+1][x-1] = '-'; map[y+1][x] = '-'; map[y+1][x-2] = 'T'; map[y+1][x-3] = '\\'; map[y+1][x+2] = '/'; map[y-1][x-2] = 'I'; map[y-2][x-1] = 'I'; map[y-1][x] = 'I'; map[y-2][x] = 'I'; map[y-1][x-2] = '/'; } } if(GetAsyncKeyState(VK_UP)) { if(map[y-1][x] == obstacle) { goto lost; } else if(map[y-1][x] != '|') { map[y][x] = ' '; map[y][x-1] = ' '; map[y][x+1] = ' '; map[y][x-2] = ' '; map[y][x-3] = ' '; map[y][x-4] = ' '; map[y][x-5] = ' '; map[y][x+2] = ' '; map[y][x+3] = ' '; map[y][x+4] = ' '; map[y-1][x+1] = ' '; map[y-1][x-1] = ' '; map[y+1][x+1] = ' '; map[y+1][x-1] = ' '; map[y+1][x] = ' '; map[y+1][x-2] = ' '; map[y+1][x-3] = ' '; map[y+1][x+2] = ' '; map[y-1][x-2] = ' '; map[y-2][x-1] = ' '; map[y-1][x] = ' '; map[y-2][x] = ' '; map[y-1][x-2] = ' '; if (laser == 0) { map [l-2] [s] = ' '; map [l-2] [s-1] = ' '; l-=3; } y -=3; map[y][x] = 'I'; map[y][x-1] = 'I'; map[y][x+1] = 'I'; map[y][x-2] = 'I'; map[y][x-3] = '-'; map[y][x-4] = '-'; map[y][x-5] = 'i'; map[y][x+2] = '-'; map[y][x+3] = '-'; map[y][x+4] = 'i'; map[y-1][x+1] = '\\'; map[y-1][x-1] = 'I'; map[y+1][x+1] = 'T'; map[y+1][x-1] = '-'; map[y+1][x] = '-'; map[y+1][x-2] = 'T'; map[y+1][x-3] = '\\'; map[y+1][x+2] = '/'; map[y-1][x-2] = 'I'; map[y-2][x-1] = 'I'; map[y-1][x] = 'I'; map[y-2][x] = 'I'; map[y-1][x-2] = '/'; } } if(GetAsyncKeyState(VK_DOWN)) { if(map[y+1][x] == obstacle) { goto lost; } else if(map[y+1][x] != '|') { map[y][x] = ' '; map[y][x-1] = ' '; map[y][x+1] = ' '; map[y][x-2] = ' '; map[y][x-3] = ' '; map[y][x-4] = ' '; map[y][x-5] = ' '; map[y][x+2] = ' '; map[y][x+3] = ' '; map[y][x+4] = ' '; map[y-1][x+1] = ' '; map[y-1][x-1] = ' '; map[y+1][x+1] = ' '; map[y+1][x-1] = ' '; map[y+1][x] = ' '; map[y+1][x-2] = ' '; map[y+1][x-3] = ' '; map[y+1][x+2] = ' '; map[y-1][x-2] = ' '; map[y-2][x-1] = ' '; map[y-1][x] = ' '; map[y-2][x] = ' '; map[y-1][x-2] = ' '; if (laser == 0) { map [l-2] [s] = ' '; map [l-2] [s-1] = ' '; l+=3; } y +=3; map[y][x] = 'I'; map[y][x-1] = 'I'; map[y][x+1] = 'I'; map[y][x-2] = 'I'; map[y][x-3] = '-'; map[y][x-4] = '-'; map[y][x-5] = 'i'; map[y][x+2] = '-'; map[y][x+3] = '-'; map[y][x+4] = 'i'; map[y-1][x+1] = '\\'; map[y-1][x-1] = 'I'; map[y+1][x+1] = 'T'; map[y+1][x-1] = '-'; map[y+1][x] = '-'; map[y+1][x-2] = 'T'; map[y+1][x-3] = '\\'; map[y+1][x+2] = '/'; map[y-1][x-2] = 'I'; map[y-2][x-1] = 'I'; map[y-1][x] = 'I'; map[y-2][x] = 'I'; map[y-1][x-2] = '/'; } } if(GetAsyncKeyState(VK_SPACE)) //Schießen { for (f=0; f<=53; f++) { if ( map [l-f] [s] == obstacle || map [l-f] [s-1] == obstacle) { map [m-1] [n-1] ='*'; map [m-1] [n+1] = '*'; map [m-1] [n] = '*'; map[m] [n] = '*'; map[m+1][n-1] = '*'; map[m+1][n+1] = '*'; map[m+2][n] = '*'; m = 53; } if ( map [l-f] [s] == obstacle2 || map [l-f] [s-1] == obstacle2) { map [g-1] [h-1] ='*'; map [g-1] [h+1] = '*'; map [g-1] [h] = '*'; map[g] [h] = '*'; map[g+1][h-1] = '*'; map[g+1][h+1] = '*'; map[g+2][h] = '*'; g=53; } if ( map [l-f] [s] == obstacle1 || map [l-f] [s-1] == obstacle1) { map [a-1] [b-1] ='*'; map [a-1] [b+1] = '*'; map [a-1] [b] = '*'; map[a] [b] = '*'; map[a+1][b-1] = '*'; map[a+1][b+1] = '*'; map[a+2][b] = '*'; a = 53; } map [l-f] [s] = '|'; map [l-f] [s-1] = '|'; } F=1; t=1; } if (F==1) { ++G; if (G==2) { for (f=0; f<=53; f++) { map [l-f] [s] = ' '; map [l-f] [s-1] = ' '; map [l-f] [s+1] = ' '; map [l-f] [s-2] = ' '; map [l-f] [s+2] = ' '; map [l-f] [s-3] = ' '; map [l-f] [s+3] = ' '; map [l-f] [s-4] = ' '; map [l-f] [s+4] = ' '; map [l-f] [s-5] = ' '; map [l-f] [s+5] = ' '; map [l-f] [s-6] = ' '; } G=0; } if (G==0) { F=0; } } //checks if the car crashed if(map[y-2][x] == obstacle2 || map[y-2][x-1] == obstacle2 || map[y-2][x+1] == obstacle2 || map[y] [x-4] == obstacle2 || map[y] [x+5] == obstacle2) { lost: cout << "\n\nDu hast verloren!\n" << endl; cin.get(); return 0; } //checks if the player picked up cash ++points; //speeds up the obstacles each time the player gets another 100 points if(points == 100 || points == 200 || points == 300 || points == 400) { } Sleep(speedship); } } return 0; } void clearscreen() { HANDLE hOut; COORD Position; hOut = GetStdHandle(STD_OUTPUT_HANDLE); Position.X = 0; Position.Y = 0; SetConsoleCursorPosition(hOut, Position); }
-
Das mag vielleicht hart klingen, aber: Der Code ist Muell. Und zwar uebler Muell. Das und der Umstand, dass du ueber 500 Zeilen schlecht formatierten Code hier postest und sagst: Leute, das ist mein Problem, loest das mal, wird dazu fuehren, dass sich sehr wahrscheinlich niemand damit befassen wird.
Was solltest du also tun?
Einen Debugger nutzen, um verschiedene Faelle durchzugehen und zu sehen, was den Fehler verursacht. Wenn das nicht moeglich ist, den Code auf das noetigste (!) zu reduzieren. Meistens findet man dann selbst den Fehler, ansonsten ist es einfacher, dann willige Helfer zu finden.
Hier zum Beispiel: dein Code kompiliert bei mir nicht mal. Wie soll man denn da helfen?Was du aber noch eher tun solltest, ist ein gutes Buch ueber C++ zu lesen und deinen Code neuschreiben. Da ist so viel uebel, da weiss man gar nicht wo man anfangen soll.
-
ja ok, ich habe einige Variablen drinn die ich nicht benutzt habe nur zum ausprobieren. Und wirklich viel Ahnung hab ich auch nicht

Bei mir kompiliert alles ohne Fehler nur an dieser Stelle harpert es. (Ich benutze code blocks).
Aber trotzdem Danke
-
Jannisspace schrieb:
Wenn ich jedoch an bestimmten Positionen mit dem Schiff schieße, denkt das Programm, es sei ein Objekt im weg obwohl es ganz woanders ist. Dieses wird dann auch verschwinden.
Kann ich auch nach erreichen von ~280 Punkten nicht nachvollziehen. Das einzige was ich dir sagen kann ist, das die Steuerung und der Bildaufbau eine Qual sind.
-
Also das "Flackern" wurde durch VOID CLEARSCREEN() kein bisschen besser. Und das du zwischendurch einfach mal 7 Zeilen frei hast ist auch unnötig. Und ganz ehrlich: Die wirklich interessanten Sachen sind nicht kommentiert. Naja, ich seh mir das mal an wenn ich zuhause bin. BG
PS: Benutz mal den "C++" Buttton. Syntax Highlighting hilft!!!
-
Wow also ich dachte echt das kann man vielleicht retten... naja...
Also erstmal habe ich verschiedene Settings ausprobiert. Ein bisschen mit den Startparametern herumgespielt. Hat trotzdem Augenkrebs verursacht. Das System den Bildschirm zu einem Koordinatensystem zu verwandeln ist an sich gut. Aber:- Der Bildschirm flackert echt hart. Ich denke, dass das Programm viel zu viel Leistung frisst um vernünftig zu laufen. Das Spiel zieht meine CPU auf bis zu 70%. Und damit läuft Skyrim locker flockig durch!
- Das Bild ist unruhig. das Fenster nach unten zu vergrössern hat geholfen. Flackern tuts immer noch.
- Wenn ich Schieße wird das Programm gekillt und meine CPU geht kurz auf 100%. Kann aber evtl. am Compiler liegen.
Die Variablen sehen danach so aus:
map 0x0079e718 {0x0079e718 " || ÌÌÌÌÌ..., ...} char[60][80]
map[y-2] 0x0079f5c8 " || ÌÌÌÌÌ... char[80]
map[y] 0x0079f668 " i--I||I--i ÌÌÌÌÌ... char[80]Irgendwas verhunzt wohl die Speicherstellen...
- AUßERDEM!!! KEIN GOTO!!! Dein Code war schon sehr unübersichtlich, aber nicht soo kompliziert. Daher verständlich. GOTO führ zu "Spaghetti-Code". Nicht tun. Bitte. Es gibt immer bessere Lösungen. Schleifen zum Beispiel. (Ausnahmen bestätigen die Regel. Aber schleifen mit einer BREAK Anweisung wären wohl besser gewesen.)
Zu dem vorhandenen Bug kann ich ja leider nichts sagen da das Programm einen Schuss mit einem Angriff auf meine CPU gleichsetzt

Fazit: An sich kein schlechter einstieg für C++. Sowas habe ich (auch in anderen Sprachen) schon häufiger gesehen. Aber vieles ist eben unschön gelöst.
Greets,
PALone
-
PALone schrieb:
Also das "Flackern" wurde durch VOID CLEARSCREEN() kein bisschen besser.
Wie denn auch? Die ganze 'Map' wird andauernd neu via cout ausgegeben. Das kann doch nur flackern!
-
Davor jeweils ein CLEARSCREEN() (oder SYSTEM("cls") dachte das könnte evtl Performance heben da systemnah) haben NIX genützt. Hab mich aber auch nur etwa 30 min damit befasst. Nachdem ich das erste GOTO gesehen habe war schon verloren

-
PALone schrieb:
Davor jeweils ein CLEARSCREEN() (oder SYSTEM("cls") dachte das könnte evtl Performance heben da systemnah) haben NIX genützt.
Was ist daran "systemnah"?
system()startet erst mal eine neue Instanz des Befehlsinterpreters (<-- damit hast du performancetechnisch schon verloren) der dann"cls"ausführt, was wiederum den gesamten Console Screen Buffer mit dem Zeichen' 'und den Attributen (Farbe) des Zeichens in der linken oberen Ecke überschreibt.Auf die Lösung hat Belli schon angespielt:
1. sich merken was neu gezeichnet werden muss
2. nur eben das zeichnen
3. nicht über gepufferte Ausgabe wiestd::coutsondern am besten direkt in den Console Screen Buffer schreiben.
4. ?
5. Profit.// edit: wenns immer noch zu lahm ist: double buffering.