该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
第一次写java的小游戏
想实现点击右下角的暂停 实现游戏的暂停和继续 不知道该怎么加,自己找了线程的内容但试了还是成功不了,求大佬们帮一下小白。代码有点乱,大佬们见谅。,,
TypingGame文件:
import javax.swing.JFrame;public class TypingGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
TypingGamePanel panel = new TypingGamePanel();
frame.add(panel);
Thread t = new Thread(panel);
t.start();
panel.addKeyListener(panel);
panel.setFocusable(true);
panel.addMouseListener(panel);
frame.setSize(1500, 900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);
}}
TypingGamePanel文件:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.JPanel;public class TypingGamePanel extends JPanel implements Runnable,KeyListener,MouseListener{
int[] xx = new int[10];
int[] yy = new int[10];
char[] words = new char[10];
int score = 0;
int speed = 10;
BufferedImage backImage;
public TypingGamePanel(){
try {
backImage = ImageIO.read(TypingGamePanel.class.getResource("car.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<10;i++){
boolean equal;
xx[i] = (int)(100+Math.random()*1400);
yy[i] = -(int)(Math.random()*900);
words[i] = judge(i);
}
}
public char judge(int num){
char random = (char)(Math.random()*26+'A');
for(int i=0;i
if(words[i] == random)
judge(num);
}
return random;
}
public void paint(Graphics g){
super.paint(g);
g.drawImage(backImage,0,0,1500,900,null);
Font ft = new Font("宋体",Font.BOLD,25);
g.setFont(ft);
for(int i = 0; i < 10; i++){
g.setColor(Color.RED);
g.drawString(words[i]+"", xx[i], yy[i]);
g.setColor(Color.BLUE);
g.drawString("当前分数:",10,20);
g.drawString(score+"",140,20);
g.setColor(Color.orange);
g.fillRect(50, 50, 60, 30);
g.setColor(Color.yellow);
g.fillRect(50, 90, 60, 30);
g.setColor(Color.green);
g.fillRect(50, 130, 60, 30);
g.setColor(Color.white);
g.drawString("高速", 50, 70);
g.drawString("中速", 50, 110);
g.drawString("低速", 50, 150);
g.fillRect(1400,800,80,40);
g.setColor(Color.black);
g.drawString("暂停",1420,830);
}
}
public void run() {
// TODO Auto-generated method stub
while(true){
for(int i = 0;i < 10; i++){
yy[i]++;
if(yy[i]>900) {
yy[i] = 0;
score -= 10;
}
}
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int mouseX = e.getX();
int mouseY = e.getY();
if(mouseX > 50 && mouseX < 110 && mouseY > 50 && mouseY < 80)
speed = 5;
if(mouseX > 50 && mouseX < 110 && mouseY > 90 && mouseY < 120)
speed = 10;
if(mouseX > 50 && mouseX < 110 && mouseY > 130 && mouseY < 160)
speed = 20;
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
boolean tof = true;
for(int i=0; i<10;i++){
if(e.getKeyCode()==words[i]){
xx[i] = (int)(100+Math.random()*1400);
yy[i] = 0;
boolean equal;
char randomchar;
do
{
randomchar = (char)(Math.random()*26+'A');
equal = false;
for(int j=0;j<10;j++){
if(randomchar == words[j]){
equal = true;
break;
}
}
}while(equal);
words[i] = randomchar;
score += 10;
tof = false;
break;
}
}
if(tof) score -= 10;
} @Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
} @Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
} @Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
} @Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
} @Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
} @Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}}