废话不多说,直接奉上代码:
Quadro.java
cobra pacote; importar java.awt.Graphics;importar java.awt.Menu;importar java.awt.MenuBar;importar java.awt.MenuItem;importar java.awt.event.ActionEvent;importar java.awt.event.ActionListener;importar java.awt importar java.awt.event.KeyListener; importar javax.swing.JFrame; public class Frame estende JFrame implements KeyListener { /** * */ Boolean isAlive; Booleano éPause; Painel de painel; Direção do personagem; privado estático final longo serialVersionUID = 1L; public Frame(){ // TODO stub do construtor gerado automaticamente setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300.300); addKeyListener(este); painel = novo Painel(); adicionar(painel); setVisible(verdadeiro); isAlive = verdadeiro; isPause = falso; direção = novo personagem('d'); MenuBar menuBar = new MenuBar(); Menu menu = new Menu("menu"); MenuItem reset = new MenuItem("novojogo"); MenuItem pausa= new MenuItem("pausa"); pause.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO stub de método gerado automaticamente if(!isPause) isPause= true; else isPause= false; } }); reset.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO stub de método gerado automaticamente reset(); } }); menu.add(redefinir); menu.add(pausa); menuBar.add(menu); setMenuBar(menuBar); } public void reset(){ painel.reset(); isAlive = verdadeiro; } @Override public void keyTyped(KeyEvent e) { // stub do método TODO gerado automaticamente } @Override public void keyPressed(KeyEvent e) { // stub do método TODO gerado automaticamente if(e.getKeyCode()==KeyEvent.VK_UP ) direção = 'w'; if(e.getKeyCode()==KeyEvent.VK_DOWN) direção = 's'; if(e.getKeyCode()==KeyEvent.VK_LEFT) direção = 'a'; if(e.getKeyCode()==KeyEvent.VK_RIGHT) direção = 'd'; } @Override public void keyReleased(KeyEvent e) { // TODO stub de método gerado automaticamente } public void paint(Graphics g){ panel.repaint(); } }Lançamento.java
cobra pacote; importar java.util.Timer; importar java.util.TimerTask; classe pública Launch estende TimerTask { Frame frame = new Frame(); public Launch() { // TODO stub do construtor gerado automaticamente } boolean crashWall(){ SnakeBody sb = frame.panel.snake.getFirst(); if((sb.x<0)||(sb.y<0)||(sb.x>=Panel.LINE)||(sb.y>=Panel.LINE)) retorna verdadeiro; caso contrário, retorne falso; } void inicial(){ frame.panel.snake.add(newBody()); frame.panel.food = newBody(); } @Override public void run() { // TODO stub de método gerado automaticamente if(frame.panel.snake.isEmpty()) inicial(); if(frame.isAlive) if(!frame.isPause){ if(goStraight()) frame.isAlive = false; frame.repaint(); } if(crashWall()) frame.isAlive = false; } SnakeBody newBody(){ SnakeBody sb = new SnakeBody(); sobreposição booleana = verdadeiro; while(sobreposição){ sobreposição =falso; sb.x = (int) (Math.random()*(Panel.LINE-2)+1); sb.y = (int) (Math.random()*(Panel.LINE-2)+1); if(!frame.panel.snake.isEmpty()) for(SnakeBody s : frame.panel.snake) if(sb.equals(s)) sobreposição =true; } return sb; } void comer(SnakeBody sb){ frame.panel.snake.addFirst(sb); } boolean goStraight(){ resultado booleano = false; SnakeBody sb =new SnakeBody(frame.panel.snake.getFirst()); frame.panel.snake.removeLast(); if(frame.direction=='w') sb.turnUp(); if(frame.direction=='s') sb.turnDown(); if(frame.direction=='a') sb.turnLeft(); if(frame.direction=='d') sb.turnRight(); for(SnakeBody s: frame.panel.snake){ if(sb.equals(s)) resultado = verdadeiro; } frame.panel.snake.addFirst(sb); if(sb.equals(frame.panel.food)){ if(frame.direction=='w') frame.panel.food.turnUp(); if(frame.direction=='s') frame.panel.food.turnDown(); if(frame.direction=='a') frame.panel.food.turnLeft(); if(frame.direction=='d') frame.panel.food.turnRight(); comer(frame.panel.food); frame.panel.food = newBody(); } retornar resultado; } public static void main(String[] args){ // TODO stub de método gerado automaticamente Launch timertask = new Launch(); Temporizador temporizador = novo Temporizador(); timer.schedule(timertask,0,500); } }Painel.java
cobra pacote; importar java.awt.Color;importar java.awt.Graphics;importar java.util.LinkedList; importar javax.swing.JPanel; public class Painel estende JPanel { /** * */ private static final long serialVersionUID = 1L; public LinkedList<SnakeBody> cobra = new LinkedList<SnakeBody>(); final estático int LINHA = 10; Comida SnakeBody = novo SnakeBody(-99,-99); public Panel() { // TODO stub do construtor gerado automaticamente } public void reset(){ snake.clear(); } public void paint(Gráficos g){ g.setColor(Color.white); g.fillRect(0, 0, getWidth(), getHeight()); for(SnakeBody sb: cobra){ g.setColor(Color.black); g.drawRect(sb.x*getWidth()/LINE,sb.y*getHeight()/LINE,getWidth()/LINE,getHeight()/LINE); g.setColor(Color.orange); g.fillRect(sb.x*getWidth()/LINE,sb.y*getHeight()/LINE,getWidth()/LINE,getHeight()/LINE); } g.setColor(Color.red); g.fillRect(food.x*getWidth()/LINE,food.y*getHeight()/LINE,getWidth()/LINE,getHeight()/LINE); }}SnakeBody.java
cobra pacote; classe SnakeBody { int x; interno; public SnakeBody() { // TODO stub do construtor gerado automaticamente x = 0; y = 0; } public SnakeBody(int a,int b){ x = a; y = b; } public SnakeBody(SnakeBody sb){ this(sb.x,sb.y); } public void turnUp(){ y--; } public void turnDown(){ y++; } public void turnLeft(){ x--; } public void turnRight(){ x++; } boolean equals(SnakeBody s){ if((x==sx)&&(y==sy)) return true; caso contrário, retorne falso; } }以上所述就是本文给大家分享的贪吃蛇的全部代码了,希望能够对大家熟练掌握java有所帮助。