分享
 
 
 

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

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

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,

"you failed",

"Game Over",

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()

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有