Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

13.10.2013, 21:01

Hilfe bei einem Programm. Cursor bewegen auf der Console

Hallo Community,

Ich habe mal nach einer passenden Lösung für mein Programm gesucht und habe diesen Quellcode gefunden. Nur habe ich ein Problem. Ich weis nicht wie ich da etwas reinzeichne, wie z.B. Umrisse mit "-" oder mit "|". Kann mir da jemand helfe?

Ich Programmiere mit c++ mit dem Devcpp Compiler.

Der Quellcode:

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <cstdio> 
#include <conio.h> 
#include <windows.h> 

const int KEY_UP = 0x48; 
const int KEY_DOWN = 0x50; 
const int KEY_LEFT = 0x4b; 
const int KEY_RIGHT = 0x4d; 
const int KEY_ESCAPE = 0x1b; 

const int CONSOLE_WIDTH = 80; 
const int CONSOLE_HEIGHT = 25; 

enum color 
{ 
black, 
darkblue, 
darkgreen, 
darkcyan, 
darkred, 
darkpurple, 
darkgray, 
darkwhite, 
gray, 
blue, 
green, 
cyan, 
red, 
purple, 
yellow, 
white 
}; 

//////////////////////////////////////////////////////////////////// 
// sets the cursor position 
//////////////////////////////////////////////////////////////////// 
void gotoxy(int x, int y) 
{ 
COORD point; 
point.X = x; 
point.Y = y; 
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point); 
} 

//////////////////////////////////////////////////////////////////// 
// clears the screen 
//////////////////////////////////////////////////////////////////// 
void clrscr() 
{ 
CONSOLE_SCREEN_BUFFER_INFO csbi; 
DWORD bufferSize, charsWritten; 

COORD topleft = {0, 0}; 
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 

GetConsoleScreenBufferInfo(hConsole, &csbi); 
bufferSize = csbi.dwSize.X * csbi.dwSize.Y; 
FillConsoleOutputCharacter(hConsole, TEXT(' '), bufferSize, topleft, &charsWritten); 
GetConsoleScreenBufferInfo(hConsole, &csbi); 
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, bufferSize, topleft, &charsWritten); 
SetConsoleCursorPosition(hConsole, topleft); 
} 

//////////////////////////////////////////////////////////////////// 
// sets foreground and background color 
//////////////////////////////////////////////////////////////////// 
void setcolor(int colorBack, int colorFore) 
{ 
int back = 0; 
if (colorBack & 1) back |= BACKGROUND_BLUE; 
if (colorBack & 2) back |= BACKGROUND_GREEN; 
if (colorBack & 4) back |= BACKGROUND_RED; 
if (colorBack & 8) back |= BACKGROUND_INTENSITY; 

int fore = 0; 
if (colorFore & 1) fore |= FOREGROUND_BLUE; 
if (colorFore & 2) fore |= FOREGROUND_GREEN; 
if (colorFore & 4) fore |= FOREGROUND_RED; 
if (colorFore & 8) fore |= FOREGROUND_INTENSITY; 

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), back | fore); 
} 


//////////////////////////////////////////////////////////////////// 
// clamps a value between min/max 
//////////////////////////////////////////////////////////////////// 
template<typename T> 
T clamp(const T& min, const T& max, const T& value) 
{ 
return (value < min ? min : value > max ? max : value); 
} 


//////////////////////////////////////////////////////////////////// 
// draws a message centered 
//////////////////////////////////////////////////////////////////// 
void centerText(const char *message, int ypos) 
{ 
int len = strlen(message); 
if (len > 0) 
{ 
int xpos = (CONSOLE_WIDTH - len) / 2; 
gotoxy(xpos, ypos); 
printf(message); 
} 
} 

int main() 
{ 
bool exit = false; 
int x = 0; 
int y = 0; 

do 
{ 
setcolor(darkblue, yellow); 
centerText("use arrow keys to move or escape to exit...", CONSOLE_HEIGHT-1); 

setcolor(black, green); 
gotoxy(x, y); 
printf("@"); 

int key = _getch(); 
if (!key || key == 0xe0) 
key = _getch(); 

switch (key) 
{ 
case KEY_LEFT: --x; break; 
case KEY_RIGHT: ++x; break; 
case KEY_UP: --y; break; 
case KEY_DOWN: ++y; break; 
case KEY_ESCAPE: exit = true; break; 
} 

x = clamp(0, CONSOLE_WIDTH-1, x); 
y = clamp(0, CONSOLE_HEIGHT-2, y); 

clrscr(); 

} while (!exit); 
}

Mit freundlichen Grüßen
Sincap

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Sincap80« (13.10.2013, 21:17)


DeKugelschieber

Community-Fossil

Beiträge: 2 641

Wohnort: Rheda-Wiedenbrück

Beruf: Software-Entwickler

  • Private Nachricht senden

2

13.10.2013, 21:06

First thing you should use: Code tags.

3

13.10.2013, 21:15

Thank you

FSA

Community-Fossil

  • Private Nachricht senden

4

13.10.2013, 21:58

Ich weiß nicht ganz genau was du machen möchtest, aber std::cout sollte etwas für dich sein.

@DK: Happy Birthday :D

Zitat

Der RCCSWU (RandomCamelCaseSomtimesWithUndersquare) Stil bricht auch mal mit den veraltet strukturierten Denkmustern und erlaubt dem Entwickler seine Kreativität zu entfalten.

DeKugelschieber

Community-Fossil

Beiträge: 2 641

Wohnort: Rheda-Wiedenbrück

Beruf: Software-Entwickler

  • Private Nachricht senden

5

13.10.2013, 22:54

Danke FSA :D

@Thread: meinst du sowas: http://www.cplusplus.com/forum/general/41709/ ? (nur Windows)

Werbeanzeige