你的位置:首页 > Java教程

[Java教程]java编写一个可以上、下、左、右移动的坦克


唉,本人学习进度缓慢,但依然会坚持不懈!有感兴趣的朋友可以在下面留言。

源代码献上:

/*
* 画出我的坦克,使他可以上下左右移动
*/
package com.test4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class MytankGame2 extends JFrame{
MyPanel mp = null;
public static void main(String[] args){
MytankGame2 mtg = new MytankGame2();
}
//构造函数
public MytankGame2(){
mp = new MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(270, 500);
this.setLocation(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

class MyPanel extends JPanel implements KeyListener{
Mytank mt = null;
public MyPanel(){
mt = new Mytank(10,10);
}
//重新paint函数
public void paint(Graphics g){
super.paint(g);
g.fillRect(0, 0, 300, 400);

this.drawTank(mt.getX(), mt.getY(), 0, this.mt.direct, g);
}
//画出tank函数
public void drawTank(int x,int y,int type,int direct,Graphics g){
switch(type){
case 0:
g.setColor(Color.orange);
break;
case 1:
g.setColor(Color.blue);
break;
}
switch(direct){
case 0://上
g.fillRect(x, y, 5, 30);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x+25, y, 5, 30);
g.setColor(Color.red);
g.drawLine(x+15, y-5, x+15, y+5);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
case 1://下
g.fillRect(x, y, 5, 30);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x+25, y, 5, 30);
g.setColor(Color.red);
g.drawLine(x+15, y+15, x+15, y+35);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
case 3://左
g.fillRect(x, y, 30, 5);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x, y+25, 30, 5);
g.setColor(Color.red);
g.drawLine(x+15, y+15, x+35, y+15);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
case 2://右
g.fillRect(x, y, 30, 5);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x, y+25, 30, 5);
g.setColor(Color.red);
g.drawLine(x-5, y+15, x+15, y+15);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
}
}

public void keyTyped(KeyEvent e) {


}
//对键按下做处理
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W){
//设置我的坦克
this.mt.setDirect(0);
this.mt.moveup();
}else if(e.getKeyCode() == KeyEvent.VK_A){
this.mt.setDirect(2);
this.mt.moveleft();
}else if(e.getKeyCode() == KeyEvent.VK_D){
this.mt.setDirect(3);
this.mt.moveright();
}else if(e.getKeyCode() == KeyEvent.VK_S){
this.mt.setDirect(1);
this.mt.movedown();
}
this.repaint();
}

public void keyReleased(KeyEvent e) {


}
}

/*
* 画出我的坦克,使他可以上下左右移动
*/
package com.test4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class MytankGame2 extends JFrame{
MyPanel mp = null;
public static void main(String[] args){
MytankGame2 mtg = new MytankGame2();
}
//构造函数
public MytankGame2(){
mp = new MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(270, 500);
this.setLocation(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

class MyPanel extends JPanel implements KeyListener{
Mytank mt = null;
public MyPanel(){
mt = new Mytank(10,10);
}
//重新paint函数
public void paint(Graphics g){
super.paint(g);
g.fillRect(0, 0, 300, 400);

this.drawTank(mt.getX(), mt.getY(), 0, this.mt.direct, g);
}
//画出tank函数
public void drawTank(int x,int y,int type,int direct,Graphics g){
switch(type){
case 0:
g.setColor(Color.orange);
break;
case 1:
g.setColor(Color.blue);
break;
}
switch(direct){
case 0://上
g.fillRect(x, y, 5, 30);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x+25, y, 5, 30);
g.setColor(Color.red);
g.drawLine(x+15, y-5, x+15, y+5);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
case 1://下
g.fillRect(x, y, 5, 30);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x+25, y, 5, 30);
g.setColor(Color.red);
g.drawLine(x+15, y+15, x+15, y+35);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
case 3://左
g.fillRect(x, y, 30, 5);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x, y+25, 30, 5);
g.setColor(Color.red);
g.drawLine(x+15, y+15, x+35, y+15);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
case 2://右
g.fillRect(x, y, 30, 5);
g.fillRect(x+5, y+5, 20, 20);
g.fillRect(x, y+25, 30, 5);
g.setColor(Color.red);
g.drawLine(x-5, y+15, x+15, y+15);
g.setColor(Color.green);
g.fillOval(x+7, y+7, 16, 16);
break;
}
}

public void keyTyped(KeyEvent e) {


}
//对键按下做处理
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W){
//设置我的坦克
this.mt.setDirect(0);
this.mt.moveup();
}else if(e.getKeyCode() == KeyEvent.VK_A){
this.mt.setDirect(2);
this.mt.moveleft();
}else if(e.getKeyCode() == KeyEvent.VK_D){
this.mt.setDirect(3);
this.mt.moveright();
}else if(e.getKeyCode() == KeyEvent.VK_S){
this.mt.setDirect(1);
this.mt.movedown();
}
this.repaint();
}

public void keyReleased(KeyEvent e) {


}
}