【文件属性】:
文件名称:自己用java制作贪吃蛇小游戏
文件大小:6KB
文件格式:JAVA
更新时间:2011-11-10 13:38:21
java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class snate extends JFrame implements KeyListener,Runnable
{
JLabel j;
Canvas j1;
public static final int canvasWidth = 200;
public static final int canvasHeight = 300;
public static final int nodeWidth = 10;
public static final int nodeHeight = 10;
//SnakeModel se=null;
//222222
//
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;
snate()
{
super();
//setSize(500,400);
Container c=getContentPane();
j=new JLabel("Score:");
c.add(j,BorderLayout.NORTH);
j1=new Canvas();
j1.setSize(canvasWidth+1,canvasHeight+1);
j1.addKeyListener(this);
c.add(j1,BorderLayout.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
JLabel j2;
j2 = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER);
p1.add(j2, BorderLayout.NORTH);
j2 = new JLabel("ENTER or R or S for start;", JLabel.CENTER);
p1.add(j2, BorderLayout.CENTER);
j2 = new JLabel("SPACE or P for pause",JLabel.CENTER);
p1.add(j2, BorderLayout.SOUTH);
c.add(p1,BorderLayout.SOUTH);
addKeyListener(this);
pack();
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// begin();
//
//2222222
//
this.gs = gs;
this.maxX = maxX;
this.maxY = maxY;
// initial matirx
matrix = new boolean[maxX][];
for(int i=0; i 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 keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
{
//se.changeDirection(SnakeModel.UP);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
//se.changeDirection(SnakeModel.DOWN);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
//se.changeDirection(SnakeModel.LEFT);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
//se.changeDirection(SnakeModel.RIGHT);
}
if(e.getKeyCode()==KeyEvent.VK_R||e.getKeyCode()==KeyEvent.VK_S||e.getKeyCode()==KeyEvent.VK_ENTER)
{
}
}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{}
public void repaint()
{
Graphics g = j1.getGraphics();
//背景
g.setColor(Color.red);
g.fillRect(0,0,canvasWidth,canvasHeight);
//蛇
//g.setColor(Color.BLUE);
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillRect(10,10,10,10);
}
//
//222222
//
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