?? game.java
字號:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Game extends Applet {
private int xpos=100,ypos=0;
private SquareShape currShape;
private Image buffer;//畫布對象
private Graphics bg;
public void init() {
buffer=this.createImage(200,400);//創建畫布大小
bg=buffer.getGraphics();
bg.fillRect(0,0,200,400);
currShape=new Ding();
currShape.setPlace(xpos,ypos);
this.addKeyListener(new GameKeyLister());
}
public void update(Graphics g)//實現不閃爍
{
g.clipRect(0,0,200,400);
paint(g);
}
public void paint(Graphics g) {//圖畫由兩部分組成,一部分為背景,另一部分為圖形
g.drawImage(buffer,0,0,null);//背景
currShape.drawShape(g);//圖形
}
class GameKeyLister extends KeyAdapter
{
public void keyReleased(KeyEvent e)
{
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_UP)
{
currShape.rotate();
currShape.setPlace(xpos,ypos);
while(currShape.getLeft()<0)
{
xpos+=20;
currShape.setPlace(xpos,ypos);
}
while(currShape.getRight()>200)
{
xpos-=20;
currShape.setPlace(xpos,ypos);
}
while(currShape.getBottom()>400)
{
ypos-=20;
currShape.setPlace(xpos,ypos);
}
repaint();
}
else if(keyCode==KeyEvent.VK_RIGHT)
{
xpos+=20;
currShape.setPlace(xpos,ypos);
if(currShape.getRight()>200)
{
xpos-=20;
currShape.setPlace(xpos,ypos);
}
repaint();
}
else if(keyCode==KeyEvent.VK_LEFT)
{
xpos-=20;
currShape.setPlace(xpos,ypos);
if(currShape.getLeft()<0)
{
xpos+=20;
currShape.setPlace(xpos,ypos);
}
repaint();
}
else if(keyCode==KeyEvent.VK_DOWN)
{
ypos+=20;
currShape.setPlace(xpos,ypos);
if(currShape.getBottom()>400)
{
ypos-=20;
currShape.setPlace(xpos,ypos);
}
repaint();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -