java 贪吃蛇程序



公司的一个同事,关系不错,一起搞了一个java的贪吃蛇程序,公司之余的闲弄。
java 代码
 
  1. /* 
  2. 窗体显示 
  3.  
  4. */  
  5. class MyFrame extends JFrame  
  6. {  
  7. public MyFrame()  
  8. {  
  9. setSize(width,height);  
  10. setTitle("snake");  
  11. MyPanel p = new MyPanel();  
  12. add(p);  
  13. setLocation(200,180);  
  14. //System.out.println(getBackground());  
  15. }  
  16. private int width = 500;  
  17. private int height = 430;  
  18. }  
  19.   
  20. /* 
  21. 测试程序 
  22. */  
  23.   
  24. public class Snake  
  25. {  
  26. public static void main(String[] args)  
  27. {  
  28. MyFrame frame = new MyFrame();  
  29. frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);  
  30. frame.setVisible(true);  
  31. }  
  32. }  
  33.   
  34.   
  35.   
  36.   
  37.   
  38.   
  39.   
  40.   
  41. /*主要功能程序*/  
  42.   
  43. import javax.swing.*;  
  44. import java.awt.*;  
  45. import javax.swing.Timer;  
  46. import java.awt.event.*;  
  47. import java.util.*;  
  48. import java.awt.geom.*;  
  49. class MyPanel extends JPanel  
  50. {  
  51. public MyPanel()  
  52. {  
  53. clock.start();  
  54. snake.add(new Rectangle2D.Double(x,y,8,8));  
  55. snake.add(new Rectangle2D.Double(x + 10,y,8,8));  
  56. snake.add(new Rectangle2D.Double(x + 20,y,8,8));  
  57. snake.add(new Rectangle2D.Double(x + 30,y,8,8));  
  58. addKeyListener(new Listener());  
  59. setFocusable(true);  
  60.   
  61. }  
  62. public void paintComponent(Graphics g0)  
  63. {  
  64.   
  65. Graphics2D g = (Graphics2D) g0;  
  66. g0.setColor(backcolor);  
  67. for(Rectangle2D r : snake)  
  68. g.draw(r);  
  69. Move();  
  70. g.fillRect(108,35,50,10);  
  71. g.fillRect(380,35,50,10);  
  72. g0.setColor(frontcolor);  
  73. for(Rectangle2D r : snake)  
  74. g.draw(r);  
  75. //if(flag)  
  76. while(flag)  
  77. {  
  78.   
  79. foodx = (int)Math.round(Math.random()*390)/10*10 + 50;  
  80. foody = (int)Math.round(Math.random()*290)/10*10 + 50;  
  81. for(Rectangle2D r : snake)  
  82. if(r.getX() == foodx && r.getY() == foody) continue;  
  83. g.draw(new Rectangle2D.Double(foodx,foody,8,8));  
  84. flag = false;  
  85. }  
  86. g.drawRect(48,48,404,304);  
  87. g.drawString("Score "+String.valueOf(score),70,45);  
  88. g.drawString(String.valueOf(time),380,45);  
  89. if(delay == 0) delay = 1;  
  90. else delay = 0;  
  91. if(time > 0 && delay == 1) time--;  
  92.   
  93. }  
  94. public void Move()  
  95. {  
  96. if(direction == 0) x += 10//right  
  97. else if(direction == 1) y += 10//down  
  98. else if(direction == 2) x -= 10//left  
  99. else if(direction == 3) y -= 10//up  
  100. if(GameOver()) clock.stop();  
  101. if(x == foodx && y == foody)  
  102. {  
  103. snake.add(new Rectangle2D.Double(foodx,foody,8,8));  
  104. flag = true;  
  105. num++;  
  106. score += time;  
  107. time = 20;  
  108. }  
  109. for(int i=num;i>0;i--)  
  110. snake.set(i,snake.get(i - 1));  
  111. snake.set(0,new Rectangle2D.Double(x,y,8,8));  
  112.   
  113. }  
  114. public boolean GameOver()  
  115. {  
  116. if(x<50 || x>440 || y<50 || y>340return true;  
  117. else return false;  
  118. }  
  119.   
  120. class MyListener implements ActionListener  
  121. {  
  122. public void actionPerformed(ActionEvent e)  
  123. {  
  124. repaint();  
  125. }  
  126. }  
  127. class Listener implements KeyListener  
  128. {  
  129. public void keyTyped(KeyEvent e)  
  130. {  
  131. }  
  132.   
  133. public void keyPressed(KeyEvent e)  
  134. {  
  135. int key = e.getKeyCode(); // getKeyCode()  
  136. if(key == e.VK_RIGHT && direction != 2) direction = 0//right  
  137. else if(key == e.VK_DOWN && direction != 3) direction = 1//down  
  138. else if(key == e.VK_LEFT && direction != 0) direction = 2//left  
  139. else if(key == e.VK_UP && direction != 1) direction = 3//up  
  140. }  
  141.   
  142. public void keyReleased(KeyEvent e)  
  143. {  
  144. }  
  145. }  
  146.   
  147. private int foodx;  
  148. private int foody;  
  149. private int x = 70;  
  150. private int y = 100;  
  151. private int num = 3;  
  152. private int delay =0;  
  153. private int score = 0;  
  154. private int time = 20;  
  155. private int direction = 0;  
  156. private boolean flag = true;  
  157. private Color frontcolor = new Color(51,51,51);  
  158. private Color backcolor = new Color(238,238,238);  
  159. private Timer clock = new Timer(300,new MyListener());  
  160. private ArrayList<Rectangle2D> snake = new ArrayList<Rectangle2D>();  
  161.   
  162.   
  163. }  
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值