熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

源代碼--Java寫小游戲,吞食蛇

2022-06-13   來源: Java核心技術 

  java新手簡單的吞食蛇游戲一邊寫一邊查書寫了半天才弄出來
  
  沒法把jar文件附在文章後面想試試的人自己編譯一下吧
  
  有意見盡管提高手多多指導不過罵人就算了
  
  ///////////////////////////
  // 文件
  //////////////////////////
  
  import javaawt*;
  import javaawtevent*;
  import javaxswing*;
  import javautil*;
  
  public class GreedSnake implements KeyListener{
  JFrame mainFrame;
  Canvas paintCanvas;
  JLabel labelScore;
  SnakeModel snakeModel = null;
  
  public static final int canvasWidth = ;
  public static final int canvasHeight = ;
  
  public static final int nodeWidth = ;
  public static final int nodeHeight = ;
  
  public GreedSnake() {
  mainFrame = new JFrame(GreedSnake);
  
  Container cp = mainFramegetContentPane();
  
  labelScore = new JLabel(Score:);
  cpadd(labelScore BorderLayoutNORTH);
  
  paintCanvas = new Canvas();
  paintCanvassetSize(canvasWidth+canvasHeight+);
  paintCanvasaddKeyListener(this);
  cpadd(paintCanvas BorderLayoutCENTER);
  
  JPanel panelButtom = new JPanel();
  panelButtomsetLayout(new BorderLayout());
  JLabel labelHelp;
  labelHelp = new JLabel(PageUp PageDown for speed; JLabelCENTER);
  panelButtomadd(labelHelp BorderLayoutNORTH);
  labelHelp = new JLabel(ENTER or R or S for start; JLabelCENTER);
  panelButtomadd(labelHelp BorderLayoutCENTER);
  labelHelp = new JLabel(SPACE or P for pauseJLabelCENTER);
  panelButtomadd(labelHelp BorderLayoutSOUTH);
  cpadd(panelButtomBorderLayoutSOUTH);
  
  mainFrameaddKeyListener(this);
  mainFramepack();
  mainFramesetResizable(false);
  mainFramesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
  mainFramesetVisible(true);
  begin();
  }
  
  public void keyPressed(KeyEvent e){
  int keyCode = egetKeyCode();
  if (snakeModelrunning)
  switch(keyCode){
  case KeyEventVK_UP:
  snakeModelchangeDirection(SnakeModelUP);
  break;
  case KeyEventVK_DOWN:
  snakeModelchangeDirection(SnakeModelDOWN);
  break;
  case KeyEventVK_LEFT:
  snakeModelchangeDirection(SnakeModelLEFT);
  break;
  case KeyEventVK_RIGHT:
  snakeModelchangeDirection(SnakeModelRIGHT);
  break;
  case KeyEventVK_ADD:
  case KeyEventVK_PAGE_UP:
  snakeModelspeedUp();
  break;
  case KeyEventVK_SUBTRACT:
  case KeyEventVK_PAGE_DOWN:
  snakeModelspeedDown();
  break;
  case KeyEventVK_SPACE:
  case KeyEventVK_P:
  snakeModelchangePauseState();
  break;
  default:
  }
  
  if (keyCode == KeyEventVK_R ||
  keyCode == KeyEventVK_S ||
  keyCode == KeyEventVK_ENTER){
  snakeModelrunning = false;
  begin();
  }
  }
  
  public void keyReleased(KeyEvent e){
  }
  
  public void keyTyped(KeyEvent e){
  }
  
  
  void repaint(){
  Graphics g = paintCanvasgetGraphics();
  
  //draw background
  gsetColor(ColorWHITE);
  gfillRect(canvasWidthcanvasHeight);
  
  // draw the snake
  gsetColor(ColorBLACK);
  LinkedList na = snakeModelnodeArray;
  Iterator it = erator();
  while(ithasNext()){
  Node n = (Node)itnext();
  drawNode(gn);
  }
  
  // draw the food
  gsetColor(ColorRED);
  Node n = snakeModelfood;
  drawNode(gn);
  
  updateScore();
  }
  
  private void drawNode(Graphics g Node n){
  gfillRect(nx*nodeWidth
  ny*nodeHeight
  nodeWidth
  nodeHeight);
  }
  
  public void updateScore(){
  String s = Score: + snakeModelscore;
  labelScoresetText(s);
  }
  
  void begin(){
  if (snakeModel == null || !snakeModelrunning){
  snakeModel = new SnakeModel(this
  canvasWidth/nodeWidth
  canvasHeight/nodeHeight);
  (new Thread(snakeModel))start();
  }
  }
  
  public static void main(String[] args){
  GreedSnake gs = new GreedSnake();
  }
  }
  
  ///////////////////////////////////////////////////
  // 文件
  ///////////////////////////////////////////////////
  
  import javautil*;
  import javaxswing*;
  
  class SnakeModel implements Runnable{
  GreedSnake gs;
  boolean[][] matrix;
  LinkedList nodeArray = new LinkedList();
  Node food;
  int maxX;
  int maxY;
  int direction = ;
  boolean running = false;
  
  int timeInterval = ;
  double speedChangeRate = ;
  boolean paused = false;
  
  int score = ;
  int countMove = ;
  
  // UP and DOWN should be even
  // RIGHT and LEFT should be odd
  public static final int UP = ;
  public static final int DOWN = ;
  public static final int LEFT = ;
  public static final int RIGHT = ;
  
  public SnakeModel(GreedSnake gs int maxX int maxY){
  thisgs = gs;
  thismaxX = maxX;
  thismaxY = maxY;
  
  // initial matirx
  matrix = new boolean[maxX][];
  for(int i=; i<maxX; ++i){
  matrix[i] = new boolean[maxY];
  Arraysfill(matrix[i]false);
  }
  
  // initial the snake
  int initArrayLength = maxX > ? : maxX/;
  for(int i = ; i < initArrayLength; ++i){
  int x = maxX/+i;
  int y = maxY/;
  nodeArrayaddLast(new Node(x y));
  matrix[x][y] = true;
  }
  
  food = createFood();
  matrix[foodx][foody] = true;
  }
  
  public void changeDirection(int newDirection){
  if (direction % != newDirection % ){
  direction = newDirection;
  }
  }
  
  public boolean moveOn(){
  Node n = (Node)nodeArraygetFirst();
  int x = nx;
  int y = ny;
  
  switch(direction){
  case UP:
  y;
  break;
  case DOWN:
  y++;
  break;
  case LEFT:
  x;
  break;
  case RIGHT:
  x++;
  break;
  }
  
  if (( <= x && x < maxX) && ( <= y && y < maxY)){
  if (matrix[x][y]){
  if(x == foodx && y == foody){
  nodeArrayaddFirst(food);
  
  int scoreGet = ( * countMove) / timeInterval;
  score += scoreGet > ? scoreGet : ;
  countMove = ;
  
  food = createFood();
  matrix[foodx][foody] = true;
  return true;
  }
  else
  return false;
  }
  else{
  nodeArrayaddFirst(new Node(xy));
  matrix[x][y] = true;
  n = (Node)nodeArrayremoveLast();
  matrix[nx][ny] = false;
  countMove++;
  return true;
  }
  }
  return false;
  }
  
  public void run(){
  running = true;
  while (running){
  try{
  Threadsleep(timeInterval);
  }
  catch(Exception e){
  break;
  }
  
  if(!paused){
  if (moveOn()){
  gsrepaint();
  }
  else{
  JOptionPaneshowMessageDialog(
  null
  you failed
  Game Over
  JOptionPaneINFORMATION_MESSAGE);
  break;
  }
  }
  }
  running = false;
  }
  
  private Node createFood(){
  int x = ;
  int y = ;
  do{
  Random r = new Random();
  x = rnextInt(maxX);
  y = rnextInt(maxY);
  }while(matrix[x][y]);
  
  return new Node(xy);
  }
  
  public void speedUp()
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27025.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.