first commit

master
zhanxinlin 3 years ago
commit 21229d3a8d

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/tankWar.iml" filepath="$PROJECT_DIR$/tankWar.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="BranchesTreeState">
<expand>
<path>
<item name="ROOT" type="e8cecc67:BranchNodeDescriptor" />
<item name="LOCAL_ROOT" type="e8cecc67:BranchNodeDescriptor" />
</path>
</expand>
<select />
</component>
<component name="ChangeListManager">
<list default="true" id="3f94e015-c235-47fa-8e89-3005ea1dbca3" name="Default Changelist" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
</component>
<component name="MavenImportPreferences">
<option name="generalSettings">
<MavenGeneralSettings>
<option name="mavenHome" value="$PROJECT_DIR$/../../../worksoftware/apache-maven-3.6.3" />
<option name="userSettingsFile" value="D:\worksoftware\apache-maven-3.6.3\conf\settings.xml" />
</MavenGeneralSettings>
</option>
</component>
<component name="ProjectId" id="2FMGmTsV8KKoZs9WJhuOnwm8BeU" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showExcludedFiles" value="false" />
<option name="showLibraryContents" value="true" />
<option name="sortByType" value="true" />
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/../../tank/tank" />
<property name="settings.editor.selected.configurable" value="MavenSettings" />
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="3f94e015-c235-47fa-8e89-3005ea1dbca3" name="Default Changelist" comment="" />
<created>1664294698795</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1664294698795</updated>
<workItem from="1664294701908" duration="283000" />
<workItem from="1664375053218" duration="110000" />
</task>
<task id="LOCAL-00001" summary="day2实现坦克由键盘控制移动发出多个子弹">
<created>1664294827676</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1664294827676</updated>
</task>
<option name="localTasksCounter" value="2" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="2" />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
<option name="oldMeFiltersMigrated" value="true" />
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="day2实现坦克由键盘控制移动发出多个子弹" />
<option name="LAST_COMMIT_MESSAGE" value="day2实现坦克由键盘控制移动发出多个子弹" />
</component>
<component name="WindowStateProjectService">
<state x="-1362" y="95" key="CommitChangelistDialog2" timestamp="1664294827026">
<screen x="-1920" y="0" width="1920" height="1040" />
</state>
<state x="-1362" y="95" key="CommitChangelistDialog2/0.0.1536.824/-1920.0.1920.1040@-1920.0.1920.1040" timestamp="1664294827026" />
<state x="-1180" y="274" key="FileChooserDialogImpl" timestamp="1664375157424">
<screen x="-1920" y="0" width="1920" height="1040" />
</state>
<state x="-1180" y="274" key="FileChooserDialogImpl/0.0.1536.824/-1920.0.1920.1040@-1920.0.1920.1040" timestamp="1664375157424" />
<state x="-1368" y="252" key="Vcs.Push.Dialog.v2" timestamp="1664294847858">
<screen x="-1920" y="0" width="1920" height="1040" />
</state>
<state x="-1368" y="252" key="Vcs.Push.Dialog.v2/0.0.1536.824/-1920.0.1920.1040@-1920.0.1920.1040" timestamp="1664294847858" />
<state x="-1166" y="432" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2" timestamp="1664294901929">
<screen x="-1920" y="0" width="1920" height="1040" />
</state>
<state x="-1166" y="432" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2/0.0.1536.824/-1920.0.1920.1040@-1920.0.1920.1040" timestamp="1664294901929" />
<state x="-1198" y="419" key="git4idea.remote.GitConfigureRemotesDialog" timestamp="1664294753576">
<screen x="-1920" y="0" width="1920" height="1040" />
</state>
<state x="-1198" y="419" key="git4idea.remote.GitConfigureRemotesDialog/0.0.1536.824/-1920.0.1920.1040@-1920.0.1920.1040" timestamp="1664294753576" />
</component>
</project>

@ -0,0 +1,61 @@
import java.awt.*;
/**
* @Description:
* @Author: zhanxinlin
* @Date: 2022-09-27 23:10
*/
public class Bullet {
private static final int SPEED = 10;
private static final int WIDTH = 20;
private static final int HEIGHT = 20;
private int x;
private int y;
private FrameTank frameTank;
private Direction direction;
private boolean live = true;
public Bullet(int x, int y, Direction direction, FrameTank frameTank) {
this.x = x;
this.y = y;
this.direction = direction;
this.frameTank = frameTank;
}
public void paint(Graphics g) {
// 子弹超出界限,从子弹集合中移除
if (!live) {
frameTank.bulletList.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 LEFT:
x -= SPEED;
break;
case UP:
y -= SPEED;
break;
case RIGHT:
x += SPEED;
break;
case DOWN:
y += SPEED;
break;
}
// 子弹超出临界范围
if (x < 0 || y < 0 || y > FrameTank.GAME_HEIGHT || x > FrameTank.GAME_WIDTH) {
live = false;
}
}
}

