JAvaでの簡単な指法練習ゲーム(32ビットでのみ正常に動作)
3153 ワード
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.print.*;
import javax.swing.*;
public class typewrite {
public static void main(String[] args) {
JFrame w = new JFrame("typewrite game");
w.setSize(300, 400);
w.setFont(new Font("",10,100000));
w.setBackground(Color.RED);
MyPanel mp = new MyPanel();
w.add(mp);
w.addKeyListener(mp);
mp.addKeyListener(mp);
Thread t = new Thread(mp);
t.start();
w.setVisible(true);
}
}
class MyPanel extends JPanel implements Runnable, KeyListener ,MouseListener{
private static final long serialVersionUID = 1L;
int x[] = new int[10];
int y[] = new int[10];
char c[] = new char[10];
int sum = 1000;
public MyPanel() {
for (int i = 0; i < 10; i++) {
x[i] = (int) (Math.random() * 260);
y[i] = (int) (Math.random() * 300);
c[i] = (char) (Math.random() * 26 + 97);
}
}
public void paint(Graphics g) {
g.clearRect(0, 0, 300, 400);
g.setColor(Color.BLACK);
for (int i = 0; i < 10; i++) {
g.drawString("" + c[i], x[i], y[i]);
}
g.setColor(Color.GREEN);
g.drawString("score: " + sum, 20, 10);
if (sum < 0) {
g.clearRect(0, 0, 300, 400);
g.drawString("GAME OVER", 100, 200);
}
}
public void run() {
while (true) {
for (int i = 0; i < 10; i++) {
y[i]++;
if (y[i] > 400) {
y[i] = 0;
x[i] = (int) (Math.random() * 260);
c[i] = (char) (Math.random() * 27 + 97);
sum -= 100;
}
}
if (sum < 0)
return;
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
@Override
public void keyPressed(KeyEvent e) {
char cc = e.getKeyChar();
int yy = -1;
int index = -1;
for (int i = 0; i < 10; i++) {
if (cc == c[i]) {
if (y[i] > yy) {
yy = y[i];
index = i;
}
}
}
if(index>-1){
y[index]=0;
x[index]=(int)(Math.random()*260);
c[index]=(char)(Math.random()*26+97);
sum+=10;
}else{
sum-=100;
}
}
@Override
public void keyReleased(KeyEvent e) { }
@Override
public void keyTyped(KeyEvent e) { }
@Override
public void mouseClicked(MouseEvent arg0) { }
@Override
public void mouseEntered(MouseEvent arg0) { }
@Override
public void mouseExited(MouseEvent arg0) { }
@Override
public void mousePressed(MouseEvent arg0) { }
@Override
public void mouseReleased(MouseEvent arg0) { }
}