-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPinBall2.0
196 lines (173 loc) · 5.09 KB
/
PinBall2.0
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
import java.awt.Frame;
import java.util.Random;
import javax.swing.Timer;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class PinBall {
private final int TABLE_WIDTH = 300;
private final int TABLE_HEIGHT = 400;
private final int RACKET_Y = 340;
private final int RACKET_HEIGHT = 20;
private final int RACKET_WIDTH = 60;
private final int BALL_SIZE = 16;
private Frame f = new Frame("弹球游戏");
Random rand = new Random();
private int ySpeed = 12;
private double xyRate = rand.nextDouble() - 0.5;
private int xSpeed = (int)(ySpeed * xyRate * 2);
private int ballX = rand.nextInt(200) + 20;
private int ballY = rand.nextInt(10) + 20;
private int racketX = 150;
private MyPanel tableArea = new MyPanel();
Timer timer;
int score = 0;
private boolean isLose = false;
private boolean isFirst = true;
public void init() {
tableArea.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));
f.add(tableArea);
TextArea TA = new TextArea(0,10);
/*TA.append("按空格开始/暂停,方向键左右移动滑块");
tableArea.add(TA);*/
KeyAdapter keyProcessor = new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
if(ke.getKeyCode() == KeyEvent.VK_LEFT) {
if(racketX > 0)
racketX -= 10;
}
if(ke.getKeyCode() == KeyEvent.VK_RIGHT) {
if(racketX < TABLE_WIDTH - RACKET_WIDTH)
racketX += 10;
}
if(ke.getKeyCode() == KeyEvent.VK_SPACE) {
if(timer.isRunning())
timer.stop();
else
timer.start();
}
/*if(ke.getKeyCode() == KeyEvent.VK_TAB) {
timer.restart();
}
if(ke.getKeyCode() == KeyEvent.VK_PLUS) {
ySpeed++;
}
if(ke.getKeyCode() == KeyEvent.VK_MINUS) {
ySpeed--;
}*/
}
};
f.addKeyListener(keyProcessor);
f.addWindowListener(new MyWindowListener());
tableArea.addKeyListener(keyProcessor);
ActionListener taskPerformer = evt ->{
if(ballX <= 0 || ballX >= TABLE_WIDTH - BALL_SIZE) {
xSpeed = -xSpeed;
}
if(ballY >= RACKET_Y - BALL_SIZE && (ballX < racketX |ballX > racketX + RACKET_WIDTH)) {
timer.stop();
isLose = true;
tableArea.repaint();
}
else if(ballY <= 0 || (ballY >= RACKET_Y - BALL_SIZE && ballX > racketX && ballX <= racketX + RACKET_WIDTH)) {
ySpeed = -ySpeed;
score++;
}
ballX += xSpeed;
ballY += ySpeed;
tableArea.repaint();
};
timer = new Timer(100,taskPerformer);
//timer.start();
f.pack();
f.setVisible(true);
}
class MyWindowListener implements WindowListener{
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}
public static void main(String[] args) {
new PinBall().init();
}
class MyPanel extends Panel{
public void paint(Graphics g) {
//g.drawString("按空格开始/暂停,方向键左右移动滑块", 0, 10);
if(isFirst) {
g.setFont(new Font("Times",Font.BOLD,20));
g.drawString("按空格开始/暂停", 20, 100);
g.drawString("方向键左右移动滑块", 0, 200);
g.drawString("准备好了吗?按空格开始!", 0, 300);
isFirst = false;
}
else {
if(isLose) {
g.setColor(new Color(255,0,0));
g.setFont(new Font("Times",Font.BOLD,20));
g.drawString("游戏已结束!", 50, 100);
g.drawString("得分:"+ score, 50, 150);
//g.drawString("速度:"+ ySpeed, 50, 170);
//g.drawString("按空格开始/暂停", 20, 200);
//g.drawString("方向键左右移动滑块", 0, 300);
}
else {
g.setColor(new Color(240,240,80));
g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);
g.setColor(new Color(80,80,200));
g.fillRect(racketX, RACKET_Y, RACKET_WIDTH, RACKET_HEIGHT);
}
}
}
}
class MyCanvas extends Canvas {
public void paint(Graphics g) {
g.drawString("按空格开始/暂停,方向键左右移动滑块", 0, 10);
if(isLose) {
g.setColor(new Color(255,0,0));
g.setFont(new Font("Times",Font.BOLD,30));
g.drawString("游戏已结束!", 50, 200);
}
else {
g.setColor(new Color(240,240,80));
g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);
g.setColor(new Color(80,80,200));
g.fillRect(racketX, RACKET_Y, RACKET_WIDTH, RACKET_HEIGHT);
}
}
}
}