-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_bouncing_Ball.java
160 lines (128 loc) · 3.42 KB
/
random_bouncing_Ball.java
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
/*=============================================================================================
* Author :
*
* Himanshu Sharma
* Github - himanshuthecoder
* ********************************************
* Bouncing Ball Pattern 2
* ********************************************
*
*
*
* About:
* It is a simple pattern program which prints the pattern on screen
* In this program '@@' shape ball are there which moves on screen from left to right and up to down and vice versa
* you can call it a type of screen saver program.
*
* Instruction:-
* run the program with having terminal width & height as below mention
* width=80
* height=24
*
* or change the widht and height according to your terminal specification
* otherwise it won't show the desired output
*
*=============================================================================================*/
import java.awt.*; //used for taking color
class random_pattern
{
int width=80; //widht of terminal
int height=24; //height of terminal
int rev=2; //for reverse the ball direction
int rev2=0; //for reverse the ball direction again
int limit=0; //represent the top and botton limit of ball
int up=0; //represent the upside of ball
int down=0; //represent the upside of ball
int m=1;
int colup=0; //used to check collision conditon of ball with wall
//used for color the ball
public final String red="\u001B[31m";
public final String reset="\u001B[0m";
//control the behavior of ball
void move_ball()
{
//used for repeting the pattern
while(m!=0)
{
rev=2; //reinitializing of variable
/*** Creating the pattern ***/
for(int i=1;i<=width*2;i++)
{
try
{
Thread.sleep(20);
System.out.print("\033[H\033[2J"); //for clearing screen
System.out.flush();
}
catch(Exception e)
{
}
//managing when ball moves upward
if(limit==0)
{ up++; colup++; down=0;rev2=0; }
//managing when ball moves downward
if(limit==1)
{ up=0; down++; colup--; }
//moves the ball form left to right
if(i<=width-1)
{
//moves ball downward
if(limit==0)
{
for(int x=1;x<=up;x++)
System.out.println("");
limit=0;
if(colup==height-2)
limit=1;
}
//moves ball upward
if(limit==1)
{
rev2=rev2+1;
for(int x=1;x<=height-rev2;x++)
System.out.println("");
limit=1;
if(rev2==height)
limit=0;
}
for(int x=1;x<=i;x++)
System.out.print(" ");
}
//moves the ball form right to left
if(i>=width)
{
//moves the ball downward
if(limit==0)
{
for(int x=1;x<=up;x++)
System.out.println("");
limit=0;
if(colup==height-2)
limit=1;
}
//moves the ball upward
if(limit==1)
{ rev2=rev2+1;
for(int x=1;x<=height-rev2;x++)
System.out.println("");
limit=1;
if(rev2==height)
limit=0;
}
rev=rev+1;
for(int x=1;x<=width-rev;x++)
System.out.print(" ");
}
//printing ball
System.out.print(red+"@@"+reset);
}
}
}
//main method
public static void main(String args[])
{
random_pattern obj=new random_pattern(); //creating object
obj.move_ball(); //calling method
}
}