@ -0,0 +1,8 @@
/**
* @Description:
* @Author: zhanxinlin
* @Date: 2022-09-27 22:21
*/
public enum Direction {
LEFT, UP, RIGHT, DOWN
}

@ -0,0 +1,161 @@
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;
/**
* @Description:
* @Author: zhanxinlin
* @Date: 2022-09-27 21:34
*/
public class FrameTank extends Frame{
static final int GAME_WIDTH = 800;
static final int GAME_HEIGHT = 600;
Tank myTank = new Tank(200 ,200, Direction.DOWN, this);
List<Bullet> bulletList = new ArrayList<>(); // 用集合来装子弹
// 构造方法
public FrameTank() {
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setResizable(false); // 窗口大小不可拉伸
this.setVisible(true); // 窗口可见
this.setTitle("tank war");
// 窗口监听器,点击"×",则关闭窗口,停止程序
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 键盘按钮监听器
this.addKeyListener(new MyKeyListener());
}
Image offScreenImage = null;
/**
*
*
* @param g
*/
@Override
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.BLACK);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
/**
*
*
* @param g
*/
@Override
public void paint(Graphics g) {
Color color = g.getColor();
g.setColor(Color.WHITE);
g.drawString("子弹的数量:" + bulletList.size(), 10, 60);
g.setColor(color);
// 坦克把自己画出来
myTank.paint(g);
// 子弹把自己画出来
for (int i = 0; i < bulletList.size(); i++) {
bulletList.get(i).paint(g);
}
}
class MyKeyListener extends KeyAdapter {
boolean leftDirection = false;
boolean upDirection = false;
boolean rightDirection = false;
boolean downDirection = false;
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
leftDirection = true;
break;
case KeyEvent.VK_UP:
upDirection = true;
break;
case KeyEvent.VK_RIGHT:
rightDirection = true;
break;
case KeyEvent.VK_DOWN:
downDirection = true;
break;
case KeyEvent.VK_CONTROL:
myTank.fire();
break;
default:
break;
}
setMainTankDirection();
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
leftDirection = false;
break;
case KeyEvent.VK_UP:
upDirection = false;
break;
case KeyEvent.VK_RIGHT:
rightDirection = false;
break;
case KeyEvent.VK_DOWN:
downDirection = false;
break;
default:
break;
}
setMainTankDirection();
}
/**
*
*/
private void setMainTankDirection() {
if (!leftDirection && !upDirection && !downDirection && !rightDirection) { // 没有按方向键,静止不动
myTank.setMoving(false);
} else { // 按了方向键
myTank.setMoving(true);
if (leftDirection) {
myTank.setDirection(Direction.LEFT);
}
if (rightDirection) {
myTank.setDirection(Direction.RIGHT);
}
if (upDirection) {
myTank.setDirection(Direction.UP);
}
if (downDirection) {
myTank.setDirection(Direction.DOWN);
}
}
}
}
}

@ -0,0 +1,15 @@
/**
* @Description:
* @Author: zhanxinlin
* @Date: 2022-09-27 22:00
*/
public class Main {
public static void main(String[] args) throws InterruptedException {
FrameTank frameTank = new FrameTank();
while (true) {
Thread.sleep(50);
frameTank.repaint(); // 重画窗口重新调用paint方法
}
}
}

@ -0,0 +1,86 @@
import java.awt.*;
/**
* @Description:
* @Author: zhanxinlin
* @Date: 2022-09-27 22:49
*/
public class Tank {
private static final int SPEED = 5;
private int x;
private int y;
private Direction direction;
private boolean moving = false;
private FrameTank frameTank = null;
public Tank(int x, int y, Direction direction, FrameTank frameTank) {
this.x = x;
this.y = y;
this.direction = direction;
this.frameTank = frameTank;
}
/**
* tank
*
* @param g
*/
public void paint(Graphics g) {
Color color = g.getColor();
g.setColor(Color.GREEN);
g.fillRect(x, y, 50, 50);
g.setColor(color);
move();
}
/**
*
*/
private void move() {
if (!moving) {
return;
}
// 定义坦克的移动特征
switch (direction) {
case LEFT:
x -= SPEED;
break;
case UP:
y -= SPEED;
break;
case RIGHT:
x += SPEED;
break;
case DOWN:
y += SPEED;
break;
}
}
/**
*
* newbulletFrameTankbullet
* FrameTank
*/
public void fire() {
// 将子弹加入子弹集合
frameTank.bulletList.add(new Bullet(this.x, this.y, this.direction, this.frameTank));
}
public Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public boolean isMoving() {
return moving;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
}

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Loading…
Cancel
Save