📄 snake.java
字号:
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Snake extends Frame{
public static final int GameWidth = 800;
public static final int GameHeight = 610;
private Image offScreenImage = null;
heads h = new heads(40,50,Color.BLUE,Direction.STOP,this) ;
Yishen y = null;
Blood b = null;
Blood b1 = null;
explode e = null;
public static void main(String[] args) {
new Snake().lungame();
}
public void lungame(){
y = new Yishen(Color.CYAN,Color.ORANGE,h);
b = new Blood(300,50,this);
b1 = new Blood(300-20,500-20,this);
this.setBounds(100, 100, GameWidth, GameHeight);
this.setBackground(Color.GREEN);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.addKeyListener(new KeyMonitor());
this.setResizable(false);
this.setVisible(true);
new Thread(new painthread()).start();
}
public void paint(Graphics g){
g.drawString("Score:"+y.blocks.size()*10, 60, 60);
if(y.isPasue()){
g.drawString("按F2键继续", 100, 100);
}
if(!h.isLive()){
h.e.draw(g);
g.drawString("按F2键重新开始", 100, 100);
}
h.Draw(g);
y.draw(g);
b.Draw(g);
b1.Draw(g);
if(h.EatBlood(b)){
y.add();
b.chang();
}
if(h.EatBlood(b1)){
y.add();
b1.chang();
}
}
public void update(Graphics g){
if(offScreenImage == null){
offScreenImage = this.createImage(GameWidth,GameHeight);
};
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, GameWidth, GameHeight);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
public class painthread implements Runnable{
public void run() {
while(true){
repaint();
try {
Thread.sleep(150);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
h.keyPressed(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -