|
发表于 2017-3-28 15:07:17
|
显示全部楼层
Directionpackage tank;
public enum Direction {
UP(0),
DOWN(2),
LEFT(3),
RIGHT(1);
private int value;
private Direction(int value){
this.value = value;
}
public int getValue(){
return this.value;
}
}
Pointpackage tank;
import java.util.Random;
public class Point {
int x;
int y;
public Point(int x, int y) {
super();
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;
}
public void rotation(Direction direction,Point center){
int h = center.getY() - y;
int w = center.getX() - x;
switch(direction){
case UP:
//向上不用变化
break;
case RIGHT:
//翻转90°
x += h + w;
y = center.getY() - w;
break;
case DOWN:
//翻转180°,x不变,y变化
//其实,这边也是不用变的,反正上下都看不出
x = center.getX() + w;
y = center.getY() + h;
break;
case LEFT:
//翻转270°
x += h + w;
y = center.getY() - w;
break;
}
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
}
Tankpackage tank;
import java.awt.Color;
import java.awt.Graphics;
public class Tank {
int x,y;
byte state=1;//状态
Direction direction = Direction.UP;//默认的是向上的方向
public final int WIDTH=20;
public final int HIGHT=20;
Color tankColor;
public Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public Tank(int ax,int ay,Color ac){
x=ax;
y=ay;
tankColor=ac;
}
//move坦克
public void move(){
state--;
if(state<1)
state=3;
}
public void draw(Graphics g){
move();
Color c=g.getColor();
drawBody(g,x,y,Color.RED);
drawTrack(state,g,x,y,Color.RED);
g.setColor(c);
}
public void drawTrack(int a,Graphics g,int x,int y,Color c){
g.setColor(c);
for(int i=a;i<=28;i=i+3)
{
drawLine(g,x-13,y-14+i,x-11,y-14+i);//画出履带上的直线
drawLine(g,x+11,y-14+i,x+13,y-14+i);
}
}
public void drawBody(Graphics g,int x,int y,Color c){
g.setColor(c);
g.drawRect(x-10,y-10,20,20);
g.drawOval(x-8,y-8,16,16);
drawRect_body(g,x-1,y-24,2,24);//坦克的身体
drawRect_left(g,x-14,y-14,4,28);
drawRect_right(g,x+10,y-14,4,28);
}
private void drawLine(Graphics g, int x1, int y1, int x2, int y2){
//绘制之前首先翻转坐标
Point p1 = new Point(x1,y1);
Point p2 = new Point(x2,y2);
p1.rotation(direction, getCenterPoint());
p2.rotation(direction, getCenterPoint());
g.drawLine(p1.getX(),p1.getY(), p2.getX(),p2.getY());
}
private void drawRect_left(Graphics g,int x, int y, int width, int height){
int temp;
switch (direction) {
case UP:
break;
case RIGHT:
temp = width;
width = height;
height = temp;
break;
case DOWN:
break;
case LEFT:
temp = width;
width = height;
height = temp;
break;
}
g.drawRect(x,y,width,height);//坦克的身体
}
private void drawRect_right(Graphics g,int x, int y, int width, int height){
int temp;
switch (direction) {
case UP:
break;
case RIGHT:
x -= 24;
y += height-width;
temp = width;
width = height;
height = temp;
break;
case DOWN:
break;
case LEFT:
x -= 24;
y += height-width;
temp = width;
width = height;
height = temp;
break;
}
g.drawRect(x,y,width,height);//坦克的身体
}
private void drawRect_body(Graphics g,int x, int y, int width, int height){
int temp;
switch (direction) {
case UP:
break;
case RIGHT:
y += height;
temp = width;
width = height;
height = temp;
break;
case DOWN:
y += height;
break;
case LEFT:
y += height;
x -= height;
temp = width;
width = height;
height = temp;
break;
}
g.drawRect(x,y,width,height);//坦克的身体
}
/**
* 返回当前tank 的中点坐标
* @return
*/
public Point getCenterPoint(){
return new Point(x, 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;
}
}
TankGameWindowpackage tank;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
@SuppressWarnings("serial")
public class TankGameWindow extends Frame{
Tank tank;
public void initFrame(){
this.setVisible(true);
this.setLocation(300,300);//实现坦克大战的窗口,且填充了背景颜色
this.setSize(800,500);
this.setTitle("CRAZY TANK");
this.setResizable(false);
this.setBackground(Color.green);
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_DOWN){
tank.setY(tank.getY() + 3);
tank.setDirection(Direction.DOWN);
}else if(e.getKeyCode()== KeyEvent.VK_UP){
tank.setY(tank.getY() - 3);
tank.setDirection(Direction.UP);
}else if(e.getKeyCode()== KeyEvent.VK_LEFT){
tank.setX(tank.getX() - 3);
tank.setDirection(Direction.LEFT);
}else if(e.getKeyCode()== KeyEvent.VK_RIGHT){
tank.setX(tank.getX() + 3);
tank.setDirection(Direction.RIGHT);
}
}
});
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
tank=new Tank(300,300,Color.red);
Thread t;
t=new Thread(new PaintThread());
t.start();
}
//坦克反复擦掉重画
private class PaintThread implements Runnable
{
public void run()
{
while(true)
{
repaint();
try{
Thread.sleep(50);//间隔50mm画
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
public void paint(Graphics g){
tank.draw(g);
}
}
TankWindowpackage tank;
public class TankWindow {
public static void main(String args[]){
TankGameWindow window=new TankGameWindow();
window.initFrame();
}
}
稍微修改了下,可以运行 |
|