java

位置:IT落伍者 >> java >> 浏览文章

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


发布日期:2019年06月07日
 
源代码--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()               

上一篇:解决java网络编程IPv6带来的问题方法

下一篇:如何用javac 和java 编译运行整个Java工程