首页 后端开发 php教程 坦克大战DEMO

坦克大战DEMO

Jul 25, 2016 am 09:08 AM

实现了基本功能....地方坦克寻路做得比较弱智,每个坦克最多能同时发射5发炮弹....敌方有三种兵种,菜单栏没做响应,这个很简单,需要的可以自己加......上传全部代码,仅供学习参考...... 坦克大战DEMO
  1. package tank.common;
  2. abstract public class Bullet implements Runnable, Common {
  3. private int x, y;
  4. private int speed;
  5. private int direction;
  6. private boolean alive;
  7. protected int power;
  8. public Bullet(int x, int y, int speed, int direction) {
  9. this.x = x;
  10. this.y = y;
  11. this.speed = speed;
  12. this.direction = direction;
  13. alive = true;
  14. }
  15. public void die() {
  16. alive = false;
  17. }
  18. public int getPower() {
  19. return power;
  20. }
  21. public int getX() {
  22. return x;
  23. }
  24. public int getY() {
  25. return y;
  26. }
  27. public boolean isAlive() {
  28. return alive;
  29. }
  30. public void run() {
  31. while (true) {
  32. try {
  33. Thread.sleep(15);
  34. } catch (InterruptedException e) {
  35. // TODO Auto-generated catch block
  36. e.printStackTrace();
  37. }
  38. switch (direction) {
  39. case UP:
  40. y -= speed;
  41. break;
  42. case DOWN:
  43. y += speed;
  44. break;
  45. case RIGHT:
  46. x += speed;
  47. break;
  48. case LEFT:
  49. x -= speed;
  50. break;
  51. }
  52. if (x WIDTH || y > HEIGHT || y alive = false;
  53. return;
  54. }
  55. }
  56. }
  57. }
复制代码
  1. package tank.common;
  2. public interface Common {
  3. public static final int UP = 0;
  4. public static final int DOWN = 1;
  5. public static final int RIGHT = 2;
  6. public static final int LEFT = 3;
  7. public static final int WIDTH = 600;
  8. public static final int HEIGHT = 400;
  9. public static final int WATER = 0;
  10. public static final int WALLS = 1;
  11. public static final int STEELS = 2;
  12. public static final int GRASS = 3;
  13. }
