java贪吃蛇编写教程_用Java开发贪吃蛇游戏

1 packagesnake;2

3 importjava.awt.Color;4 importjava.awt.Font;5 importjava.awt.Graphics;6 importjava.awt.event.ActionEvent;7 importjava.awt.event.ActionListener;8 importjava.awt.event.KeyEvent;9 importjava.awt.event.KeyListener;10 importjava.util.Random;11

12 importjavax.swing.ImageIcon;13 importjavax.swing.JPanel;14 importjavax.swing.Timer;15

16 public class SnakePanel extends JPanel implementsKeyListener,ActionListener{17

18 //定义七个图片变量,代表七张图片

19 ImageIcon up = new ImageIcon("up.png"); //向上的蛇头

20 ImageIcon down = new ImageIcon("down.png"); //向下的蛇头

21 ImageIcon left = new ImageIcon("left.png"); //向左的蛇头

22 ImageIcon right = new ImageIcon("right.png"); //向右的蛇头

23 ImageIcon food = new ImageIcon("food.png"); //食物

24 ImageIcon body = new ImageIcon("body.png"); //蛇的身体

25 ImageIcon title = new ImageIcon("title.jpg"); //游戏界面的主题26

27 //蛇的每一部分

28 int[] snakex = new int [750];29 int[] snakey = new int [750];30

31 //随机生成食物

32 Random rand = newRandom();33 int foodx = rand.nextInt(34)*25+25; //此处的数值根据自己设计的游戏界面的大小来确定

34 int foody = rand.nextInt(24)*25+75;35

36 //设置游戏的默认属性

37 int len = 3;38 int score = 0;39 String direction = "R"; //U上 D下 L左 R右

40

41 boolean isStarted = false; //判断游戏是否开始

42 boolean isFailed = false; //判断游戏是否结束

43

44 Timer timer = new Timer(100,this); //每100毫秒调用一次ActionPerformed

45

46

47 public SnakePanel() { //建造画布的构造函数

48 this.setFocusable(true); //获取焦点

49 this.addKeyListener(this); //监听键盘事件

50 setup();51 timer.start();52 }53

54 public void paint(Graphics g) { //Graphics 画笔

55

56 this.setBackground(Color.BLACK); //设置画布背景颜色

57 title.paintIcon(this, g, 25, 11);//放置主题图片

58 g.fillRect(25, 75, 850, 650); //用画笔设置游戏方框59

60 //画蛇头(注意判断蛇头的方向)

61 if (direction.equals("R"))62 right.paintIcon(this, g, snakex[0], snakey[0]);63 else if (direction.equals("L"))64 left.paintIcon(this, g, snakex[0], snakey[0]);65 else if (direction.equals("U"))66 up.paintIcon(this, g, snakex[0], snakey[0]);67 else if (direction.equals("D"))68 down.paintIcon(this, g, snakex[0], snakey[0]);69

70 //画蛇的身体

71 for(int i = 1; i < len; i ++)72 body.paintIcon(this, g, snakex[i], snakey[i]);73

74 //判断如果游戏没开始显示。。。

75 if (!isStarted){76 g.setColor(Color.WHITE);77 g.setFont(new Font("arial",Font.BOLD, 30));78 g.drawString("Press Space to start / pause", 200, 300);79 }80

81 //判断如果游戏结束显示。。。

82 if(isFailed){83 g.setColor(Color.WHITE);84 g.setFont(new Font("arial",Font.BOLD, 30));85 g.drawString("Game Over ! Press space to restart", 200, 300);86 }87

88 //显示食物

89 food.paintIcon(this, g, foodx, foody);90

91 //设置分数和蛇的长度

92 g.setColor(Color.WHITE);93 g.setFont(new Font("arial",Font.PLAIN,15));94 g.drawString("Score : "+score, 650, 37);95 g.drawString("Len :"+len, 650, 57);96 }97

98 public void setup() { //游戏初始化

99 isStarted = false;100 isFailed = false;101 len = 3;102 score = 0;103 snakex[0] = 100; snakex[1] = 75; snakex[2] = 50;104 snakey[0] = 100; snakey[1] = 100; snakey[2] = 100;105 }106

107 @Override108 public voidkeyPressed(KeyEvent e) {109

110 //实现键盘响应

111 int KeyCode =e.getKeyCode();112 if (KeyCode == KeyEvent.VK_SPACE){ //敲击空格现实/消除提示信息

113 if(isFailed){114 //isStarted = false;//可以将这两行放入setup中115 //isFailed = false;

116 setup();117 }else

118 isStarted = !isStarted;119 } else if (KeyCode == KeyEvent.VK_UP && direction != "D")120 direction = "U";121 else if (KeyCode == KeyEvent.VK_DOWN && direction != "U")122 direction = "D";123 else if (KeyCode == KeyEvent.VK_RIGHT && direction != "L")124 direction = "R";125 else if (KeyCode == KeyEvent.VK_LEFT && direction != "R")126 direction = "L";127 }128

129 @Override130 public voidactionPerformed(ActionEvent e) {131 //1. 再定义一个闹钟

132 timer.start();133

134 //2. 移动数据

135 if (isStarted && !isFailed){136 //移动身体

137 for (int i = len; i>0; i--){138 snakex[i] = snakex[i-1];139 snakey[i] = snakey[i-1];140 }141 //移动头

142 if (direction.equals("R")){143 snakex[0] = snakex[0] + 25;144 if(snakex[0] > 850) snakex[0] = 25;145 }else if (direction.equals("L")){146 snakex[0] = snakex[0] - 25;147 if(snakex[0] < 25) snakex[0] = 850;148 }else if (direction.equals("U")){149 snakey[0] = snakey[0] - 25;150 if (snakey[0] < 75) snakey[0] = 650;151 }else if (direction.equals("D")){152 snakey[0] = snakey[0] + 25;153 if (snakey[0] > 650) snakey[0] = 75;154 }155

156 if (snakex[0] == foodx && snakey[0] == foody){ //吃食物

157 len ++;158 score ++;159 foodx = rand.nextInt(34)*25+25;160 foody = rand.nextInt(24)*25+75;161 }162

163 for (int i = 1; i < len; i ++){ //如果蛇头碰到自己的身体游戏结束

164 if (snakex[0] == snakex[i] && snakey[0] ==snakey[i]){165 isFailed = true;166 }167 }168

169 }170 //3. repaint()

171 repaint();172 }173

174 @Override175 public voidkeyTyped(KeyEvent e) {176

177 }178

179 @Override180 public voidkeyReleased(KeyEvent e) {181

182 }183 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值