JAVA坦克游戏半成品

十年梦回 发布于 2013/04/10 11:01
阅读 366
收藏 1

华为云11月刊推送:DIY微信问答机器人,高性能计算代码的20个技巧!>>>

我编写了一个坦克游戏,代码如下,当控制坦克移动时,在窗口有一白条乱闪,请问各位大侠那错了,我在网上下了个没有问题。抱歉没有截图,坦克能动,当按住控制键让他动的时候有特别是长按时,不按没有,有时按也没有,感觉像图片刷新太慢了,

非常谢谢大家的帮助,我已经明白了,MyPanel类继承Jpanel即可解决问题,

package tankgame;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;
public class TankGame extends JFrame{

 MyPanel mp;
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  TankGame  tankgram=new TankGame();

 }
 public TankGame()
 {
  mp=new MyPanel();
  this.addKeyListener(mp);
  this.add(mp);
  
   this.setSize(400, 300);
  this.setVisible(true);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

}
class MyPanel extends Panel implements KeyListener
 {   Tank mytank;
     Tank enemytank;
 public MyPanel()
 {
  mytank=new Tank(100,200,0);
  enemytank=new Tank(50,20,1);
  
 }
 public void paint(Graphics g)
 {
  super.paint(g);
  g.fillRect(0, 0, 400, 300);
  this.drawTank(mytank.getX(), mytank.getY(), g, mytank.getDirect(), 0);
  this.drawTank(enemytank.getX(), enemytank.getY(), g, 1, 1);
  
 }
 
 //画坦克
public void drawTank(int x, int y, Graphics g, int direct, int type)
{

 switch(type)
 {
 case 0:
  g.setColor(Color.cyan);
  break;
 case 1:
  g.setColor(Color.yellow);
  break;
 }
 // 判断方向
 switch(direct)
 {
 case 0:   // 向上
  
    g.fill3DRect(x-15, y-15, 5, 30,false);
  g.fill3DRect(x,y-15, 5, 30,false);
   g.fill3DRect(x-10, y-10,10, 20,false);
  g.fillOval(x-10, y-5, 10, 10);
    g.drawLine(x-5, y, x-5,y-20 ); 
  break;
 case 1:
  g.fill3DRect(x-15, y-15, 5, 30,false);
    g.fill3DRect(x,y-15, 5, 30,false);
  g.fill3DRect(x-10, y-10,10, 20,false);
  g.fillOval(x-10, y-5, 10, 10);
  g.drawLine(x-5, y, x-5,y+20 ); 
  break;
 case 2:
  g.fill3DRect(x-15, y-15, 30, 5,false);
   g.fill3DRect(x-15,y, 30, 5,false);
  g.fill3DRect(x-10, y-10,20, 10,false);
  g.fillOval(x-5, y-10, 10, 10);
  g.drawLine(x, y-5, x-20,y-5 ); 
  break;
 case 3:
  g.fill3DRect(x-15, y-15, 30, 5,false);
  g.fill3DRect(x-15,y, 30, 5,false);
  g.fill3DRect(x-10, y-10,20, 10,false);
    g.fillOval(x-5, y-10, 10, 10);
  g.drawLine(x, y-5, x+20,y-5 ); 
  break;
  
 }
 
 
}

//监控键盘
public void keyTyped(KeyEvent e) {
 // TODO Auto-generated method stub
 
}
@Override
public void keyPressed(KeyEvent arg0) {
 // System.out.println("键被按下"+(char)arg0.getKeyCode() );
 if(arg0.getKeyCode()==KeyEvent.VK_UP)
 {
  //System.out.println("键被按上");
  this.mytank.setDirect(0);
  this.mytank.y-=this.mytank.speed;
 }
 if(arg0.getKeyCode()==KeyEvent.VK_DOWN)
 {
  //System.out.println("键被按");
  this.mytank.setDirect(1);
  this.mytank.y+=this.mytank.speed;
    
 }
 if(arg0.getKeyCode()==KeyEvent.VK_LEFT)
 {
  //System.out.println("键被按左");
  this.mytank.setDirect(2);
  this.mytank.x-=this.mytank.speed;
 
 }
 if(arg0.getKeyCode()==KeyEvent.VK_RIGHT)
 {
  //System.out.println("键被按右");
  this.mytank.setDirect(3);
  mytank.x+=mytank.speed;
  
 }
 //调用 repaint() 函数,来重绘制界面
 this.repaint();
 
}
@Override
public void keyReleased(KeyEvent e) {
 // TODO Auto-generated method stub
 
}

}


package tankgame;

//坦克类
  class Tank
  {
   int x;
   int y;
   int type=1;
   int speed=1;
   int direct;
   boolean islive=true;
   public Tank(int x,int y,int direct)
   {
    this.x=x;
    this.y=y;
    this.direct=direct;
   }
   public boolean isIslive() {
  return islive;
 }

 public void setIslive(boolean islive) {
  this.islive = islive;
 }

 public int getX() {
  return x;
 }

 public void setX(int x) {
  this.x = x;
 }

 public int getY() {
  return y;
 }

 public void setY(int y) {
  this.y = y;
 }

 public int getType() {
  return type;
 }

 public void setType(int type) {
  this.type = type;
 }

 public int getSpeed() {
  return speed;
 }

 public void setSpeed(int speed) {
  this.speed = speed;
 }

 

 public int getDirect() {
  return direct;
 }

 public void setDirect(int direct) {
  this.direct = direct;
 }
  
  }
加载中
0
红薯
红薯
能跑?有截图吗?
0
Risol
Risol
上截图
Ryan-瑞恩
Ryan-瑞恩
这个头像,,,,换的速度挺快的嘛。
0
liuleidefeng
liuleidefeng
大概看了一下你的代码,感觉你说的" 在窗口有一白条乱闪",是因为你的重绘事件造成,用双缓冲法绘图吧。
返回顶部
顶部