📄 people.java
字号:
package game;
import game.People;
import game.Client;
import game.Way;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Random;
public class People {
/**
* 玩家能向四个方向运动
**/
public static final int XSPEED=5;
public static final int YSPEED=5;
public static final int WIDTH=10;
public static final int HEIGHT=10;
private static Random r=new Random();
private int step=r.nextInt(12)+3;
private int money=100; //起始金币值
private int life=100; //起始金币值
Client client=null;
private boolean good=true;//判断是玩家还是阻碍者
private boolean alive=true;//判断是玩家是否已回答问题
private BloodBar bb=new BloodBar();
private int x,y;
private int oldX,oldY;
private boolean bU=false,bD=false,bL=false,bR=false;
enum Direction{L,U,R,D,STOP};
private Direction dir=Direction.STOP;//定义运动方向
private Direction ptDir=Direction.D;//定义行走方向
public People(int x, int y,boolean good) {
this.x = x;
this.y = y;
this.oldX=x;
this.oldY=y;
this.good=good;
}
public People(int x, int y,boolean good,Direction dir,Client client) {
this(x,y,good);
this.dir=dir;
this.client=client;
}
public void draw(Graphics g){
if(!alive){
if(!good){
client.monster.remove(this);
}
return;
}
Color c=g.getColor();
if(good){
g.setColor(Color.RED); //“好”玩家是红色
bb.draw(g);
}
else{
g.setColor(Color.BLACK); //“坏”阻碍者是黑色
}
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
switch(ptDir){
case L:
g.drawLine(x+People.WIDTH/2, y+People.HEIGHT/2, x, y+People.HEIGHT/2);
break;
case U:
g.drawLine(x+People.WIDTH/2, y+People.HEIGHT/2, x+People.WIDTH/2, y);
break;
case R:
g.drawLine(x+People.WIDTH/2, y+People.HEIGHT/2, x+People.WIDTH, y+People.HEIGHT/2);
break;
case D:
g.drawLine(x+People.WIDTH/2, y+People.HEIGHT/2, x+People.WIDTH/2, y+People.HEIGHT);
break;
}
move();
}
void move(){
this.oldX=x;
this.oldY=y;
switch(dir){
case L:
x-=XSPEED;
break;
case U:
y-=YSPEED;
break;
case R:
x+=XSPEED;
break;
case D:
y+=XSPEED;
break;
case STOP:
break;
}
if(this.dir!=Direction.STOP){
this.ptDir=this.dir;
}
//解决坦克出界问题
if(x<0) x=0;
if(y<40) y=40;
if(x+People.WIDTH>Client.BG_WIDTH) x=Client.BG_WIDTH-People.WIDTH;
if(y+People.HEIGHT>Client.BG_HEIGHT) y=Client.BG_HEIGHT-People.HEIGHT;
if(!good){//使用随机数产生器,让敌军坦克动起来
Direction[]dirs=Direction.values();
if(step==0){
step=r.nextInt(12)+3;
int randomNumber=r.nextInt(dirs.length);
dir=dirs[randomNumber];
//this.fire();
}
step--;
}
}
private void stay(){
x=oldX;
y=oldY;
}
public void keyPressed(KeyEvent e) {
int key=e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:
bL=true;
break;
case KeyEvent.VK_UP:
bU=true;
break;
case KeyEvent.VK_RIGHT:
bR=true;
break;
case KeyEvent.VK_DOWN:
bD=true;
break;
case KeyEvent.VK_A:
bL=true;
break;
case KeyEvent.VK_W:
bU=true;
break;
case KeyEvent.VK_D:
bR=true;
break;
case KeyEvent.VK_S:
bD=true;
break;
case KeyEvent.VK_F2: //若我方坦克死了,按F2重新开始游戏
if(!this.alive){
this.alive=true;
this.money=100;
}
break;
}
locateDirection();
}
void locateDirection(){
if(bL&&!bU&&!bR&&!bD) dir=Direction.L;
else if(!bL&&bU&&!bR&&!bD) dir=Direction.U;
else if(!bL&&!bU&&bR&&!bD) dir=Direction.R;
else if(!bL&&!bU&&!bR&&bD) dir=Direction.D;
else if(!bL&&!bU&&!bR&&!bD) dir=Direction.STOP;
}
//处理键抬起时的消息
public void keyReleased(KeyEvent e) {
int key=e.getKeyCode();
switch(key){
case KeyEvent.VK_M:
//按Alt键抬起时发出多发炮弹炮弹_在此之前的版本我的是Alt键
//老是惹出很多问题
//!!!!不可用Alt键
superFire();
break;
case KeyEvent.VK_CONTROL://按Ctrl键抬起时发出炮弹——-优化程序
fire();
break;
case KeyEvent.VK_LEFT:
bL=false;
break;
case KeyEvent.VK_UP:
bU=false;
break;
case KeyEvent.VK_RIGHT:
bR=false;
break;
case KeyEvent.VK_DOWN:
bD=false;
break;
case KeyEvent.VK_A:
bL=false;
break;
case KeyEvent.VK_W:
bU=false;
break;
case KeyEvent.VK_D:
bR=false;
break;
case KeyEvent.VK_S:
bD=false;
break;
/*case KeyEvent.VK_F2: //若我方坦克死了,按F2重新开始游戏
break;*/
}
locateDirection();
}
public Missile fire(){//坦克打出一颗子弹
if(!alive) return null;
int x=this.x+People.WIDTH/2-Missile.WIDTH/2;
int y=this.y+People.HEIGHT/2-Missile.HEIGHT/2;
Missile m=new Missile(x,y,good,ptDir,this.client);//根据炮筒方向发射炮弹,并判断是否为好炮弹
client.missiles.add(m);
return m;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public boolean isAlive() {
return alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
public boolean isGood() {
return good;
}
public Rectangle getRect(){ //获得“恶魔”的位置
return new Rectangle(x,y,WIDTH,HEIGHT);
}
public boolean onWay(Way wall){ //判断是否沿路运动
if(this.alive&&!this.getRect().intersects(wall.getRect())){
//this.stay();//若“恶魔”下道,则返回下道前位置——————此为法一…………即路尽头
//若下道,则方向改为反向————————————此为法二
if(this.dir==Direction.L){
dir=Direction.R;
}
else if(this.dir==Direction.U){
dir=Direction.D;
}
else if(this.dir==Direction.R){
dir=Direction.L;
}
else if(this.dir==Direction.D){
dir=Direction.U;
}
else if(this.dir==Direction.STOP){
dir=Direction.STOP;
}
return true;
}
return false;
}
//?????????????????????
public boolean collidesWithMonster(java.util.List<People> players){ //判断玩家与阻碍者间的碰撞
for(int i=0;i<players.size();i++){
People t=players.get(i);
//People people=new People();
if(this!=t){
if(this.alive&&t.alive&&this.getRect().intersects(t.getRect())){
this.stay();
t.stay();
return true;
}
}
}
return false;
}
public boolean collidesWithPeoples(java.util.List<People> players){ //判断阻碍者间的碰撞
for(int i=0;i<players.size();i++){
People t=players.get(i);
if(this!=t){
if(this.alive&&t.alive&&this.getRect().intersects(t.getRect())){
this.stay();
t.stay();
return true;
}
}
}
return false;
}
public Missile fire(Direction dir){
if(!alive) return null;
int x=this.x+People.WIDTH/2-Missile.WIDTH/2;
int y=this.y+People.HEIGHT/2-Missile.HEIGHT/2;
Missile m=new Missile(x,y,good,dir,this.client);//根据炮筒方向发射炮弹,并判断是否为好炮弹
client.missiles.add(m);
return m;
}
public void superFire(){//坦克发射多发炮弹
Direction[] dirs=Direction.values();
for(int i=0;i<dirs.length;i++){
fire(dirs[i]);
}
}
public int getMoney() {
return money;
}
public void setMoneye(int money) {
this.money = money;
}
/**
* 使用内部类
* 用图形化的血条来表示我方坦克的生命值
**/
public class BloodBar{
public void draw(Graphics g){
Color c=g.getColor();
g.drawRect(x, y-10, WIDTH, 10);
g.setColor(Color.RED);
g.fillOval(x, y-10, WIDTH*money/100, 10);
g.setColor(c);
}
}
public boolean eat(Blood b){
if(this.alive&&b.isAlive()&&this.getRect().intersects(b.getRect())&&this.getMoney()<100){
this.money=100;
b.setAlive(false);
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -