选择显示字体大小

源代码--java写小游戏,吞食蛇

java新手,简单的吞食蛇游戏,一边写一边查书,写了半天才弄出来

没法把jar文件附在文章后面,想试试的人自己编译一下吧

有意见尽管提,高手多多指导,不过骂人就算了

///////////////////////////////////////////////////
// 文件1
///////////////////////////////////////////////////


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,
               nodeheight-1);
  }
  
  public void updatescore(){
    string s = "score: " + snakemodel.score;
    labelscore.settext(s);
  }

  void begin(){
    if (snakemodel == null !snakemodel.running){
      snakemodel = new snakemodel(this,
                                  canvaswidth/nodewidth,
                                  canvasheight/nodeheight);
      (new thread(snakemodel)).start();
    }
  }

  public static void main(string[] args){
    greedsnake gs = new greedsnake();
  }
}

///////////////////////////////////////////////////
// 文件2
///////////////////////////////////////////////////

import java.util.*;
import javax.swing.*;

class snakemodel implements runnable{
  greedsnake gs;
  boolean[][] matrix;
  linkedlist nodearray = new linkedlist();
  node food;
  int maxx;
  int maxy;
  int direction = 2;
  boolean running = false;

  int timeinterval = 200;
  double speedchangerate = 0.75;
  boolean paused = false;
  
  int score = 0;
  int countmove = 0;

  // up and down should be even
  // right and left should be odd
  public static final int up = 2;
  public static final int down = 4;
  public static final int left = 1;
  public static final int right = 3;

  public snakemodel(greedsnake gs, int maxx, int maxy){
    this.gs = gs;
    this.maxx = maxx;
    this.maxy = maxy;

    // initial matirx
    matrix = new boolean[maxx][];
    for(int i=0; i<maxx; ++i){
      matrix[i] = new boolean[maxy];
      arrays.fill(matrix[i],false);
    }

    // initial the snake
    int initarraylength = maxx > 20 ? 10 : maxx/2;
    for(int i = 0; i < initarraylength; ++i){
      int x = maxx/2+i;
      int y = maxy/2;
      nodearray.addlast(new node(x, y));
      matrix[x][y] = true;
    }

    food = createfood();
    matrix[food.x][food.y] = true;
  }

  public void changedirection(int newdirection){
    if (direction % 2 != newdirection % 2){
      direction = newdirection;
    }
  }

  public boolean moveon(){
    node n = (node)nodearray.getfirst();
    int x = n.x;
    int y = n.y;

    switch(direction){
      case up:
        y--;
        break;
      case down:
        y++;
        break;
      case left:
        x--;
        break;
      case right:
        x++;
        break;
    }

    if ((0 <= x && x < maxx) && (0 <= y && y < maxy)){
      if (matrix[x][y]){
        if(x == food.x && y == food.y){
          nodearray.addfirst(food);
          
          int scoreget = (10000 - 200 * countmove) / timeinterval;
          score += scoreget > 0? scoreget : 10;
          countmove = 0;
          
          food = createfood();
          matrix[food.x][food.y] = true;
          return true;
        }
        else
          return false;
      }
      else{
        nodearray.addfirst(new node(x,y));
        matrix[x][y] = true;
        n = (node)nodearray.removelast();
        matrix[n.x][n.y] = false;
        countmove++;
        return true;
      }
    }
    return false;
  }

  public void run(){
    running = true;
    while (running){
      try{
        thread.sleep(timeinterval);
      }
      catch(exception e){
        break;
      }

      if(!paused){
        if (moveon()){
          gs.repaint();
        }
        else{
          joptionpane.showmessagedialog(
              null,
              &quot;you failed&quot;,
              &quot;game over&quot;,
              joptionpane.information_message);
          break;
        }
      }
    }
    running = false;
  }

  private node createfood(){
    int x = 0;
    int y = 0;
    do{
      random r = new random();
      x = r.nextint(maxx);
      y = r.nextint(maxy);
    }while(matrix[x][y]);

    return new node(x,y);
  }

  public void speedup(){
    timeinterval *= speedchangerate;
  }

  public void speeddown(){
    timeinterval /= speedchangerate;
  }

  public void changepausestate(){
    paused = !paused;
  }

  public string tostring(){
    string result = &quot;&quot;;
    for(int i=0; i<nodearray.size(); ++i){
      node n = (node)nodearray.get(i);
      result += &quot;[&quot; + n.x + &quot;,&quot; + n.y + &quot;]&quot;;
    }
    return result;
  }
}

class node{
  int x;
  int y;
  node(int x, int y){
    this.x = x;
    this.y = y;
  }
}


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons