好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java实现简单的飞机大战游戏(控制主飞机篇)

本文实例为大家分享了Java实现简单的飞机大战游戏,控制主飞机的具体代码,供大家参考,具体内容如下

接着上一篇: Java实现简单的飞机大战游戏(敌机下落篇) ,首先我们需要明白,在控制一架飞机的时候,需要通过对键盘的监听。上下左右比较简单,因为是对单一按键的监听,在这里我们可以通过switch语句,通过读取键盘上每个按键的ASCII值。但是如果想要实现边飞行边发射子弹,又或者是实现斜飞怎么办?这里就需要通过对多个按键的监听。
解决的方法是:单一的方向控制我们还是使用switch语句,但是如果牵扯到2个或2个以上的按键监听,我们需要将按键设置一下标记位。即按下为真,释放为假。并且这些处理都是在switch case语句以外,就可以实现斜飞。然后再对窗体进行重绘,我们就可以看到飞机的坐标在不断的发生改变。

如以下代码所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

public class MovePlaneListener extends KeyAdapter implements KeyListener {

     public int x, y, flag = 0 ;

     public JFrame frame;

     public boolean left = false , right = false , up = false , down = false ;

     public Graphics g;

     public Image image;

     public int x1, y1, vy = 16 ;

     public Vector<Bullet> vector;

     ImageIcon icon = new ImageIcon( "plane/myplane.png" );

     public BulletAI ba;

 

     public void setmove( int x, int y, JFrame frame) {

         this .x = x;

         this .y = y;

         this .frame = frame;

     }

 

     public MovePlaneListener() {

         vector = new Vector<Bullet>();

 

     }

 

     public void keyPressed(KeyEvent e) {

 

         switch (e.getKeyCode()) {

 

         case 87 : //W-> 上

             if (y > 0 ) {

 

                 y = y - 3 ;

                 up = true ;

             }

 

             break ;

         case 65 : //A-> 左

             if (x > 0 ) {

                 x = x - 3 ;

                 left = true ;

             }

             break ;

         case 83 : //S-> 下

 

             if (y + icon.getIconHeight() < frame.getHeight()) {

                 y = y + 3 ;

                 down = true ;

             }

             break ;

         case 68 : //D-> 右

 

             if (x + icon.getIconWidth() < frame.getWidth()) {

                 x = x + 3 ;

                 right = true ;

             }

 

             break ;

 

         case 32 : // 空格->发射子弹

 

             Bullet bu = new Bullet(vector, frame, x, y, vy);

             vector.add(bu);

             ba = new BulletAI(vector, frame, x, y, vy);

             if (flag == 0 ) {

                 ba.start();

                 flag = 1 ;

             }

 

             break ;

         }

 

         if (x > 0 && y > 0 && x + icon.getIconWidth() < frame.getWidth()

                 && y + icon.getIconHeight() < frame.getHeight())

 

         {

             if (left == true && up == true ) {

                 x = x - 3 ;

                 y = y - 3 ;

             } else if (left == true && down == true ) {

                 x = x - 3 ;

                 y = y + 3 ;

             } else if (right == true && up == true ) {

                 x = x + 3 ;

                 y = y - 3 ;

             } else if (right == true && down == true ) {

                 x = x + 3 ;

                 y = y + 3 ;

             }

         }

         // System.out.println(x + ">>>>>" + y);

         //窗体重绘

         frame.repaint();

 

     }

 

     public void keyReleased(KeyEvent e) {

         switch (e.getKeyCode()) {

         case 87 : // 上

             up = false ;

             break ;

 

         case 65 : // 左

 

             left = false ;

             break ;

 

         case 83 : // 下

 

             down = false ;

             break ;

 

         case 68 : // 右

 

             right = false ;

             break ;

         }

     }

}

之后就是关于子弹的发射,这里的处理方法和我的上一篇文章差不多,通过新建一个子弹类,每生成一个子弹后就将其添加到vector向量里,然后利用线程不断的改变子弹的坐标。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/weixin_42331530/article/details/89393890

查看更多关于Java实现简单的飞机大战游戏(控制主飞机篇)的详细内容...

  阅读:17次