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(){
timeInterval *= speedChangeRate;
}
public void speedDown(){
timeInterval /= speedChangeRate;
}
public void changePauseState(){
paused = !paused;
}
public String toString(){
String result = "";
for(int i=0; i<nodeArray.size(); ++i){
Node n = (Node)nodeArray.get(i);
result += "[" + n.x + "," + n.y + "]";
}
return result;
}
}
class Node{
int x;
int y;
Node(int x, int y){
this.x = x;
this.y = y;
}
}
温馨提示:内容为网友见解,仅供参考
求贪吃蛇java程序代码(要能运行的,有完整注释的)
matrix[i]=new boolean[maxY];\/\/将矩阵的每一行定义成列的集合 Arrays.fill(matrix[i], false);\/\/\/使用java.util.Arrays的static方法fill,将matrix[]数组里面的元素全部定义成false \/\/至此,矩阵里面所有的点的boolean值都是flase \/\/for(int j=0;j<maxY;j++){ \/\/matrix[i][j]=false; \/\/} } \/\/\/ ...
求JAVA贪吃蛇代码 要能运行有注解的!
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 canva...
求"贪吃蛇"小游戏JAVA源代码一份
贪吃蛇 import java.awt.*;import java.awt.event.*;public class GreedSnake \/\/主类 { \/ param args \/ public static void main(String[] args) { \/\/ TODO Auto-generated method stub new MyWindow();} } class MyPanel extends Panel implements KeyListener,Runnable\/\/自定义面板类,继承了...
我想求一个Java编写的贪吃蛇游戏,要有注释和流程图最好
import java.util.Arrays;import java.util.LinkedList;import java.util.Observable;import java.util.Random;\/** * 游戏的Model类,负责所有游戏相关数据及运行 *\/class SnakeModel extends Observable implements Runnable { boolean[][] matrix; \/\/ 指示位置上有没蛇体或食物 LinkedList nodeArray = new LinkedList...
谁帮我写个java贪吃蛇游戏全面的包括得分等功能!谢谢!希望大家帮帮忙...
"\\n3 :按暂停键可以暂停游戏,再按暂停键能继续玩游戏."+ "\\n4 :黄色为普通食物,吃一个得100分."+ "\\n5 :青色为穿身宝物,吃一个得100分,该宝物允许玩家穿过一次蛇身"+ "\\n6 :红色为穿墙宝物,吃一个得200分,该宝物允许玩家穿过一次墙壁."+ "\\n7 :当分数到达一定时,会自动升级.当...
帮忙给个java小游戏源程序
\/***贪食蛇游戏,注释简单点啦 而且很多功能没有完善了***\/import java.awt.*;import java.awt.event.*;import javax.swing.*;class Snake extends JFrame implements ActionListener, KeyListener, Runnable { JMenuBar bar; JMenu menu1; JMenu menu2; JMenu menu3; JMenu menu4; JMenuItem menuitem1;...
用Java逻辑思维,前十章做一个小游戏。可以在MyEclipse8.5运行的...
贪吃蛇游戏!package com.hbsi;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;\/\/Main Class public class GreedSnake extends KeyAdapter{ JFrame mainFrame;Canvas paintCanvas;JLabel labelScore;\/\/计分牌 SnakeModel snakeModel=null;\/\/ 蛇 public static ...
谁会用java编写“贪吃蛇”小游戏
汗 要程序怎么这么点分啊 哭了 呵呵 不过你还是挺幸运 给你吧 连连看的代码(基本算法)加了部分注释 import java.awt.*;import java.awt.event.*;public class lianliankan implements ActionListener { static String s="no"; \/\/用来纪录点击按钮的信息 int x0=0,y0=0,x=0,y=0,n1=0,n2=...
求大神给一份JAVA小游戏,贪吃蛇或者俄罗斯方块源代码。my eclipse能直接...
import javax.swing.*;import java.awt.*;import java.awt.event.*;class Game extends JFrame implements KeyListener,Runnable { Thread Down10 = new Thread(this);GameCanvas test = new GameCanvas();JLabel LabelTetris = new JLabel("Tetris Game");JLabel ScorePrint1 = new JLabel("...
哪位能告诉我贪吃蛇游戏的全部代码?
boolean out = false;\/\/没开始运行? boolean run = false;\/\/暂停运行 String JiBie = "中级"; JFrame f = new JFrame("贪吃蛇 V1.0"); JPanel show = new JPanel(); JLabel Message = new JLabel("级别:中级 蛇长:5 时间500ms 分数:00"); \/\/ JButton play = new JButton("开始"); JLab...