鄙人做了个坦克大战的小游戏,相信很多大神们都做过,但是鄙人的炮筒就是显示不出来...求好心人指点,本人学生新手,就这点分了,全拿出来了。
1,目前共三个class文件,分别为TankClient,Tank,Missile....
2,疑问所在:我的drawLine()画不出直线。。。
3,疑问来源: case D:
System.out.println("炮筒方向开始调用--下!");
g.drawLine(x+Tank.TKLENGTH/2, y+Tank.TKWIDTH/2, x+Tank.TKLENGTH/2,y+Tank.TKWIDTH ); //就是画不出来!
System.out.println("炮筒方向调用结束--下!");
3.11简要说明:dir坦克当前方向。PTdir炮筒当前方向。fire()开火,setlocation()调整当前方向。命名做到见名知意。
4,原先的3个文件的代码:
①TankClient类:
import java.awt.*;
import java.awt.event.*;
public class TankClient extends Frame {
Tank mytank=new Tank(50,50,this);
Missile mis=null;
public void paint(Graphics g) {
mytank.drawTank(g);
if(mis!=null) mis.drawMissile(g);
}
public void launchFrame(){
this.setLocation(40, 30);
this.setSize(800, 600);
this.setVisible(true);
this.setResizable(false);
this.setBackground(Color.green);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.addKeyListener(new KeyMoniter());
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankClient tc =new TankClient();
tc.launchFrame();
}
private class PaintThread implements Runnable{
public void run() {
while(true){
repaint();
try {
Thread.sleep(35);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private class KeyMoniter extends KeyAdapter{
public void keyPressed(KeyEvent e) {
mytank.keyPressed(e);
//mis.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
mytank.keyReleased(e);
}
}
②Tank类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
public class Tank {
private int x,y;
public static final int XSPEED=5,YSPEED=5;
public static final int TKLENGTH=30,TKWIDTH=30;
private boolean BL,BR,BU,BD;
TankClient tc;
enum Direction{U,D,L,R,LU,RU,RD,LD,STOP};
private Direction dir=Direction.STOP;
Direction PTdir=Direction.D;
public Tank(int x,int y) {
this.x=x;this.y=y;
}
public Tank(int x,int y,TankClient tc){
this.x=x;this.y=y;
this.tc=tc;
}
Missile fire(){
int x=this.x+Tank.TKLENGTH/2;
int y=this.y+Tank.TKWIDTH/2;
Missile mis=new Missile(x,y,PTdir);
return mis;
}
public void drawTank(Graphics g){
Color c=g.getColor();
g.setColor(Color.red);
g.fillOval(x, y, TKLENGTH, TKWIDTH);
g.setColor(c);
//System.out.println("炮筒方向开始调用");
g.drawLine(x+Tank.TKLENGTH/2,y+Tank.TKWIDTH/2,x+Tank.TKLENGTH/2,y+Tank.TKWIDTH );
//g.setColor(Color.black);
switch(PTdir){
case U:
g.drawLine(x+Tank.TKLENGTH/2,y+Tank.TKWIDTH/2,x+Tank.TKLENGTH/2,y);
System.out.println("炮筒方向开始调用--上!");
break;
case D:
System.out.println("炮筒方向开始调用--下!");
g.setColor(Color.black);
g.drawLine(x+Tank.TKLENGTH/2, y+Tank.TKWIDTH/2, x+Tank.TKLENGTH/2,y+Tank.TKWIDTH );
System.out.println("炮筒方向调用结束--下!");
break;
case L:
g.drawLine(x+Tank.TKLENGTH/2, y+Tank.TKWIDTH/2, x,y+Tank.TKWIDTH/2 );
break;