java 贪吃蛇程序

java 贪吃蛇程序公司的一个同事 关系不错 一起搞了一个 java 的贪吃蛇程序 公司之余的闲弄 java 代码 nbsp nbsp 窗体显示 nbsp nbsp nbsp nbsp class nbsp MyFrame nbsp extends nbsp JFrame nbsp nbsp nbsp nbsp public nbsp MyFrame nbsp nbsp nbsp nbsp setSize width height nbsp nbsp setTitl

公司的一个同事,关系不错,一起搞了一个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

     snake = 
    new ArrayList

    ();  

  161.   
  162.   
  163. }  
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/217571.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 上午9:16
下一篇 2026年3月18日 上午9:17


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号