admin 管理员组文章数量: 887021
2024年3月1日发(作者:常量元素与微量元素速记口诀)
贪吃蛇:看了传智博客的视频整理出来的代码
Snake类
package snake;
import ;
import cs;
import ;
import t;
import List;
import ;
public class Snake {
//定义方向的常量
public static final int UP=-1;
public static final int DOWN=1;
public static final int LEFT=2;
public static final int RIGHT=-2;
private int oldDirection,newDirection;
private Point oldTail;
private boolean life;
private LinkedList
private Set
//蛇没身体 要初始化
public Snake(){
init();
}
public void init(){
//显示区最中间点
int x=/2;
int y=/2;
//初始化身体节点
for(int i=0;i<3;i++){
//添加节点
t(new Point(x--,y));//蛇头在右边 默认方向为右
oldDirection=newDirection=RIGHT;
life=true;
}
}
public void move(){
n("Snake's move");
if(!((oldDirection+newDirection)==0)){
oldDirection=newDirection;
}
//1. 去尾
oldTail=Last();
int x=st().x; //原坐标
int y=st().y;
switch(oldDirection){
case UP:
y--;
if(y<0){
y=-1;
}
break;
case DOWN:
y++;
if(y>=){
y=0;
}
break;
case LEFT:
x--;
if(x<0){
x=-1;
}
break;
case RIGHT:
x++;
if(x>=){
x=0;
}
break;
}
Point newHead=new Point(x,y);
//2. 加头
st(newHead);
}
public void changeDirection(int direction){
n("Snake's changeDirection");
/*if(!(direction+ion==0)){
ion=direction;
}*/
newDirection=direction;
}
public void die(){
life=false;
}
public void eatFood(){
n("Snake's eatFood");
t(oldTail);
}
public boolean isEatBody(){
n("Snake's isEatBody");
for(int i=1;i<();i++){
if((i).equals(d()))
return true;
}
return false;
}
//显示
public void drawMe(Graphics g){
n("Snake's drawMe");
or();//蛇身颜色
//遍历
for(Point p: body)
3DRect(p.x*_SIZE,
p.y*_SIZE,_SIZE , _SIZE,true);
}
//得到蛇头的方法
public Point getHead(){
return st();
}
//内部类
private class SnakeDriver implements Runnable{
}
public void run(){
while(life){
move();
for(SnakeListener l:listener){
ove();
}
try{
(1000);
}catch(InterruptedException e){
tackTrace();
}
}
}
}
//启动线程
public void start(){
new Thread(new SnakeDriver()).start();
}
public void addSnakeListener(SnakeListener l){
if(l!=null){
(l);
}
}
Food类
package snake;
import cs;
import ;
public class Food extends Point {
public void newFood(Point p){
ation(p);
}
public boolean isSnakeEatFood(Snake snake){
n("Food's isAnakeEatFood");
return (d());
//return false;
}
public void drawMe(Graphics g){
n("Food's drawMe");
//al(x*,
_SIZE,_SIZE);
3DRect(x*_SIZE, y*_SIZE,
_SIZE, true);
}
}
y*,
_SIZE,
Ground类
package snake;
import ;
import cs;
import ;
import ;
public class Ground {
private int[][] rocks=new int[][];
//生成墙
public Ground(){
for(int y=0;y<;y++){
rocks[0][y]=1;
rocks[-1][y]=1;
}
for(int x=0;x<;x++){
rocks[x][0]=1;
rocks[x][-1]=1;
}
}
public boolean isSnakeEatGround(Snake snake){
n("Ground's isSnakeEatGround ");
for(int x=0;x<;x++)
for(int y=0;y<;y++){
if(rocks[x][y]==1&&(x==d().x&&y==d().y)){//石头和蛇头重合
return true;
}
}
return false;
}
//获得随机位置
public Point getPoint(){
Random ran=new Random();
int x=0,y=0;
do{
x=t();
y=t();
}
while(rocks[x][y]==1);
return new Point(x,y);
}
public void drawMe(Graphics g){
n("Ground's drawMe");
or();
for(int x=0;x<;x++)
for(int y=0;y<;y++){
if(rocks[x][y]==1){
3DRect(x*_SIZE,
_SIZE, _SIZE, true);
}
}
}
}
GamePanel类
package snake;
import ;
import cs;
import ;
public class GamePanel extends JPanel {
private Snake snake;
private Food food;
private Ground ground;
public void display(Snake snake,Food food,Ground ground){
n("CamePanel's display");
=snake;
=food;
=ground;
t();
y*_SIZE,
}
protected void paintComponent(Graphics g) {
// TODO 自动生成的方法存根
//重新显示
omponent(g);
or(new Color(0xcfcfcf));
ct(0, 0, *_SIZE,
*_SIZE);
if(snake!=null&&food!=null&&ground!=null){
(g);
(g);
(g);
}
}
/*public void addSnakeListener(Controller controller) {
// TODO 自动生成的方法存根
}
*/
}
Controller类
package snake;
import ;
import pter;
import nt;
import ;
public class Controller extends KeyAdapter implements SnakeListener{
private Snake snake;
private Food food;
private Ground ground;
private GamePanel gamePanel;
public void keyPressed(KeyEvent e) {
// TODO 自动生成的方法存根
//ssed(e);
//转动方向
switch(Code()){
case _UP:
Direction();
break;
case _DOWN:
Direction();
break;
case _LEFT:
Direction();
break;
case _RIGHT:
Direction();
break;
}
}
public Controller(Snake snake, Food food, Ground ground, GamePanel gamePanel) {
super();
= snake;
= food;
= ground;
nel = gamePanel;
}
//逻辑
public void snakeMove(Snake snake) {
// TODO 自动生成的方法存根
if(eEatFood(snake)){
d();
d(nt());
}
//吃到石头
if(eEatGround(snake)){
();//让snake里线程退出
}
//吃到身体
if(ody()){
();
}
y(snake,food,ground);
}
}
public void newGame(){
();
d(nt());
}
SnakeListener接口
package snake;
public interface SnakeListener {
void snakeMove(Snake snake);
}
Global 类
package snake;
public class Global {
//格子
public static final int CELL_SIZE=20;
public static final int WIDTH=30;
public static final int HEIGHT=30;
}
TestMyGame 类
package snake;
import Layout;
import ;
public class TestMyGame {
public static void main(String[]args){
Snake snake=new Snake();
Food food=new Food();
Ground ground=new Ground();
GamePanel gamePanel=new GamePanel();
Controller controller=new Controller(
snake,food,ground,gamePanel);
JFrame frame=new JFrame();
aultCloseOperation(_ON_CLOSE);
e(*_SIZE+15,*_SIZE+38);
(gamePanel,);
e(*_SIZE,*_SIZE);
Listener(controller);
keListener(controller);
Listener(controller);
ible(true);
e();
}
}
版权声明:本文标题:Java编写的贪吃蛇游戏代码 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1709283170h541941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论