forked from digidotcom/DCRabbit_10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPONG.C
132 lines (107 loc) · 3.48 KB
/
PONG.C
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
/*
Copyright (c) 2015, Digi International Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/***********************************************************
Samples\PONG.C
Demonstration of output to STDIO. Uses ANSI escape sequences.
Appearance will vary with different character sets.
For best results the font for the STDIO window should be set
to "Terminal".
************************************************************/
#class auto
int xl, xh, yl, yh;
/********* Position Cursor **********/
void gotoxy (int x, int y)
{
printf( "\x1B[%d;%dH", y, x);
}
/********* Clear Screen ******/
void cls ()
{
printf( "\x1B[2J"); // erase screen and move to home position
}
void box(int x,int y, int w, int h)
{
int i;
char hor_line[100];
//define horizontal border
memset( hor_line, 0xC4, w - 1);
hor_line[w - 1] = 0;
//print top, with upper-left (0xDA) and upper-right (0xBF) corners
gotoxy(x, y);
printf ( "\xDA%s\xBF", hor_line);
//print sides
for( i = 1 ; i < h ; i++)
{
gotoxy(x, y + i);
putchar ( '\xB3' );
gotoxy(x + w, y + i);
putchar ( '\xB3' );
}
//print bottom, with lower-left (0xC0) and lower-right (0xD9) corners
gotoxy(x, y + h);
printf ( "\xC0%s\xD9", hor_line );
}
void pong()
{
int px,py; // Current Position
int dx,dy; // Current Direction
int nx,ny; // New Position
px = xl; py = yl; // Position Ball
dx = 1; dy = 1; // Give Direction
while (1)
{
costate
{
px += dx; py += dy; // Move Ball to new position
// draw ball and move cursor back to home position
gotoxy ( px, py );
putchar ( '*' );
gotoxy ( 1, 1);
waitfor(DelayMs(75));
nx = px + dx; // Try New Position
ny = py + dy;
if (nx <= xl || nx >= xh) // Reverse direction on collision
{
dx = -dx;
}
if (ny <= yl || ny >= yh)
{
dy = -dy;
}
// erase old ball
gotoxy ( px, py );
putchar ( ' ' );
}
}
}
///////////////////////////////////////////////////////////////////////
void main ()
{
cls (); // Clear Screen
xl = 1; // box coordinates
xh = 26;
yl = 4;
yh = 12;
/*
Calculation to center title:
xh-xl = width of box
subtract 4 (width of title) = amount of whitespace on either side of title
divide by 2 (after adding 1 to round up) = offset from left side of box
add offset to xl = screen position of title
*/
gotoxy ( (xh-xl-4+1)/2+xl, yl-1 ); // Position Cursor
printf ( "Pong" ); // Title
box(xl, yl, xh - xl, yh - yl);
pong();
}