前期文章:
【Java_项目篇<1>】--JAVA实现坦克大战游戏--画出坦克(一)
控制小球移动
1.外部类 实现KeyListener监听接口写法
package com.test3;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Demo9_4 extends JFrame{
MyPanel p1 = new MyPanel();
public Demo9_4(){
add(p1);
addKeyListener(p1);//写法
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Demo9_4();
}
}
@SuppressWarnings("serial")
class MyPanel extends JPanel implements KeyListener{//必须实现keylistener中的所有方法
int x = 5;
int y = 5;
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.fillOval(this.x, this.y, 10, 10);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_DOWN){
y+=5;
repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
2.匿名内部类写法
package com.test3;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Demo9_4 extends JFrame{
MyPanel p1 = new MyPanel();
public Demo9_4(){
add(p1);
/**
* 匿名内部类
* 不能是p1.addKeyListener(new keyListner)
* 如果是监听按钮就可以这样写
* p1是面板,所以不能这样写? p1面板没有对应的出发时间类型? 不对啊 JPanel是Component的子类,可以触发事件类型啊?
*/
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_DOWN){
p1.setY(p1.getY()+5);
repaint();
}
}
});
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Demo9_4();
}
}
@SuppressWarnings("serial")
class MyPanel extends JPanel {
int x = 5;
int y = 5;
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;
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.fillOval(this.x, this.y, 10, 10);
}
}
好,现在进入正式的程序编写
坦克2.0
1.画出坦克,颜色可控
2.实现坦克移动 并且速度可控
MyTankGame2.Java
members.java
实现效果
显然:坦克的方向并没有改变,他只是僵硬的移动
所以我们需要用画笔画出坦克不同方向的样子
更新 第96-130行
67行的倒数第二个参数,需要改成灵活的方式 用鼠标可以控制方向
drawTank(hero.getX(),hero.getY(),g,hero.Direct,1);
另外 我们需要添加敌方坦克
//敌方坦克
class EnemyTank extends Tank{
public EnemyTank(int x, int y) {
super(x, y);
}
}
显然:坦克的方向并没有改变,他只是僵硬的移动
所以我们需要用画笔画出坦克不同方向的样子
更新 第82-116行
55行的倒数第二个参数,需要改成灵活的方式 用鼠标可以控制方向
drawTank(hero.getX(),hero.getY(),g,hero.Direct,1);
/**
* 功能:坦克游戏的2.0
* 1.画出坦克
* 2.坦克的移动
*/
package Tank_02;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MyTankGame2 extends JFrame{
MyPanel p1 = new MyPanel();
public MyTankGame2(){
add(p1);
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(p1);//注册监听
}
public static void main(String[] args) {
new MyTankGame2();
}
}
//画图
@SuppressWarnings("serial")
class MyPanel extends JPanel implements KeyListener{
Hero hero = null;
public MyPanel(){
hero = new Hero(50,10);//设置坦克出现的位置(10,10)
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.fillRect(0, 0, 400, 300);//背景填充
drawTank(hero.getX(),hero.getY(),g,hero.Direct,1);//一定要传入画笔g
g.setColor(Color.yellow);
}
/*
* drawTank (坦克坐标x,y,画笔g,方向,坦克类型)
* 方法介绍:
* 可以设置-->坦克的颜色(类型:敌方坦克,我方坦克),方向,出现的坐标
*
* 如果type是default 则默认颜色为画出黑色坦克
*
* 封装性:将坦克封装到方法中。
*
*/
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);
default:
break;
}
switch (direct) {
case 0:
//向上
g.fill3DRect(x , y , 5 , 30, false);
g.fill3DRect(x+15, y , 5 , 30, false);
g.fill3DRect(x+5 , y+5 , 10, 20, false);
g.fillOval(x+4, y+10, 10 , 10);
g.drawLine(x+9, y+15, x+9, y );
break;
case 1:
//向下w
g.fill3DRect(x , y , 5 , 30, false);
g.fill3DRect(x+15, y , 5 , 30, false);
g.fill3DRect(x+5 , y+5 , 10, 20, false);
g.fillOval(x+4, y+10, 10 , 10);
g.drawLine(x+9, y+15, x+9, y+30 );
break;
case 2:
//向左
g.fill3DRect(x , y , 30, 5 , false);
g.fill3DRect(x , y+15 , 30, 5 , false);
g.fill3DRect(x+5 , y+5 , 20, 10, false);
g.fillOval(x+9 , y+4, 10 , 10 );
g.drawLine(x+5, y+9, x-4, y+9);
break;
case 3:
//向右
g.fill3DRect(x , y , 30, 5 , false);
g.fill3DRect(x , y+15 , 30, 5 , false);
g.fill3DRect(x+5 , y+5 , 20, 10, false);
g.fillOval(x+9 , y+4, 10 , 10 );
g.drawLine(x+15, y+9, x+30, y+9);
break;
default:
break;
}
//repaint(); 因为监听器里面有repaint() 所以不用在加repaint()了
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {//实现接口 根据按键上下左右移动 可以控制速度和移动
if(e.getKeyCode()==KeyEvent.VK_W){
hero.setDirect(0);
hero.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_S){
hero.setDirect(1);
hero.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_A){
hero.setDirect(2);
hero.moveLeft();
}else if(e.getKeyCode()==KeyEvent.VK_D){
hero.setDirect(3);
hero.moveRight();
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}
最终的代码
MyTankGame2.java
/**
* 功能:坦克游戏的2.0
* 1.画出坦克
* 2.坦克的移动
*/
package Tank_02;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MyTankGame2 extends JFrame{
MyPanel p1 = new MyPanel();
public MyTankGame2(){
add(p1);
setSize(1200, 800);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(p1);//注册监听
}
public static void main(String[] args) {
new MyTankGame2();
}
}
//画图
@SuppressWarnings("serial")
class MyPanel extends JPanel implements KeyListener{
Hero hero = null;
Vector ets = new Vector();
int enSize = 20;
public MyPanel(){
hero = new Hero(50,10);//设置坦克出现的位置(10,10)
// 初始化敌人的坦克
for(int i=0;i坦克的颜色(类型:敌方坦克,我方坦克),方向,出现的坐标
*
* 如果type是default 则默认颜色为画出黑色坦克
*
* 封装性:将坦克封装到方法中。
*
*/
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);
default:
break;
}
switch (direct) {
case 0:
//向上
g.fill3DRect(x , y , 5 , 30, false);
g.fill3DRect(x+15, y , 5 , 30, false);
g.fill3DRect(x+5 , y+5 , 10, 20, false);
g.fillOval(x+4, y+10, 10 , 10);
g.drawLine(x+9, y+15, x+9, y );
break;
case 1:
//向下w
g.fill3DRect(x , y , 5 , 30, false);
g.fill3DRect(x+15, y , 5 , 30, false);
g.fill3DRect(x+5 , y+5 , 10, 20, false);
g.fillOval(x+4, y+10, 10 , 10);
g.drawLine(x+9, y+15, x+9, y+30 );
break;
case 2:
//向左
g.fill3DRect(x , y , 30, 5 , false);
g.fill3DRect(x , y+15 , 30, 5 , false);
g.fill3DRect(x+5 , y+5 , 20, 10, false);
g.fillOval(x+9 , y+4, 10 , 10 );
g.drawLine(x+5, y+9, x-4, y+9);
break;
case 3:
//向右
g.fill3DRect(x , y , 30, 5 , false);
g.fill3DRect(x , y+15 , 30, 5 , false);
g.fill3DRect(x+5 , y+5 , 20, 10, false);
g.fillOval(x+9 , y+4, 10 , 10 );
g.drawLine(x+15, y+9, x+30, y+9);
break;
default:
break;
}
//repaint(); 因为监听器里面有repaint() 所以不用在加repaint()了
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {//实现接口 根据按键上下左右移动 可以控制速度和移动
if(e.getKeyCode()==KeyEvent.VK_W){
hero.setDirect(0);
hero.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_S){
hero.setDirect(1);
hero.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_A){
hero.setDirect(2);
hero.moveLeft();
}else if(e.getKeyCode()==KeyEvent.VK_D){
hero.setDirect(3);
hero.moveRight();
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}
members.java
package Tank_02;
//坦克父类 可以设置坦克出现位置(x,y)
class Tank {
int x = 0;
int y = 0;
int speed = 10;
int Direct = 0;
int color;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getDirect() {
return Direct;
}
public void setDirect(int direct) {
Direct = direct;
}
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;
}
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;
}
//视频中是把移动放在hero类中
public void moveUp(){
y-=speed;
}
public void moveDown(){
y+=speed;
}
public void moveLeft(){
x-=speed;
}
public void moveRight(){
x+=speed;
}
}
//敌方坦克
class EnemyTank extends Tank{
public EnemyTank(int x, int y) {
super(x, y);
}
}
//我的英雄坦克
class Hero extends Tank{
public Hero(int x,int y){
super(x, y);
}
}