course3 绘制坦克图片

master
Jian Hu 3 years ago
parent 45a1939f92
commit b14b6fd4f7

Binary file not shown.

Binary file not shown.

@ -42,7 +42,7 @@ public class Bullet {
default:
break;
}
if(x < 0 || y<0 || x> TankFrame.GAME_WIDTH || x > TankFrame.GAME_HEIGHT){
if(x < 0 || y<0 || x> TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT){
live = false;
}
}

@ -0,0 +1,82 @@
package com.demo.tank.course3;
import java.awt.*;
public class Bullet {
private int x, y;
private Direction direction;
private static final int SPEED = 5;
private static final int WIDTH = 20;
private static final int HEIGHT = 20;
private boolean live = true;
private TankFrameV3 tf;
public Bullet(int x, int y, Direction direction, TankFrameV3 tf) {
this.x = x;
this.y = y;
this.direction = direction;
this.tf = tf;
}
public void paint(Graphics g){
if(!live){
tf.bullets.remove(this);
}
Color color = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(color);
move();
}
private void move() {
switch (direction){
case UP: y -= SPEED;
break;
case DOWN: y += SPEED;
break;
case LEFT: x -= SPEED;
break;
case RIGHT: x += SPEED;
break;
default:
break;
}
if(x < 0 || y < 0 || x > TankFrameV3.GAME_WIDTH || y > TankFrameV3.GAME_HEIGHT){
live = false;
}
}
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 Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
}

@ -0,0 +1,5 @@
package com.demo.tank.course3;
public enum Direction {
LEFT, RIGHT, UP, DOWN;
}

@ -0,0 +1,13 @@
package com.demo.tank.course3;
import java.io.IOException;
public class MainV3 {
public static void main(String[] args) throws InterruptedException, IOException {
TankFrameV3 tf = new TankFrameV3();
while (true){
Thread.sleep(50);
tf.repaint();
}
}
}

@ -0,0 +1,20 @@
package com.demo.tank.course3;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class ResourceManager {
public static BufferedImage tankL, tankR, tankU, tankD;
static {
try {
tankL = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/tankL.gif"));
tankR = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/tankR.gif"));
tankU = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/tankU.gif"));
tankD = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/tankD.gif"));
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,91 @@
package com.demo.tank.course3;
import java.awt.*;
public class Tank {
private int x,y;
private Direction dir;
private static final int SPEED = 10;
private boolean moving;
private TankFrameV3 tankFrame;
public Tank(int x, int y, Direction dir, TankFrameV3 tankFrame) {
this.x = x;
this.y = y;
this.dir = dir;
this.tankFrame = tankFrame;
}
public void paint(Graphics g) {
//根据方向绘制坦克
switch (dir){
case UP:
g.drawImage(ResourceManager.tankU, x, y, null);
break;
case DOWN:
g.drawImage(ResourceManager.tankD, x, y, null);
break;
case LEFT:
g.drawImage(ResourceManager.tankL, x, y, null);
break;
case RIGHT:
g.drawImage(ResourceManager.tankR, x, y, null);
break;
}
move();
}
public void move(){
//如果没有移动 return
if(!moving) return;
switch (dir){
case UP: y -= SPEED;
break;
case DOWN: y += SPEED;
break;
case LEFT: x -= SPEED;
break;
case RIGHT: x += SPEED;
break;
default:
break;
}
}
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 Direction getDir() {
return dir;
}
public void setDir(Direction dir) {
this.dir = dir;
}
public boolean isMoving() {
return moving;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
public void fire() {
tankFrame.bullets.add(new Bullet(x, y, this.dir, tankFrame));
}
}

@ -0,0 +1,135 @@
package com.demo.tank.course3;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
public class TankFrameV3 extends Frame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
Image image = null;
Tank tank = new Tank(500, 500, Direction.UP, this);
// Bullet bullet = new Bullet(520, 440, Direction.UP);
List<Bullet> bullets = new ArrayList();
public TankFrameV3(){
setVisible(true);
setBounds(200, 200 , GAME_WIDTH, GAME_HEIGHT);
setResizable(false);
setTitle("tank war");
this.addKeyListener(new MyKeyListener());
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void update(Graphics g) {
if(image == null){
image = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics imgGraphic = image.getGraphics();
Color color = g.getColor();
imgGraphic.setColor(Color.BLACK);
imgGraphic.fillRect(0,0, GAME_WIDTH, GAME_HEIGHT);
imgGraphic.setColor(color);
paint(imgGraphic);
g.drawImage(image, 0, 0, null);
}
@Override
public void paint(Graphics g){
//打印出子弹数量
Color color = g.getColor();
g.setColor(Color.WHITE);
g.drawString("当前子弹数量:" + bullets.size(), 60, 50);
g.setColor(color);
tank.paint(g);
for (int i = 0; i< bullets.size(); i++){
bullets.get(i).paint(g);
}
// for(Iterator<Bullet> it = bullets.iterator(); it.hasNext();){
// Bullet b = it.next();
// if(!b.isLive()){
// it.remove();
// }
// }
//
// for (Bullet b : bullets){
// b.paint(g);
// }
}
class MyKeyListener extends KeyAdapter{
boolean bL = false;
boolean bR = false;
boolean bU = false;
boolean bD = false;
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()){
case KeyEvent.VK_A:
bL = true;
break;
case KeyEvent.VK_D:
bR = true;
break;
case KeyEvent.VK_W:
bU = true;
break;
case KeyEvent.VK_S:
bD = true;
break;
default:
break;
}
setTankDirection();
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()){
case KeyEvent.VK_A:
bL = false;
break;
case KeyEvent.VK_D:
bR = false;
break;
case KeyEvent.VK_W:
bU = false;
break;
case KeyEvent.VK_S:
bD = false;
break;
case KeyEvent.VK_SPACE:
tank.fire();
break;
default:
break;
}
setTankDirection();
}
public void setTankDirection(){
if(!bL && !bR && !bU && !bD){
tank.setMoving(false);
}else{
tank.setMoving(true);
if(bL) tank.setDir(Direction.LEFT);
if(bR) tank.setDir(Direction.RIGHT);
if(bU) tank.setDir(Direction.UP);
if(bD) tank.setDir(Direction.DOWN);
}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 824 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 868 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 936 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 855 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 839 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 839 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 852 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 847 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1022 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 814 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 460 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 565 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 846 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 864 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 863 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 861 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 863 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 673 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 670 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 680 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 677 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 691 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 674 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 271 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Loading…
Cancel
Save