java新手,简单的吞食蛇游戏,一边写一边查书,写了半天才弄出来
没法把jar文件附在文章后面,想试试的人自己编译一下吧
有意见尽管提,高手多多指导,不过骂人就算了
[原文地址]
////////////////////////////
// 文件1
////////////////////////////
| import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class greedsnake implements keylistener{ public static final int canvaswidth = 200; public static final int nodewidth = 10; public greedsnake() { container cp = mainframe.getcontentpane(); paintcanvas = new canvas(); jpanel panelbuttom = new jpanel(); |
labelhelp = new jlabel("enter or r or s for start;", jlabel.center); mainframe.addkeylistener(this); public void keypressed(keyevent e){ if (keycode == keyevent.vk_r || |
begin(); public void keyreleased(keyevent e){ public void keytyped(keyevent e){ //draw background // draw the snake // draw the food private void drawnode(graphics g, node n){ void begin(){ |
(new thread(snakemodel)).start(); public static void main(string[] args){ |
///////////////////////////////////////////////////
// 文件2
///////////////////////////////////////////////////
import java.util.*; class snakemodel implements runnable{ int timeinterval = 200; // up and down should be even public snakemodel(greedsnake gs, int maxx, int maxy){ // initial matirx |
arrays.fill(matrix[i],false); // initial the snake food = createfood(); public void changedirection(int newdirection){ public boolean moveon(){ switch(direction){ if ((0 <= x && x < maxx) && (0 <= y && y < maxy)){ |
| 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(){ if(!paused){ private node createfood(){ |
random r = new random(); return new node(x,y); public void speedup(){ public void speeddown(){ public void changepausestate(){ public string tostring(){ class node{ |
闽公网安备 35060202000074号