Java/PHP/GitLab Fan, Passionate Developer and Loves Teaching

0%

Moving Stickman

Summary

Moving Stickman code in C Language using Visual Studio.

Customized functions for Visual Studio 2010 to act same as Turbo C

textcolor() “Changes text color”
clrscr() “Clears screen output”
gotoxy() “Move cursor to x,y position”

This stickman moves on keys.

w ==> up
a ==> left
s ==> down
d ==> right
c ==> changes the color

Press any key and change character of stickman

Code

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// MovingStickMan.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <Windows.h>

// Clear screen output
void clrscr()
{
system("cls");
}

// move cursor to x,y co-ordinates
void gotoxy(int a,int b)
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD Cord;
Cord.X = a;
Cord.Y = b;
SetConsoleCursorPosition(hStdOut,Cord);
}

// Change text color
void textcolor(int a)
{
int fontcolor = a;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
int color_A = 0;
color_A = _rotl(color_A,4) | fontcolor;
SetConsoleTextAttribute(hStdOut,a);
}

// Define global variables
char c;
int x=10,y=5;
int color=1;

/**
* Draw Sleeping Line with
*
* @param x "X co-ordinate of cursor position"
* @param y "Y co-ordinate of cursor position"
* @param len "Length of sleeping line to print"
* @param ch "Character to be used for printing"
*/
void sl(int x,int y,int len,char ch)
{
int i=0;
for(i=0;i<len;i++)
{
gotoxy(x+i,y);
putch(ch);
}
}

/**
* Draw Sleeping Line with
*
* @param x "X co-ordinate of cursor position"
* @param y "Y co-ordinate of cursor position"
* @param len "Length of vertical line to print"
* @param ch "Character to be used for printing"
*/
void vl(int x,int y,int len,char ch)
{
int i=0;
for(i=0;i<len;i++)
{
gotoxy(x,y+i);
putch(ch);
}
}

/**
* Draw stickman
*
* @param x "Starting x co-ordinate of stickman"
* @param y "Starting y co-ordinate of stickman"
* @param a "Character used for priting stickman"
*/
void sm(int x,int y,char a)
{
sl(x,y,5,a);
vl(x,y+1,3,a);
sl(x+1,y+3,3,a);
vl(x+4,y+1,3,a);
vl(x+2,y+4,7,a);
sl(x-3,y+6,11,a);
vl(x-3,y+7,1,a);
vl(x+7,y+7,1,a);
sl(x-1,y+11,7,a);
vl(x-1,y+12,3,a);
vl(x+5,y+12,3,a);
}

/**
* Move stickman to new position
*
* @param m "Operation used to move stickman"
*/
void move(char m)
{
switch(m)
{
case 'w':
sm(x,y,' ');
y--;
sm(x,y,c);
break;
case 'a':
sm(x,y,' ');
x--;
sm(x,y,c);
break;
case 's':
sm(x,y,' ');
y++;
sm(x,y,c);
break;
case 'd':
sm(x,y,' ');
x++;
sm(x,y,c);
break;
case 'c':
if(color==16)
color=1;
else
color++;
textcolor(color);
sm(x,y,c);
break;
default:
c = m;
sm(x,y,c);
}
}

/**
* Entry point of application
*/
int _tmain(int argc, _TCHAR* argv[])
{
char mv;
clrscr();
gotoxy(15,15);
printf("Enter character for Stickman==> ");
scanf("%c",&c);
clrscr();
sm(x, y, c);
// Iterate the loop till use presses `q` for exit
do
{
mv = getch();
move(mv);
}while(mv!='q');
getch();
return 0;
}