用JAVA写的贪吃蛇,Eclipse平台,2个类。内容入下:
class snake:
-----------------------
package tanchishe;
import java.awt.Color;
import java.awt.Graphics;
public class Snake {
private GamePanel panel = null;
int x = 0;
int y = 0;
public Snake(int x, int y, GamePanel panel) {
this.x = x;
this.y = y;
this.panel = panel;
}
// 画蛇身
void draw(Graphics g) {
g.setColor(new Color(255, 255, 255));
g.fillRect(x * 20, y * 20, 19, 19);
}
// 画食物
void drawfood(Graphics g) {
g.setColor(new Color(255, 255, 255,255));
g.fillOval(x * 20+5, y * 20+5, 10, 10);
}
// 画蛇头
void drawhead(Graphics g) {
g.setColor(new Color(255, 0, 0));
g.fillRect(x * 20, y * 20, 19, 19);
}
// 蛇头移动
void move(boolean dir, int step) {
if (dir) {
x+=step;
}else {
y+=step;
}
panel.repaint();
}
//蛇移动
public void movebody(Boolean dir, int step) {
// 蛇头移动
panel.snakehead.move(dir, step);
// 蛇身移动
for (int i = 1; i < panel.snakes.size(); i++) {
panel.snakes.get(i).x = panel.snakeroad.get(panel.snakeroad.size()-i).x;
panel.snakes.get(i).y = panel.snakeroad.get(panel.snakeroad.size()-i).y;
}
panel.repaint();
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
----------------------