Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
7.9 kB
0
Indexable
Never
import javax.swing.*;
2
import java.awt.*;
3
import java.awt.event.*;
4
​
5
public class Panel00 extends JPanel {
6
    Timer t = new Timer(1, new Listener());
7
    int ctr = 0;
8
    double G = 0.1; //Gravitational Constant
9
​
10
    final int xpos = 280;
11
    
12
    double[] p2d = {280, 200};
13
    double[] v2d = {0, 0};
14
    
15
    int points = 0;
16
    int lives = 0;
17
    int sides = 13;
18
    
19
    double snorm = 400;
20
    double sd = 450;
21
    double sv = 0;
22
    boolean setlock = false;
23
    boolean rdown, ldown;
24
    double paddle = 130;
25
    double rtheta = 0;
26
    double ltheta = 0;
27
    
28
    int preset[][] = {
29
            {0, 400, 135, 450,1}, //right paddle
30
            {135, 450, 270, 400,1}, //left paddle
31
            {270, 0, 300, 20, 1}, //first bouncey thingy
32
            {291, 0, 291, 500, 1}, //right wall
33
            {-1, 0, 270, 0, 1}, //top wall
34
            {0, -1, 0, 500, 1} //left wall
35
    };
36
​
37
    int[][] balls = {
38
            {80, 80, 30, 50},
39
            {230, 280, 20, 200},
40
            {50, 200, 25, 100},
41
            {200, 100, 10, 500}
42
    };
43
    int lines[][] = new int[100][5];
44
    
45
    public Panel00(){
46
        super();
47
        t.start();
48
        addKeyListener(new Key());
49
        setFocusable(true);
50
        
51
        for(int i = 0; i < preset.length; i++){
52
            lines[i] = preset[i];
53
        }
54
        
55
        int plen = preset.length;
56
        
57
        int ct = 0;
58
        for(int k = 0; k < balls.length; k++){
59
            int px = balls[k][0], py = balls[k][1], radius = balls[k][2];
60
            for(double i = 0; i < 2 * Math.PI; i+= 2 * Math.PI/ sides){
61
                ct++;
62
                lines[plen + ct][0] = px + (int) (radius * Math.cos(i));
63
                lines[plen + ct][1] = py + (int) (radius * Math.sin(i));
64
                lines[plen + ct][2] = px + (int) (radius * Math.cos(i - 2 *Math.PI / sides));
65
                lines[plen + ct][3] = py + (int) (radius * Math.sin(i - 2 * Math.PI / sides));
66
            }
67
        }
68
        
69
    }
70
    
71
    private class Listener implements ActionListener {
72
        public void actionPerformed(ActionEvent e){
73
            repaint();
74
        }
75
    }
76
    
77
    public void paintComponent(Graphics g){
78
        super.paintComponent(g);
79
        v2d[1] += G;
80
        p2d[1] += v2d[1];
81
        p2d[0] += v2d[0];
82
        
83
​
84
        
85
        if(p2d[1] > 1000){
86
            p2d[0] = 280;
87
            p2d[1] = 200;
88
            v2d[0] = 0;
89
            v2d[1] = 0;
90
            lives++;
91
        }
92
        if(p2d[0] == 280 && p2d[1] > sd){
93
            p2d[1] = sd;
94
            v2d[1] = Math.min(v2d[1], sv);
95
        }
96
        
97
        if(setlock == false){
98
            sv *= 0.95; //the dampening coefficient for the springiness
99
            sv -= (sd - snorm)/30;
100
            sd += sv;
101
        }
102
        double rc = 0.1;
103
        if(rdown){
104
            rtheta = Math.max(-0.5, rtheta - rc);
105
        }else{
106
            rtheta = Math.min(0.5, rtheta + rc);
107
        }
108
        if(ldown){
109
            ltheta = Math.max(-0.5, ltheta - rc);
110
        }else{
111
            ltheta = Math.min(0.5, ltheta + rc);
112
        }
113
        
114
        lines[0][2] = lines[0][0] + (int) (Math.cos(ltheta) * paddle);
115
        lines[0][3] = lines[0][1] + (int) (Math.sin(ltheta) * paddle);
116
        lines[1][0] = lines[1][2] + (int) (-Math.cos(rtheta) * paddle);
117
        lines[1][1] = lines[1][3] + (int) (Math.sin(rtheta) * paddle);
118
        int rX = (int) p2d[0];
119
        int rY = (int) p2d[1];
120
        int r = 10;
121
        g.setColor(Color.blue);
122
        g.drawArc(rX - r, rY - r, 2 * r, 2 * r, 0, 360);
123
        g.setColor(Color.black);
124
        for(int i = 0; i < lines.length; i++){
125
            int x1 = lines[i][0],
126
                y1 = lines[i][1],
127
                x2 = lines[i][2];
128
            double y2 = lines[i][3] + 0.0001;
129
            if(i > preset.length){
130
                g.setColor(Color.red);
131
            }
132
            g.drawLine(x1, y1, x2, (int) Math.round(y2));
133
​
134
            double bmag = Math.sqrt(v2d[0] * v2d[0] + v2d[1] * v2d[1]);
135
            double lineslope = ((double)(x2 - x1))/((double)(y2 - y1));
136
            double ballslope = v2d[0] / v2d[1];
137
            //System.out.println(ballslope + " " + lineslope);
138
            //xpos * ballslope + p2d[1] = xpos * lineslope + y1;
139
            double binter = p2d[0] - ballslope * p2d[1];
140
            double linter = x1 - lineslope * y1;
141
            
142
            double y = (binter - linter)/(lineslope - ballslope);
143
            double sx = y * ballslope + binter;
144
            //double qx = y * lineslope + linter;
145
            double la = Math.atan2(y2 - y1, x2 - x1);
146
            double ba = Math.atan2(v2d[1], v2d[0]);
147
            
148
            double da = 2 * la -  ba;
149
                    
150
            //System.out.println(sx + " " + y);
151
            /*
152
            g.drawLine((int)sx, (int)y, (int)p2d[0], (int)p2d[1]); 
153
            g.fillRect((int)sx - 2, (int)y - 2, 4, 4);
154
            g.drawLine((int)p2d[0], (int)p2d[1], (int) (p2d[0] + Math.cos(da) * 100), (int)(p2d[1] + Math.sin(da) * 100)); 
155
            //*/
156
            if(sx >= Math.min(x2, x1) && sx <= Math.max(x1, x2) && 
157
               Math.min(y1, y2) <= y && Math.max(y1, y2) >= y){
158
                double interdist = Math.sqrt(Math.pow(sx - p2d[0],2) + Math.pow(y - p2d[1],2));
159
                double tiny = 0.0001;
160
                double futuredist = Math.sqrt(Math.pow(sx - (p2d[0] + Math.cos(ba) * tiny),2) + Math.pow(y - (p2d[1] + Math.sin(ba) * tiny),2));
161
                
162
                if(interdist <=  bmag + r && futuredist < interdist){ 
163
                    //System.out.println("Carl Sagan" + i); //this is a pun because he wrote a book called Contact
164
                    if(i > preset.length){
165
                        int ball = (int) Math.floor((i - preset.length)/sides);
166
                        //System.out.println(balls[ball][2]);
167
                        points += balls[ball][3] * bmag;
168
                    }
169
                    v2d[0] = Math.cos(da) * bmag;
170
                    v2d[1] = Math.sin(da) * bmag;
171
                }
172
            }
173
        }
174
        g.setColor(Color.black);
175
        //System.out.println(sx + " " + qx);
176
        //System.out.println(ballslope + " " + lineslope);
177
        //double slope = Math.atan2(v2d[1], v2d[0]);
178
        //g.drawLine((int) p2d[0], (int) p2d[1], (int) (p2d[0]+10*v2d[0]), (int) (p2d[1]+10*v2d[1]));
179
        
180
        g.fillRect(xpos - 5, (int)sd + 10, 10, 20);
181
        
182
        g.drawString("Score: " + points + " Resets: " + lives, 10, 15);
183
        
184
    }
185
    
186
    private class Key extends KeyAdapter {
187
        public void keyPressed(KeyEvent e){
188
            if(e.getKeyCode() == KeyEvent.VK_DOWN){
189
                setlock = true;
190
                sd += 2;
191
            }
192
            if(e.getKeyCode() == KeyEvent.VK_LEFT){
193
                ldown = true;
194
            }
195
            if(e.getKeyCode() == KeyEvent.VK_RIGHT){
196
                rdown = true;
197
            }
198
        }
199
        public void keyReleased(KeyEvent e){
200
            setlock = false;
201
            if(e.getKeyCode() == KeyEvent.VK_LEFT){
202
                ldown = false;
203
            }
204
            if(e.getKeyCode() == KeyEvent.VK_RIGHT){
205
                rdown = false;
206
            }
207
        }
208
    }
209
}