网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 交易代码 > Java精品代码 > 正文

用java编写的小游戏源代码分析

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务

import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import java.util.*;
  
  public class GreedSnake implements KeyListener{
   JFrame mainFrame;
   Canvas paintCanvas;
   JLabel labelScore;
   SnakeModel snakeModel = null;
  
   public static final int canvasWidth = 200;
   public static final int canvasHeight = 300;
  
   public static final int nodeWidth = 10;
   public static final int nodeHeight = 10;
  
   public GreedSnake() {
    mainFrame = new JFrame("GreedSnake");
  
    Container cp = mainFrame.getContentPane();
   
    labelScore = new JLabel("Score:");
    cp.add(labelScore, BorderLayout.NORTH);
  
    paintCanvas = new Canvas();
    paintCanvas.setSize(canvasWidth+1,canvasHeight+1);
    paintCanvas.addKeyListener(this);
    cp.add(paintCanvas, BorderLayout.CENTER);
  
    JPanel panelButtom = new JPanel();
    panelButtom.setLayout(new BorderLayout());
    JLabel labelHelp;
    labelHelp = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER);
    panelButtom.add(labelHelp, BorderLayout.NORTH);
    labelHelp = new JLabel("ENTER or R or S for start;", JLabel.CENTER);
    panelButtom.add(labelHelp, BorderLayout.CENTER);
    labelHelp = new JLabel("SPACE or P for pause",JLabel.CENTER);
    panelButtom.add(labelHelp, BorderLayout.SOUTH);
    cp.add(panelButtom,BorderLayout.SOUTH);
  
    mainFrame.addKeyListener(this);
    mainFrame.pack();
    mainFrame.setResizable(false);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setVisible(true);
    begin();
   }
  
   public void keyPressed(KeyEvent e){
    int keyCode = e.getKeyCode();
    if (snakeModel.running)
     switch(keyCode){
      case KeyEvent.VK_UP:
       snakeModel.changeDirection(SnakeModel.UP);
       break;
      case KeyEvent.VK_DOWN:
       snakeModel.changeDirection(SnakeModel.DOWN);
       break;
      case KeyEvent.VK_LEFT:
       snakeModel.changeDirection(SnakeModel.LEFT);
       break;
      case KeyEvent.VK_RIGHT:
       snakeModel.changeDirection(SnakeModel.RIGHT);
       break;
      case KeyEvent.VK_ADD:
      case KeyEvent.VK_PAGE_UP:
       snakeModel.speedUp();
       break;
      case KeyEvent.VK_SUBTRACT:
      case KeyEvent.VK_PAGE_DOWN:
       snakeModel.speedDown();
       break;
      case KeyEvent.VK_SPACE:
      case KeyEvent.VK_P:
       snakeModel.changePauseState();
       break;
      default:
     }
  
    if (keyCode == KeyEvent.VK_R ||
      keyCode == KeyEvent.VK_S ||
      keyCode == KeyEvent.VK_ENTER){
     snakeModel.running = false;
     begin();
    }
   }
  
   public void keyReleased(KeyEvent e){
   }
  
   public void keyTyped(KeyEvent e){
   }
  
  
   void repaint(){
    Graphics g = paintCanvas.getGraphics();
  
    //draw background
    g.setColor(Color.WHITE);
    g.fillRect(0,0,canvasWidth,canvasHeight);
  
    // draw the snake
    g.setColor(Color.BLACK);
    LinkedList na = snakeModel.nodeArray;
    Iterator it = na.iterator();
    while(it.hasNext()){
     Node n = (Node)it.next();
     drawNode(g,n);
    }
  
    // draw the food
    g.setColor(Color.RED);
    Node n = snakeModel.food;
    drawNode(g,n);
    
    updateScore();
   }
  
   private void drawNode(Graphics g, Node n){
    g.fillRect(n.x*nodeWidth,
          n.y*nodeHeight,
          nodeWidth-1,
          nodeHeigh

  • 下一篇资讯: 使用非JAVA代码
  • 设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
    版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师