公司的一个同事,关系不错,一起搞了一个java的贪吃蛇程序,公司之余的闲弄。
java 代码
- /*
- 窗体显示
- */
- class MyFrame extends JFrame
- {
- public MyFrame()
- {
- setSize(width,height);
- setTitle("snake");
- MyPanel p = new MyPanel();
- add(p);
- setLocation(200,180);
- //System.out.println(getBackground());
- }
- private int width = 500;
- private int height = 430;
- }
- /*
- 测试程序
- */
- public class Snake
- {
- public static void main(String[] args)
- {
- MyFrame frame = new MyFrame();
- frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- }
- /*主要功能程序*/
- import javax.swing.*;
- import java.awt.*;
- import javax.swing.Timer;
- import java.awt.event.*;
- import java.util.*;
- import java.awt.geom.*;
- class MyPanel extends JPanel
- {
- public MyPanel()
- {
- clock.start();
- snake.add(new Rectangle2D.Double(x,y,8,8));
- snake.add(new Rectangle2D.Double(x + 10,y,8,8));
- snake.add(new Rectangle2D.Double(x + 20,y,8,8));
- snake.add(new Rectangle2D.Double(x + 30,y,8,8));
- addKeyListener(new Listener());
- setFocusable(true);
- }
- public void paintComponent(Graphics g0)
- {
- Graphics2D g = (Graphics2D) g0;
- g0.setColor(backcolor);
- for(Rectangle2D r : snake)
- g.draw(r);
- Move();
- g.fillRect(108,35,50,10);
- g.fillRect(380,35,50,10);
- g0.setColor(frontcolor);
- for(Rectangle2D r : snake)
- g.draw(r);
- //if(flag)
- while(flag)
- {
- foodx = (int)Math.round(Math.random()*390)/10*10 + 50;
- foody = (int)Math.round(Math.random()*290)/10*10 + 50;
- for(Rectangle2D r : snake)
- if(r.getX() == foodx && r.getY() == foody) continue;
- g.draw(new Rectangle2D.Double(foodx,foody,8,8));
- flag = false;
- }
- g.drawRect(48,48,404,304);
- g.drawString("Score "+String.valueOf(score),70,45);
- g.drawString(String.valueOf(time),380,45);
- if(delay == 0) delay = 1;
- else delay = 0;
- if(time > 0 && delay == 1) time--;
- }
- public void Move()
- {
- if(direction == 0) x += 10; //right
- else if(direction == 1) y += 10; //down
- else if(direction == 2) x -= 10; //left
- else if(direction == 3) y -= 10; //up
- if(GameOver()) clock.stop();
- if(x == foodx && y == foody)
- {
- snake.add(new Rectangle2D.Double(foodx,foody,8,8));
- flag = true;
- num++;
- score += time;
- time = 20;
- }
- for(int i=num;i>0;i--)
- snake.set(i,snake.get(i - 1));
- snake.set(0,new Rectangle2D.Double(x,y,8,8));
- }
- public boolean GameOver()
- {
- if(x<50 || x>440 || y<50 || y>340) return true;
- else return false;
- }
- class MyListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- repaint();
- }
- }
- class Listener implements KeyListener
- {
- public void keyTyped(KeyEvent e)
- {
- }
- public void keyPressed(KeyEvent e)
- {
- int key = e.getKeyCode(); // getKeyCode()
- if(key == e.VK_RIGHT && direction != 2) direction = 0; //right
- else if(key == e.VK_DOWN && direction != 3) direction = 1; //down
- else if(key == e.VK_LEFT && direction != 0) direction = 2; //left
- else if(key == e.VK_UP && direction != 1) direction = 3; //up
- }
- public void keyReleased(KeyEvent e)
- {
- }
- }
- private int foodx;
- private int foody;
- private int x = 70;
- private int y = 100;
- private int num = 3;
- private int delay =0;
- private int score = 0;
- private int time = 20;
- private int direction = 0;
- private boolean flag = true;
- private Color frontcolor = new Color(51,51,51);
- private Color backcolor = new Color(238,238,238);
- private Timer clock = new Timer(300,new MyListener());
- private ArrayList<Rectangle2D> snake = new ArrayList<Rectangle2D>();
- }