坦克游戏教程二:实现坦克移动以及方向控制

今天实现了可以通过键盘的游戏键控制坦克的移动,以及可以控制其方向位置改变。同时可以绘制多辆敌方坦克。

代码如下:

/*
 * Function: TankGame 1.0
 * Draw Tank
*/
package com.test1;

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

public class MyTankGame1 extends JFrame {
	
	MyPanel mp = null;
	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		MyTankGame1 myTankGame1 = new MyTankGame1();

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

}

//My Planel
class MyPanel extends JPanel implements KeyListener{
	//Define a Tank
	Hero  hero = null;
	
	// define enemy tank group
	Vector<EnemyTank> ets = new Vector<EnemyTank>();
	
	int ensize = 3;
	
	
	public MyPanel(){
		hero = new Hero(100, 100);
		
		for(int i = 0; i < ensize; i++){
			EnemyTank et = new EnemyTank(i * 50,  0);
			et.setColor(0);
			et.setDirect(2);
			ets.add(et);
		}
	}
	
	public void paint(Graphics g){
		super.paint(g);
		// Draw my tank. And then encapsulate them into functions.
		g.fillRect(0, 0, 400, 300);
		this.drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 0);
		
		for(int i = 0; i <ets.size(); i++){
			this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirect(), 1);
		}
	}
	
	public void drawTank(int x, int y, Graphics g, int direct, int type){
		
		// judge type
		switch(type){
		case 0:
			g.setColor(Color.cyan);
			break;
		case 1:
			g.setColor(Color.YELLOW);
			break;
		}
		
		// judge direction
		switch(direct){
		case 0:
			g.fill3DRect(x, y, 5, 30, false);
			// Draw right rect
			g.fill3DRect(x + 15,  y, 5, 30, false);
			// Draw middle rect
			g.fill3DRect(x + 5, y + 5, 10, 20, false);
			// Draw circle
			g.fillOval(x + 5, y + 10, 10, 10);
			// Draw line
			g.drawLine(x + 10, y + 15, x + 10, y);
			break;
		
		case 1:
			g.fill3DRect(x, y, 30, 5, false);
			// Draw right rect
			g.fill3DRect(x,  y + 15, 30, 5, false);
			// Draw middle rect
			g.fill3DRect(x + 5, y + 5, 20, 10, false);
			// Draw circle
			g.fillOval(x + 10, y + 5, 10, 10);
			// Draw line
			g.drawLine(x + 15, y + 10, x + 30, y + 10);
			
			break;
		case 2:
			g.fill3DRect(x, y, 5, 30, false);
			// Draw right rect
			g.fill3DRect(x + 15,  y, 5, 30, false);
			// Draw middle rect
			g.fill3DRect(x + 5, y + 5, 10, 20, false);
			// Draw circle
			g.fillOval(x + 5, y + 10, 10, 10);
			// Draw line
			g.drawLine(x + 10, y + 15, x + 10, y + 30);
			break;
		case 3:
			g.fill3DRect(x, y, 30, 5, false);
			// Draw right rect
			g.fill3DRect(x,  y + 15, 30, 5, false);
			// Draw middle rect
			g.fill3DRect(x + 5, y + 5, 20, 10, false);
			// Draw circle
			g.fillOval(x + 10, y + 5, 10, 10);
			// Draw line
			g.drawLine(x + 15, y + 10, x, y + 10);
			
			break;
			
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// 对键按下进行处理  a s w d left down up right
		if(e.getKeyCode() == KeyEvent.VK_W){
			this.hero.setDirect(0);
			this.hero.moveUp();
		}
		
		if(e.getKeyCode() == KeyEvent.VK_D){
			this.hero.setDirect(1);
			this.hero.moveRight();
		}
		
		if(e.getKeyCode() == KeyEvent.VK_S){
			this.hero.setDirect(2);
			this.hero.moveDown();
		}
		
		if(e.getKeyCode() == KeyEvent.VK_A){
			this.hero.setDirect(3);
			this.hero.moveLeft();
		}
		
		this.repaint();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO 自动生成的方法存根
		
	}
}

package com.test1;

public class Members {

}


//Tank Class
class Tank{
	
	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;
	}

	int x = 0;
	int y = 0;
	public int getDirect() {
		return direct;
	}

	public void setDirect(int direct) {
		this.direct = direct;
	}

	int direct = 0;
	int speed = 2;
	int color;
	
	// 0:up 
	// 1:down
	// 2:right
	// 3:left
	
	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public int getSpeed() {
		return speed;
	}

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

	public Tank(int x, int y){
		this.x = x ;
		this.y = y ;
	}
}

class EnemyTank extends Tank {

	public EnemyTank(int x, int y) {
		super(x, y);
		// TODO 自动生成的构造函数存根
	}
	
}

class Hero extends Tank{
	public Hero(int x, int y){
		super(x, y);
	}
	
	//tank move to direction up
	public void moveUp(){
		this.y -= speed;
	}
	public void moveDown(){
		this.y += speed;
	}
	
	public void moveLeft(){
		this.x -= speed;
	}
	
	public void moveRight(){
		this.x += speed;
	}
}

运行效果图:


  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值