PlayerOne类
package com.learn.exercise.tanke;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
/**
* 玩家一类
*/
public class PlayerOne extends Tank{
public boolean left;
public boolean right;
public boolean up;
public boolean down;
//创造构造函数匹配得出以下
public PlayerOne(String img, int x, int y, Panel panel, String upImg, String leftImg, String rightImg, String downImg) {
super(img, x, y, panel, upImg, leftImg, rightImg, downImg);
}
//控制坦克移动点击键盘
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
switch (key){
case KeyEvent.VK_A:
left = true;
break;
case KeyEvent.VK_S:
down = true;
break;
case KeyEvent.VK_D:
right = true;
break;
case KeyEvent.VK_W:
up = true;
break;
//按空格进行射击
case KeyEvent.VK_SPACE:
attack();
default:
break;
}
}
//释放键盘
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
switch (key){
case KeyEvent.VK_A:
left = false;
break;
case KeyEvent.VK_S:
down = false;
break;
case KeyEvent.VK_D:
right = false;
break;
case KeyEvent.VK_W:
up = false;
break;
default:
break;
}
}
//移动执行类
public void move(){
if (left){
leftward();
}else if (right){
rightward();
}else if (up){
upward();
}else if (down){
downward();
}
}
@Override
public void paintSelf(Graphics g) {
g.drawImage(img,x,y,null);
move();
}
@Override
public Rectangle gerRec() {
return new Rectangle(x,y,width,height);
}
}
PlayerTwo类
package com.learn.exercise.tanke;
import java.awt.*;
import java.awt.event.KeyEvent;
/**
* 玩家二类
*/
public class PlayerTwo extends Tank{
public boolean left;
public boolean right;
public boolean up;
public boolean down;
//创造构造函数匹配得出以下
public PlayerTwo(String img, int x, int y, Panel panel, String upImg, String leftImg, String rightImg, String downImg) {
super(img, x, y, panel, upImg, leftImg, rightImg, downImg);
}
//控制坦克移动点击键盘
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
switch (key){
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_DOWN:
down = true;
break;
case KeyEvent.VK_RIGHT:
right = true;
break;
case KeyEvent.VK_UP:
up = true;
break;
//按空格进行射击
case KeyEvent.VK_K:
attack();
default:
break;
}
}
//释放键盘
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
switch (key){
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_DOWN:
down = false;
break;
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_UP:
up = false;
break;
default:
break;
}
}
//移动执行类
public void move(){
if (left){
leftward();
}else if (right){
rightward();
}else if (up){
upward();
}else if (down){
downward();
}
}
@Override
public void paintSelf(Graphics g) {
g.drawImage(img,x,y,null);
move();
}
@Override
public Rectangle gerRec() {
return new Rectangle(x,y,width,height);
}
}