学了集合 所以用集合做了个贪吃蛇 只完成了大致功能 材料是老师的俄罗斯方块 只要将俄罗斯方块的tetris.png和I.png拖到包内就能运行 但是在判断是否触墙时有点问题 而且感觉在用集合做添加cell时也没有特别方便,可能是结构安排有问题。
cell
-------------------------
package snake;
import java.awt.Image;
public class Cell {
private int row;
private int col;
private Image image;
public Cell(int row, int col, Image image) {
super();
this.row = row;
this.col = col;
this.image = image;
}
public Cell(){
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public void copyCell(Cell cell){
this.row = cell.row;
this.col = cell.col;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public String toSting(){
return "("+this.row+","+this.col+")"+" ";
}
}
snake
---------------------------------------------------------------------
package snake;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Paint;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import sun.net.www.content.image.gif;
public class Snake extends JPanel{
/**
* @param args
*/
public static final int ROWS=20;
public static final int COLS=10;
public static BufferedImage I;
public static BufferedImage bg;
public static final int CELL_SIZE=26;
public static int direction=1;
public int flag=0;
public static int score=0;
public boolean cantouchB=true;
public boolean str=false;
Cell[][] board = new Cell[COLS][ROWS];
private Timer timer;
static {
try {
bg=ImageIO.read(Snake.class.getResource("tetris.png"));
I=ImageIO.read(Snake.class.getResource("I.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ArrayList<Cell> list=new ArrayList<Cell>();
public Snake(){
list.add(new Cell(0,4,I));
list.add(new Cell(1,4,I));
list.add(new Cell(2,4,I));
list.add(new Cell(3,4,I));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf=new JFrame("贪吃蛇");
jf.setSize(530 , 580);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Snake snake=new Snake();
jf.add(snake);
snake.action();
ArrayList<Cell>list=new ArrayList<Cell>();
}
public void action(){
KeyListener listener=new KeyAdapter(){
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_LEFT&&direction!=2&&cantouchB==true){
direction=4;
move();
repaint();
}else if(e.getKeyCode()==KeyEvent.VK_DOWN&&direction!=3&&cantouchB==true){
direction=1;
move();
repaint();
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT&&direction!=4&&cantouchB==true){
direction=2;
move();
repaint();
}else if(e.getKeyCode()==KeyEvent.VK_UP&&direction!=1&&cantouchB==true){
direction=3;
move();
repaint();
}else if(e.getKeyCode()==KeyEvent.VK_Q){
System.exit(0);
}
}
};
this.addKeyListener(listener);
this.requestFocus();
timer=new Timer();
timer.schedule(new TimerTask(){
public void run() {
// TODO Auto-generated method stub
if(!eatItSelf()&&!outOfBound()){
move();
}else{
cantouchB=false;
}
repaint();
}
}, 700,700);
}
public void paint(Graphics g){
g.drawImage(bg, 0, 0, null);
g.translate(15, 15);
eatfood();
drawBorder(g);
paintSnake(g);
if(flag==0){
producefood();
}
paintfood(g);
g.setColor(Color.BLACK);
Font font=getFont();
font=new Font(font.getName(),font.BOLD,30);
g.setFont(font);
g.drawString("SCORE:"+score, 289, 160);
if(str){
font=new Font(font.getName(),font.BOLD,40);
g.drawString("perfect", 330,60);
str=false;
}
}
public boolean eatItSelf(){
for(int i=0;i<list.size()-1;i++){
Cell cell=list.get(i);
if(cell.getCol()==list.get(list.size()-1).getCol()&&
cell.getRow()==list.get(list.size()-1).getRow()){
return true;
}
}
return false;
}
public boolean outOfBound(){
if(list.get(list.size()-1).getCol()<0||
list.get(list.size()-1).getCol()+1>10||
list.get(list.size()-1).getRow()<0||
list.get(list.size()-1).getRow()+1>20){
return true;
}
return false;
}
private void paintSnake(Graphics g) {
for(Cell cell:list){
g.drawImage(cell.getImage(),cell.getCol()*CELL_SIZE,
cell.getRow()*CELL_SIZE,null);
}
}
public void producefood(){
Random random=new Random();
int x=random.nextInt(COLS);
int y=random.nextInt(ROWS);
board[x][y]=new Cell(x,y,I);
flag=1;
}
public void paintfood(Graphics g){
for(int x=0;x<COLS;x++){
for(int y=0;y<ROWS;y++){
if(board[x][y]!=null){
g.drawImage(board[x][y].getImage(),
x*CELL_SIZE,y*CELL_SIZE, null);
}
}
}
}
public void eatfood(){
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < ROWS; j++) {
if(board[i][j]!=null&&board[i][j].getCol()
==list.get(list.size()-1).getRow()&&board[i][j].getRow()==list.get(list.size()-1).getCol())
{
if(direction==1){
list.add(new Cell(list.get(list.size()-1).getRow()+1,list.get(list.size()-1).getCol(),
board[i][j].getImage()));
}else if(direction==2){
list.add(new Cell(list.get(list.size()-1).getRow(),list.get(list.size()-1).getCol()+1,
board[i][j].getImage()));
}else if(direction==3){
list.add(new Cell(list.get(list.size()-1).getRow()-1,list.get(list.size()-1).getCol(),
board[i][j].getImage()));
}else if(direction==4){
list.add(new Cell(list.get(list.size()-1).getRow(),list.get(list.size()-1).getCol()-1,
board[i][j].getImage()));
}
flag=0;
board[i][j]=null;
score++;
str=true;
}
}
}
}
public void move(){
for(int i=1;i<list.size();i++){
list.get(i-1).setCol(list.get(i).getCol());
list.get(i-1).setRow(list.get(i).getRow());
}
if(direction==1){
list.get(list.size()-1).setRow(list.get(list.size()-1).getRow()+1);
}else if(direction==2){
list.get(list.size()-1).setCol(list.get(list.size()-1).getCol()+1);
}else if(direction==3){
list.get(list.size()-1).setRow(list.get(list.size()-1).getRow()-1);
}else if(direction==4){
list.get(list.size()-1).setCol(list.get(list.size()-1).getCol()-1);
}
}
private void drawBorder(Graphics g) {
// TODO Auto-generated method stub
for(int i=0;i<COLS;i++){
for(int j=0;j<ROWS;j++){
g.drawRect(i*CELL_SIZE, j*CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
}
}