前言:上周偶然关注了下WCG2011,很久没有关注这个了,很遗憾sky没有夺冠,毕竟从学校就开始关注;也很遗憾fly100%没有加冕,有实力;也相当遗憾,moon没有进半决赛。而是韩国兽王lyn登顶。看着视频,想打几把,周末和朋友high了下,操作依旧那么飘逸,呵呵~。不知不觉中,有点想写游戏程序的想法,来点带动感的程序,于是花了半天写了这个简易游戏。
代码及注释如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Snake extends JFrame implements KeyListener{
private int direction,snakeLength = 6,positionX = 5,positionY = 6,foodX,foodY,queueLength;
private int [] savePositionX = new int [20];//存放snakeLength长度的positionX值
private int [] savePositionY = new int [20];//存放snakeLength长度的positionY值
private boolean start = true;
private final int EAST = 1, WEST = 2, SOUTH = 3, NORTH = 4;
public Snake(){
super("贪吃蛇 by envi");
setSize(500,500);//设置窗口大小
setVisible(true);//设定窗口属性
addKeyListener(this);//添加按键监听
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
queueLength = snakeLength;//
direction = EAST;//方向初始化的设置
for(int count = 0; count < snakeLength; count++){
savePositionX[count] = positionX++;
savePositionY[count] = positionY;
}
locateFood();
while(start == true){
startToRun();
}
//匿名类用于注册监听器
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}//Snake()
//按键与方向选择
public void keyPressed(KeyEvent event){
switch(event.getKeyCode()){
case KeyEvent.VK_UP:
if(direction!=SOUTH)
direction = NORTH;
break;
case KeyEvent.VK_DOWN:
if(direction!=NORTH)
direction = SOUTH;
break;
case KeyEvent.VK_LEFT:
if(direction!=EAST)
direction = WEST;
break;
case KeyEvent.VK_RIGHT:
if(direction!=WEST)
direction = EAST;
break;
}
}
public void keyReleased(KeyEvent event) {
}
public void keyTyped(KeyEvent event) {
}
//获取按键后的坐标
public void getSnakePosition(){
if(start == true){
switch(direction){
case EAST:
positionX++;
break;
case WEST:
positionX--;
break;
case SOUTH:
positionY++;
break;
case NORTH:
positionY--;
break;
}
//每前进一步 坐标均有改变
queueLength++;
savePositionX[queueLength] = positionX;
savePositionY[queueLength] = positionY;
}
}
//随机数据产生food的坐标位置
public void locateFood(){
Random random = new Random();
foodX = random.nextInt(20);
foodY = random.nextInt(20);
}
//贪吃蛇获取食物
public void getFood(){
if(foodX == positionX && foodY == positionY){
if(snakeLength < 20){
snakeLength++;
}
locateFood();//吃到食物后,重新随机生成食物坐标点
}
try{
Thread.sleep(100);
}
catch(InterruptedException e){}//延迟
}
//运行
public void startToRun(){
getSnakePosition();//蛇头前进位置
try{
Thread.sleep(150);
}
catch(InterruptedException e){}//延迟
getFood();
//超出实际长度 消除尾影,更新蛇身
System.out.println("queueLength "+ queueLength +" snakeLength "+ snakeLength);
//其实此时的queueLength等于snakeLength + 1
if(queueLength > snakeLength){
for(int i = 1; i <= queueLength; i++){
savePositionX[i-1] = savePositionX[i];//更新坐标,保持显示snakeLength个蛇身格子
savePositionY[i-1] = savePositionY[i];
}
queueLength = snakeLength;//更新长度
}
//更新绘图 repaint()是回调函数
repaint();
}
public void paint(Graphics g){
super.paint(g);
//绘制蛇身
g.setColor(Color.BLACK);
for(int temp = 0; temp < snakeLength; temp++){
//g.fillRect(25*savePositionX[temp] + 5, 25*savePositionY[temp] + 5, 24, 24);
if(temp == 0){
g.fillRoundRect(25*savePositionX[temp] + 5, 25*savePositionY[temp] + 5, 24, 24, 360, 360);
}
else if(temp == snakeLength - 1){
g.fillRoundRect(25*savePositionX[temp] + 5, 25*savePositionY[temp] + 5, 24, 24, 10, 10);
}
else{
g.fill3DRect(25*savePositionX[temp] + 5, 25*savePositionY[temp] + 5, 24, 24, true);
}
}
//绘制随机食物点
g.setColor(Color.RED);
g.fillRect(25*foodX + 5, 25*foodY + 5, 24, 24);
}
//主函数
public static void main(String args[]){
new Snake();//通过匿名类加载其构造函数
}
}
后记:实现基本算法,但设计过于简单,而且按键滞后,用线程会有所改善,有空再完善。