-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStackItUp.java
More file actions
229 lines (196 loc) · 4.52 KB
/
StackItUp.java
File metadata and controls
229 lines (196 loc) · 4.52 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import java.awt.event.KeyEvent;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.util.*;
import javax.swing.JPanel;
@SuppressWarnings({ "serial" })
class StackItUp extends JFrame{
static int cntr=0,t=0;
int i,x1,x2,diff,xe1,xe2,width=250,yc=650;
Block blk =new Block();
Block nextblk =new Block();
Block temp =new Block();
Block temp2 =new Block();
ArrayList<Block> b = new ArrayList<Block>();
int x_left = 0, x_right = 720;
JLabel t1 = new JLabel();
StackItUp()
{
super("StackItUp");
JLabel t1 = new JLabel();
this.t1=t1;
JPanel newPanel = new JPanel();
newPanel.setBackground(Color.BLACK);
t1.setBounds(10,10,50,50);
t1.setForeground(Color.WHITE);
newPanel.add(t1);
add(newPanel);
File bg = new File(".//bg.wav");
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(bg);
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.start();
} catch(Exception e) {System.out.println(e);}
b.add(new Block());
nextblk = b.get(cntr);
nextblk.w=width;
cntr++;
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(width>0)
{
if(cntr>0)
{ temp=b.get(cntr-1);
x1=temp.x;
xe1=temp.x+temp.w;
if(xe1<x_left||x1>x_right) {
temp.w = 0;
}
else if(x1<x_left) {
temp.x = x_left;
temp.w -= x_left - x1;
}
else if(xe1>x_right) {
temp.w -= (xe1 - x_right);
}
x_left = temp.x;
x_right = temp.x+temp.w;
width = temp.w;
System.out.println("prev blk"+x1+" "+xe1);
/*if(cntr>1)
{temp2=b.get(cntr-2);
x2=temp2.x;
xe2=temp2.x+temp2.w;
System.out.println("prev of prev"+x2+" "+xe2);
System.out.println(" \n");
}*/
}
b.add(new Block());
nextblk = b.get(cntr);
nextblk.w=width;
if(cntr<10)
nextblk.y-=nextblk.h*cntr;
else {
nextblk.y-=500;
drop();
}
cntr++;
t1.setText("SCORE : "+(cntr-1));
if(cntr>1)
{File bg = new File(".//drop.wav");
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(bg);
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.start();
} catch(Exception e1) {System.out.println(e1);}
}
}
else
{
t1.setText("GAME OVER YOUR SCORE:"+(cntr-1));
nextblk.vel=0.0;
}
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(720,720);
setResizable(false);
setVisible(true);
}
public void move() {
nextblk.moverect();
}
public void paint(Graphics g) {
super.paint(g);
for (Block nb : b)
nb.paint(g);
}
void drop()
{ for (Block nb : b)
{nb.y+=nb.h;
}
}
void game_over()
{
t1.setText("GAME OVER YOUR SCORE:"+(cntr-1));
nextblk.vel=0.0;
}
public static void main(String[]args)throws InterruptedException {
{
StackItUp rect=new StackItUp();
while(true)
{
rect.move();
rect.repaint();
Thread.sleep(8);
}
}
}
}
@SuppressWarnings("serial")
class Block extends JFrame{
double vel=2.0;
int x,w,h=50,y=650,r=255,gn=255,b=255;
Block(){
int r = (int)(Math.random()*((255+1)));
int gn = (int)(Math.random()*((255+1)));
int b = (int)(Math.random()*((255+1)));
this.x=x;
this.y=y;
this.w=w;
this.h= h;
this.vel=vel;
this.r=r;
this.gn=gn;
this.b=b;
}
public void moverect()
{
if(x+vel<720 && x+vel>0)
{x+=vel;
}
if(x+vel>=720-w)
{vel=vel*(-1);
x+=vel;
}
if(x+vel==0)
{vel=vel*(-1);
x+=vel;
}
}
public void nextrect()
{
y-=h;
x=0;
}
@Override
public void paint(Graphics g)
{
g.setColor(new Color(r, gn, b));
g.fillRect(x,y,w,h);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_ENTER)
{
this.vel = 0.000;
}
}
}
//w-=cum_diff
//cum_diff+=diff