DefaultFrame .java 程序
import java.awt.*;
import lor.*;
import java.awt.event.*;
public class DefaultFrame extends Frame {
public static final int GAME_WIDTH = 600;
public static final int GAME_HEIGHT = 400;
public static void main(String[] args) {
DefaultFrame defaultFrame1 = new DefaultFrame();
defaultFrame1.lauchFrame();
}
private void lauchFrame() {
this.setBounds(150, 150, GAME_WIDTH, GAME_HEIGHT);
this.setBackground(Color.DARK_GRAY);
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
it(0);
}
});
KeyMonitor km = new KeyMonitor(this);
this.addKeyListener(km);
}
class KeyMonitor extends KeyAdapter {
private DefaultFrame defaultFrame;
public KeyMonitor (DefaultFrame d) {
defaultFrame = d;
}
public void keyPressed(KeyEvent e) {
KeyCode() == KeyEvent.VK_SPACE) {
defaultFrame.setVisible(false);
new GameFrame().launchFrame();
}
}
}
public void paint(Graphics g) {
g.setFont(new Font("a",Font.BOLD,20));
g.setColor(Color.blue);
g.drawString("打字小游戏", 250, 100);
g.drawString("看看30秒时间你能打多少个字母", 150, 200);
g.drawString("按空格键开始游戏", 200, 300);
}
}
GameFrame.java程序
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
public class GameFrame extends Frame {
public static final int GAME_WIDTH = 600;
public static final int GAME_HEIGHT = 400;
public static int GAME_LEVEL = 2;
public static int charNum = 0; //记录总字符数
public static int hitNum = 0; //记录打中的字符数
List myChar = new ArrayList();
private boolean timeUp = false;
private Image offScreenImage = null;
public void launchFrame() {
this.setBounds(150, 150, GAME_WIDTH, GAME_HEIGHT);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
it(0);
}
});
this.setVisible(true);
KeyMonitor keyMonitor = new KeyMonitor();
this.addKeyListener(keyMonitor);
Thread t = new Thread(new CreaterChar());
t.start();
new Thread(new CountTime()).start();
new Thread(new Repaint()).start();
}
class KeyMonitor extends KeyAdapter {
免费游戏代码大全 public void keyPressed(KeyEvent e) {
for(int i = 0; i < myChar.size();i++) {
MyChar aChar = ((i);
aChar.keyPressed(e);
}
}
}
public void paint(Graphics g) {
g.d);
g.setFont(new Font("a",Font.BOLD,10));
// g.drawString("屏幕中字符数" + myChar.size(), 500, 40);
g.drawString("打中的字符数"+ hitNum, 450, 60);
for (int i = 0; i < myChar.size(); i++) {
MyChar aChar = ((i);
if(aChar.isLive())
aChar.draw(g);
else {
(i));
}
}
if(timeUp == true) {
g.setColor(Color.blue);
g.drawString("Time Up", 200, 150);
g.drawString("打中的字符数"+ hitNum,200,200);
g.drawString("按F1重新开始",200,250);
}
}
// 双缓冲技术
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = ateImage(GAME_WIDTH,GAME_HEIGHT);
}
Graphics gOffScreen = Graphics();
Color c = Color();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
paint(gOffScreen);
gOffScreen.setColor(c);
g.drawImage(offScreenImage, 0, 0, null);
}
class CreaterChar implements Runnable {
public void run() {
int sleep = 0;
switch (GAME_LEVEL) {
case 2:
sleep = 1000;
}
while (!timeUp) {
try {
MyChar newchar = new MyChar();
MyChar newchar1 = new MyChar();
MyChar newchar2 = new MyChar();
myChar.add(newchar);
myChar.add(newchar1);
myChar.add(newchar2);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论