test-lexcial

来自「Java语言词法分析器的设计与实现 其中具体要求: 1.使用DFA实现词法分」· 代码 · 共 104 行

TXT
104
字号
a.b
import java.awt.* ; 
import javax.swing.* ; 
import java.awt.event.*; 
import java.util.EventListener; 

public class HsTank2 extends JFrame { 
HsTank2(String title) { 
this.setTitle(title) ; 
this.setSize(608 , 630) ; 
this.setLocation(300 , 100) ; 
this.setBackground(Color.WHITE) ; 

MyTank mp = new MyTank() ; 
this.add(mp) ; 

this.addKeyListener(mp) ; 

new Thread(mp).start() ; 
} 
public static void main(String[] args) { 

HsTank2 h = new HsTank2("坦克大战(版本1.0)") ; 

h.setVisible(true) ; 
} 
} 

//主战坦克 
class MyTank extends JPanel implements KeyListener , Runnable { 

int x = 280, y = 280 ;//坦克的初始位置 
int op = 1 ;//坦克的移动方向 
int color = 0 ; 
int tankspeed = 8 ;//坦克的速度 
int tankbullet = 8 ;//坦克的子弹速度 
int tankfbullet = 4 ;//敌军的子弹速度 
int shengming = 100 ;//生命 
int fenshu = 0 ; 
int nandu = 5 ; //设置游戏难度 

//子弹 
int dx = 295 , dy = 295 ; 
int dx1 = 295 , dy1 = -10 ;  
int dx2 = 600 , dy2 = 295 ;  
int dx3 = 295 , dy3 = 600 ;  
int dx4 = -10 , dy4 = 295 ;   

//敌军坦克 
int num = 10 ;//敌军坦克数量,不能修改 
int[] xf = new int[num] ; 
int[] yf = new int[num] ; 
int[] opf = new int[num] ;  

int[] dxf = new int[num] ; 
int[] dyf = new int[num] ; 

int[] dxf1 = new int[num] ; 
int[] dyf1 = new int[num] ; 
int[] dxf2 = new int[num] ; 
int[] dyf2 = new int[num] ; 
int[] dxf3 = new int[num] ; 
int[] dyf3 = new int[num] ; 
int[] dxf4 = new int[num] ; 
int[] dyf4 = new int[num] ; 

//构造函数,初始化敌军坦克的位置和状态 
MyTank() { 
for (int i = 0; i<num; i++) { 
xf[i] = (int) (Math.random() * 560) ; 
yf[i] = (int) (Math.random() * 560) ; 
dxf[i] = xf[i] + 15 ;  
dyf[i] = yf[i] + 15 ; 
} 

for (int i = 0; i<num; i++) { 
dxf1[i] = 295 ; dyf1[i] = -10 ;  
dxf2[i] = 600 ; dyf2[i] = 295 ;  
dxf3[i] = 295 ; dyf3[i] = 600 ;  
dxf4[i] = -10 ; dyf4[i] = 295 ;  
} 
} 


if(op == 1) { 
g.setColor(Color.red) ; 
g.fillRect(x , y , 40 , 40) ; 

switch (color % 6) { 
     case 0: g.setColor(Color.blue) ; break; 
     case 1: g.setColor(Color.yellow) ; break; 
     case 2: g.setColor(Color.red) ; break; 
     case 3: g.setColor(Color.orange) ; break; 
     case 4: g.setColor(Color.green) ; break; 
     case 5: g.setColor(Color.black) ; break; 
    } 
g.fillOval(x - 5 , y - 5 , 10 , 10) ; 
g.fillOval(x - 5 , y + 5 , 10 , 10) ; 
g.fillOval(x - 5 , y + 15 , 10 , 10) ; 
g.fillOval(x - 5 , y + 25 , 10 , 10) ; 
g.fillOval(x - 5 , y + 35 , 10 , 10) ; 

g.fillOval(x + 35 , y - 5 , 10 , 10) ; 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?