复制代码
  1. package tank.common;
  2. import java.awt.Color;
  3. import java.awt.Point;
  4. public abstract class EnemyTank extends Tank {
  5. private class AutoFire implements Runnable {
  6. @Override
  7. public void run() {
  8. // TODO Auto-generated method stub
  9. while (isAlive()) {
  10. try {
  11. Thread.sleep(500);
  12. } catch (InterruptedException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. if (Math.random() > shotKey) {
  17. shot();
  18. }
  19. }
  20. }
  21. }
  22. private class AutoMove implements Runnable {
  23. @Override
  24. public void run() {
  25. // TODO Auto-generated method stub
  26. int moves[] = new int[4];
  27. while (isAlive()) {
  28. try {
  29. Thread.sleep(110);
  30. } catch (InterruptedException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. for (int i = 0; i moves[i] = judgeHero(i);
  35. }
  36. int direction = 0;
  37. int max = Integer.MIN_VALUE;
  38. for (int i = 0; i if (moves[i] >= max) {
  39. max = moves[i];
  40. direction = i;
  41. }
  42. }
  43. move(direction);
  44. }
  45. }
  46. }
  47. private double shotKey;
  48. private Point heroPosition;
  49. private Tank hero;
  50. public EnemyTank(int x, int y, Tank hero, int life, int ID) {
  51. super(x, y, Color.cyan);
  52. this.lifes = life;
  53. this.hero = hero;
  54. super.setImageID(ID);
  55. }
  56. private int judgeHero(int direction) {
  57. heroPosition = new Point(hero.getX() + 9, hero.getY() + 9);
  58. int result = 0;
  59. int x = this.getX();
  60. int y = this.getY();
  61. int speed = this.getSpeed();
  62. double distance1 = Math.abs((this.getX() - heroPosition.x)
  63. * (this.getX() - heroPosition.x)
  64. + (this.getY() - heroPosition.y)
  65. * (this.getY() - heroPosition.y));
  66. switch (direction) {
  67. case UP:
  68. y -= speed;
  69. break;
  70. case DOWN:
  71. y += speed;
  72. break;
  73. case RIGHT:
  74. x += speed;
  75. break;
  76. case LEFT:
  77. x -= speed;
  78. break;
  79. }
  80. if (getDirection() == direction) {
  81. result += 5000;
  82. }
  83. if (!canMove(x, y)) {
  84. result -= Integer.MAX_VALUE;
  85. }
  86. double distance2 = Math.abs((x - heroPosition.x) * (x - heroPosition.x)
  87. + (y - heroPosition.y) * (y - heroPosition.y));
  88. if (Math.random() > 0.8) {
  89. result += Math.random() * 20000;
  90. }
  91. result += (distance1 - distance2) * 10;
  92. return result;
  93. }
  94. public void setPosition(int x, int y) {
  95. super.x = x;
  96. super.y = y;
  97. }
  98. public void setShotSpeed(double shotSpeed) {
  99. this.shotKey = shotSpeed;
  100. }
  101. public void startFire() {
  102. new Thread(new AutoFire()).start();
  103. }
  104. public void startMove() {
  105. new Thread(new AutoMove()).start();
  106. }
  107. }
复制代码
  1. package tank.common;
  2. import java.awt.Color;
  3. import java.util.ArrayList;
  4. import tank.entity.NormalBullet;
  5. public abstract class Tank implements Common {
  6. protected int x;
  7. protected int y;
  8. private Color color;
  9. private int speed;
  10. private int direction;
  11. private ArrayList bullets;
  12. private ArrayList tanks;
  13. private Bullet bullet;
  14. private int maxBulletNum;
  15. private boolean alive;
  16. protected int lifes;
  17. ArrayList walls;
  18. private int tankImageID;
  19. {
  20. speed = 2;
  21. direction = UP;
  22. alive = true;
  23. }
  24. public Tank() {
  25. this.x = 0;
  26. this.y = 0;
  27. color = Color.black;
  28. }
  29. public Tank(int x, int y, Color color) {
  30. this.x = x;
  31. this.y = y;
  32. this.color = color;
  33. maxBulletNum = 5;
  34. bullets = new ArrayList();
  35. walls = new ArrayList();
  36. }
  37. protected boolean canMove(int x, int y) {
  38. if (x WIDTH - 20 || y HEIGHT - 20) {
  39. return false;
  40. }
  41. if (tanks == null) {
  42. return true;
  43. }
  44. if (tanks.size() == 1) {
  45. return true;
  46. }
  47. for (int i = 0; i Wall tempWall = walls.get(i);
  48. if (tempWall.isAlive()) {
  49. if (x >= tempWall.getX() && y >= tempWall.getY()) {
  50. if (x return tempWall.canBeWalk();
  51. }
  52. } else if (x >= tempWall.getX() && y if (x && (y + 20) >= tempWall.getY()) {
  53. return tempWall.canBeWalk();
  54. }
  55. } else if (x = tempWall.getY()) {
  56. if ((x + 20) >= tempWall.getX()
  57. && y return tempWall.canBeWalk();
  58. }
  59. } else if (x if ((x + 20) >= tempWall.getX()
  60. && (y + 20) >= tempWall.getY()) {
  61. return tempWall.canBeWalk();
  62. }
  63. }
  64. }
  65. }
  66. for (int i = 0; i Tank tempTank = tanks.get(i);
  67. if (tempTank == this)
  68. break;
  69. if (tempTank.isAlive()) {
  70. if (x >= tempTank.getX() && y >= tempTank.getY()) {
  71. if (x return false;
  72. }
  73. } else if (x >= tempTank.getX() && y if (x && (y + 20) >= tempTank.getY()) {
  74. return false;
  75. }
  76. } else if (x = tempTank.getY()) {
  77. if ((x + 20) >= tempTank.getX()
  78. && y return false;
  79. }
  80. } else if (x if ((x + 20) >= tempTank.getX()
  81. && (y + 20) >= tempTank.getY()) {
  82. return false;
  83. }
  84. }
  85. }
  86. }
  87. return true;
  88. }
  89. public void damage(int power) {
  90. lifes -= power;
  91. if (lifes alive = false;
  92. }
  93. }
  94. public ArrayList getBullet() {
  95. return bullets;
  96. }
  97. public Color getColor() {
  98. return color;
  99. }
  100. public int getDirection() {
  101. return direction;
  102. }
  103. public int getImageID() {
  104. return tankImageID;
  105. }
  106. public int getSpeed() {
  107. return speed;
  108. }
  109. public ArrayList getWalls() {
  110. return this.walls;
  111. }
  112. public int getX() {
  113. return x;
  114. }
  115. public int getY() {
  116. return y;
  117. }
  118. public boolean isAlive() {
  119. return alive;
  120. }
  121. public void move(int direction) {
  122. setDirection(direction);
  123. int x = this.x;
  124. int y = this.y;
  125. switch (direction) {
  126. case UP:
  127. y -= speed;
  128. break;
  129. case DOWN:
  130. y += speed;
  131. break;
  132. case RIGHT:
  133. x += speed;
  134. break;
  135. case LEFT:
  136. x -= speed;
  137. break;
  138. }
  139. if (canMove(x, y)) {
  140. this.x = x;
  141. this.y = y;
  142. }
  143. }
  144. public void setAllTanks(ArrayList tanks) {
  145. this.tanks = tanks;
  146. }
  147. public void setDirection(int direction) {
  148. this.direction = direction;
  149. }
  150. final protected void setImageID(int ID) {
  151. this.tankImageID = ID;
  152. }
  153. public void setSpeed(int speed) {
  154. this.speed = speed;
  155. }
  156. public void setWalls(ArrayList wallList) {
  157. this.walls = wallList;
  158. }
  159. public void shot() {
  160. switch (direction) {
  161. case UP:
  162. bullet = new NormalBullet(x + 10, y, UP);
  163. break;
  164. case DOWN:
  165. bullet = new NormalBullet(x + 10, y + 20, DOWN);
  166. break;
  167. case RIGHT:
  168. bullet = new NormalBullet(x + 20, y + 10, RIGHT);
  169. break;
  170. case LEFT:
  171. bullet = new NormalBullet(x, y + 10, LEFT);
  172. break;
  173. }
  174. for (int i = 0; i Bullet temp = bullets.get(i);
  175. if (!temp.isAlive()) {
  176. bullets.remove(temp);
  177. }
  178. }
  179. if (bullets.size() >= maxBulletNum) {
  180. } else {
  181. new Thread(bullet).start();
  182. bullets.add(bullet);
  183. }
  184. }
  185. }
复制代码
  1. package tank.common;
  2. public abstract class Wall {
  3. private int x, y;
  4. private int wallImageID;
  5. private boolean canWalk;
  6. private boolean canFly;
  7. private boolean alive;
  8. private boolean canHit;
  9. public Wall(int x, int y, int ID, boolean walk, boolean fly, boolean Hit) {
  10. this.x = x;
  11. this.y = y;
  12. this.wallImageID = ID;
  13. this.canWalk = walk;
  14. this.canFly = fly;
  15. this.alive = true;
  16. this.canHit = Hit;
  17. }
  18. public boolean canBeFly() {
  19. return canFly;
  20. }
  21. public boolean canBeHit() {
  22. return this.canHit;
  23. }
  24. public boolean canBeWalk() {
  25. return canWalk;
  26. }
  27. public void die() {
  28. alive = false;
  29. }
  30. public int getImageID() {
  31. return wallImageID;
  32. }
  33. public int getX() {
  34. return x;
  35. }
  36. public int getY() {
  37. return y;
  38. }
  39. public boolean isAlive() {
  40. return alive;
  41. }
  42. }
复制代码
  1. package tank.entity;
  2. public class Bomb {
  3. private int x;
  4. private int y;
  5. private int life = 9;
  6. private boolean alive;
  7. public Bomb(int x, int y) {
  8. this.x = x;
  9. this.y = y;
  10. alive = true;
  11. }
  12. public void decrese() {
  13. if (life > 0) {
  14. life--;
  15. } else {
  16. alive = false;
  17. }
  18. }
  19. public int getLife() {
  20. return life;
  21. }
  22. public int getX() {
  23. return x;
  24. }
  25. public int getY() {
  26. return y;
  27. }
  28. public boolean isAlive() {
  29. return alive;
  30. }
  31. }
复制代码
  1. package tank.entity;
  2. import tank.common.EnemyTank;
  3. import tank.common.Tank;
  4. public class EnemyTank1 extends EnemyTank {
  5. public EnemyTank1(int x, int y, Tank hero) {
  6. super(x, y, hero, 3, 1);
  7. setSpeed(2);
  8. setShotSpeed(0.8);
  9. }
  10. }
复制代码
  1. package tank.entity;
  2. import tank.common.EnemyTank;
  3. import tank.common.Tank;
  4. public class EnemyTank2 extends EnemyTank {
  5. public EnemyTank2(int x, int y, Tank hero) {
  6. super(x, y, hero, 5, 2);
  7. setSpeed(3);
  8. setShotSpeed(0.5);
  9. }
  10. }
复制代码
  1. package tank.entity;
  2. import tank.common.EnemyTank;
  3. import tank.common.Tank;
  4. public class EnemyTank3 extends EnemyTank {
  5. public EnemyTank3(int x, int y, Tank hero) {
  6. super(x, y, hero, 10, 3);
  7. setSpeed(5);
  8. setShotSpeed(0.2);
  9. }
  10. }
复制代码
  1. package tank.entity;
  2. import tank.common.Wall;
  3. public class Grass extends Wall {
  4. public Grass(int x, int y) {
  5. super(x, y, 3, true, true, false);
  6. }
  7. }
复制代码
  1. package tank.entity;
  2. import java.awt.Color;
  3. import tank.common.Tank;
  4. public class MyTank extends Tank {
  5. public MyTank(int x, int y) {
  6. super(x, y, Color.yellow);
  7. lifes = 5;
  8. super.setImageID(0);
  9. }
  10. public int getLife() {
  11. return lifes;
  12. }
  13. }
复制代码
  1. package tank.entity;
  2. import tank.common.Bullet;
  3. public class NormalBullet extends Bullet {
  4. public NormalBullet(int x, int y, int direction) {
  5. super(x, y, 2, direction);
  6. power = 1;
  7. }
  8. }
复制代码
  1. package tank.entity;
  2. import java.util.ArrayList;
  3. import tank.common.EnemyTank;
  4. public class Stage {
  5. private int totalEnemyNum;
  6. private int leaveEnemyNum;
  7. private int totalHeroLife;
  8. private int leaveHeroLife;
  9. private int level;
  10. private WallContainer wallContainer;
  11. private ArrayList enemeyTanks;
  12. private int activeEnemyTankNum;
  13. private EnemyTank activeEnemyTanks[];
  14. private MyTank hero;
  15. public Stage(int totalEnemyNum, int totalHeroLife, int level,
  16. int activeEnemyTankNum) {
  17. this.totalEnemyNum = totalEnemyNum;
  18. this.totalHeroLife = totalHeroLife;
  19. this.level = level;
  20. this.activeEnemyTankNum = activeEnemyTankNum;
  21. this.leaveEnemyNum = this.totalEnemyNum;
  22. this.leaveHeroLife = this.totalHeroLife;
  23. this.enemeyTanks = new ArrayList();
  24. this.activeEnemyTanks = new EnemyTank[this.activeEnemyTankNum];
  25. this.hero = new MyTank(290, 370);
  26. this.wallContainer = new WallContainer();
  27. }
  28. public void addEnemyTank(EnemyTank tank) {
  29. enemeyTanks.add(tank);
  30. }
  31. public void autoCreateEnemyTank() {
  32. for (int i = 0; i double key = Math.random();
  33. if (key this.enemeyTanks.add(new EnemyTank1(0, 0, hero));
  34. } else if (key >= 0.66) {
  35. this.enemeyTanks.add(new EnemyTank2(0, 0, hero));
  36. } else {
  37. this.enemeyTanks.add(new EnemyTank3(0, 0, hero));
  38. }
  39. }
  40. }
  41. public int getActiveEnemyTankNum() {
  42. return this.activeEnemyTankNum;
  43. }
  44. public EnemyTank[] getEnemyTank() {
  45. return this.activeEnemyTanks;
  46. }
  47. public MyTank getHeroTank() {
  48. return this.hero;
  49. }
  50. public int getLeaveEnemyNum() {
  51. return this.leaveEnemyNum;
  52. }
  53. public int getLeaveHeroLife() {
  54. return this.leaveHeroLife;
  55. }
  56. public int getLevel() {
  57. return this.level;
  58. }
  59. public WallContainer getWallContainer() {
  60. return this.wallContainer;
  61. }
  62. public boolean isHeroDead() {
  63. if (this.leaveHeroLife return true;
  64. } else
  65. return false;
  66. }
  67. public EnemyTank popEnemyTank() {
  68. if (leaveEnemyNum > 0) {
  69. this.leaveEnemyNum--;
  70. EnemyTank temp = enemeyTanks.get(enemeyTanks.size() - 1);
  71. enemeyTanks.remove(temp);
  72. return temp;
  73. } else
  74. return null;
  75. }
  76. public MyTank popHero() {
  77. leaveHeroLife--;
  78. MyTank temp = new MyTank(290, 370);
  79. temp.setWalls(wallContainer.getWallList());
  80. return temp;
  81. }
  82. public void setHeroTank(MyTank tank) {
  83. this.hero = tank;
  84. }
  85. }
复制代码
  1. package tank.entity;
  2. import tank.common.Wall;
  3. public class Steels extends Wall {
  4. public Steels(int x, int y) {
  5. super(x, y, 2, false, false, false);
  6. }
  7. }
复制代码
  1. package tank.entity;
  2. import java.util.ArrayList;
  3. import tank.common.Common;
  4. import tank.common.Wall;
  5. public class WallContainer implements Common {
  6. private ArrayList data;
  7. public WallContainer() {
  8. data = new ArrayList();
  9. for (int i = 0; i this.addWall(10 + i * 20, 100, WATER);
  10. if (i == 11) {
  11. i += 4;
  12. }
  13. }
  14. for (int i = 0; i this.addWall(10 + i * 20, 160, WALLS);
  15. if (i == 12) {
  16. i += 4;
  17. }
  18. }
  19. for (int i = 0; i this.addWall(10 + i * 20, 220, STEELS);
  20. if (i == 11) {
  21. i += 4;
  22. }
  23. }
  24. for (int i = 0; i this.addWall(10 + i * 20, 280, GRASS);
  25. if (i == 12) {
  26. i += 4;
  27. }
  28. }
  29. }
  30. public void addWall(int x, int y, int kind) {
  31. switch (kind) {
  32. case WATER:
  33. data.add(new Water(x, y));
  34. break;
  35. case WALLS:
  36. data.add(new Walls(x, y));
  37. break;
  38. case STEELS:
  39. data.add(new Steels(x, y));
  40. break;
  41. case GRASS:
  42. data.add(new Grass(x, y));
  43. break;
  44. }
  45. }
  46. public ArrayList getWallList() {
  47. ArrayList temp = data;
  48. return temp;
  49. }
  50. public boolean isEmpty() {
  51. return data.isEmpty();
  52. }
  53. public void removeDead() {
  54. for (int i = 0; i Wall temp = data.get(i);
  55. if (!temp.isAlive())
  56. data.remove(temp);
  57. }
  58. }
  59. }
复制代码
  1. package tank.entity;
  2. import tank.common.Wall;
  3. public class Walls extends Wall {
  4. public Walls(int x, int y) {
  5. super(x, y, 1, false, false, true);
  6. }
  7. }
复制代码
  1. package tank.entity;
  2. import tank.common.Wall;
  3. public class Water extends Wall {
  4. public Water(int x, int y) {
  5. super(x, y, 0, false, true, false);
  6. }
  7. }
复制代码
  1. package tank.gui;
  2. import javax.swing.JFrame;
  3. import javax.swing.JMenu;
  4. import javax.swing.JMenuBar;
  5. import javax.swing.JMenuItem;
  6. public class TankFrame extends JFrame {
  7. /**
  8. *
  9. */
  10. private static final long serialVersionUID = 1L;
  11. private TankPanel gamePanel;
  12. public TankFrame() {
  13. super("坦克大战————玄雨制作");
  14. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. // 添加游戏主面板
  16. gamePanel = new TankPanel();
  17. this.add(gamePanel);
  18. gamePanel.addMouseListener(gamePanel);
  19. this.addKeyListener(gamePanel);
  20. // 添加菜单栏
  21. JMenuBar menuBar = new JMenuBar();
  22. // ///////////////////////////////
  23. JMenu menu1 = new JMenu("菜单");
  24. menuBar.add(menu1);
  25. JMenuItem itemNewGame = new JMenuItem("新游戏");
  26. menu1.add(itemNewGame);
  27. menu1.addSeparator();
  28. JMenuItem itemList = new JMenuItem("排行榜");
  29. menu1.add(itemList);
  30. menu1.addSeparator();
  31. JMenuItem itemExit = new JMenuItem("退出");
  32. menu1.add(itemExit);
  33. // //////////////////////////////////
  34. JMenu menu2 = new JMenu("设置");
  35. JMenuItem itemSet = new JMenuItem("设置");
  36. menu2.add(itemSet);
  37. menuBar.add(menu2);
  38. // /////////////////////////////////
  39. JMenu menu3 = new JMenu("帮助");
  40. menuBar.add(menu3);
  41. JMenuItem itemInfo = new JMenuItem("关于");
  42. menu3.add(itemInfo);
  43. JMenuItem itemHelp = new JMenuItem("帮助");
  44. menu3.add(itemHelp);
  45. this.setJMenuBar(menuBar);
  46. this.setResizable(false);
  47. this.pack();
  48. this.setVisible(true);
  49. }
  50. }
复制代码
  1. package tank.start;
  2. import tank.gui.TankFrame;
  3. public class TankStart {
  4. /**
  5. * @param args
  6. */
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9. new TankFrame().setLocation(250, 150);
  10. }
  11. }
复制代码


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1658
14
CakePHP 教程
1415
52
Laravel 教程
1309
25
PHP教程
1257
29
C# 教程
1231
24
会话如何劫持工作,如何在PHP中减轻它? 会话如何劫持工作,如何在PHP中减轻它? Apr 06, 2025 am 12:02 AM

会话劫持可以通过以下步骤实现:1.获取会话ID,2.使用会话ID,3.保持会话活跃。在PHP中防范会话劫持的方法包括:1.使用session_regenerate_id()函数重新生成会话ID,2.通过数据库存储会话数据,3.确保所有会话数据通过HTTPS传输。

说明PHP中的不同错误类型(注意,警告,致命错误,解析错误)。 说明PHP中的不同错误类型(注意,警告,致命错误,解析错误)。 Apr 08, 2025 am 12:03 AM

PHP中有四种主要错误类型:1.Notice:最轻微,不会中断程序,如访问未定义变量;2.Warning:比Notice严重,不会终止程序,如包含不存在文件;3.FatalError:最严重,会终止程序,如调用不存在函数;4.ParseError:语法错误,会阻止程序执行,如忘记添加结束标签。

PHP和Python:比较两种流行的编程语言 PHP和Python:比较两种流行的编程语言 Apr 14, 2025 am 12:13 AM

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1? 说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,应使用password_hash和password_verify函数实现安全的密码哈希处理,不应使用MD5或SHA1。1)password_hash生成包含盐值的哈希,增强安全性。2)password_verify验证密码,通过比较哈希值确保安全。3)MD5和SHA1易受攻击且缺乏盐值,不适合现代密码安全。

什么是HTTP请求方法(获取,发布,放置,删除等),何时应该使用? 什么是HTTP请求方法(获取,发布,放置,删除等),何时应该使用? Apr 09, 2025 am 12:09 AM

HTTP请求方法包括GET、POST、PUT和DELETE,分别用于获取、提交、更新和删除资源。1.GET方法用于获取资源,适用于读取操作。2.POST方法用于提交数据,常用于创建新资源。3.PUT方法用于更新资源,适用于完整更新。4.DELETE方法用于删除资源,适用于删除操作。

PHP:网络开发的关键语言 PHP:网络开发的关键语言 Apr 13, 2025 am 12:08 AM

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

解释PHP 7.4中引入的箭头功能(短闭合)。 解释PHP 7.4中引入的箭头功能(短闭合)。 Apr 06, 2025 am 12:01 AM

箭头函数在PHP7.4中引入,是短闭包的简化形式。1)它们使用=>运算符定义,省略function和use关键字。2)箭头函数自动捕获当前作用域变量,无需use关键字。3)它们常用于回调函数和短小计算,提高代码简洁性和可读性。

PHP行动:现实世界中的示例和应用程序 PHP行动:现实世界中的示例和应用程序 Apr 14, 2025 am 12:19 AM

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

See all articles