吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6981|回复: 16
收起左侧

[Java 转载] java 实现贪吃蛇 王校长吃面包版

  [复制链接]
20175101019 发表于 2019-2-16 13:09
[Java] 纯文本查看 复制代码
package 贪吃蛇带音乐版;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;


public class Game extends JFrame implements KeyListener,Runnable{
	private int score=0;
	private int leftX=0;
	private int leftY=0;
	private int overtime=500;
	private int counttime=0;
	private int headX=2;
	private int headY=2;   
	private int flag=1;
	private int width=0;
	private int height=0;
	private int [][] map=null;
	private int head_dir=4;
	private Image [] image=null;
	private ArrayList<node> snake=new ArrayList<node>();
	Music music;
	Thread t;
	
	final int wall=1,space=0,food=2,
			HEADUP=3,HEADDOWN=4,HEADLEFT=5,HEADRIGHT=6,
			BODYUP=7,BODYDOWN=8,BODYLEFT=9,BODYRIGHT=10,
			UP=1,DOWN=2,LEFT=3,RIGHT=4;
	
	Game(){
		super("贪吃蛇带音乐版");
		this.setVisible(true);
		this.setSize(600, 600);
		setResizable(false);
	
		setLocation(800, 20);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		this.setBackground(Color.YELLOW);
		Container con=this.getContentPane();
		con.setLayout(null);
//		con.setBackground(Color.white);
    	this.setBackground(Color.yellow);
		
		getImage();
		
		
		width=this.getWidth();
		height=this.getHeight();
		this.setFocusable(true);
		
		initMap();

		this.addKeyListener(this);
		
		
//		repaint();
		
		
		
		t=new Thread(this);
		t.start();
		music=new Music("G:\\eclipse安装包\\Java工程文件\\贪吃蛇带音乐版\\素材\\音乐素材\\初音宝宝 - 甩葱歌.mp3");
		music.loop();
	}
	void getImage() {
		image=new Image[11];
		for(int i=0;i<11;i++)
		{
			image[i]=Toolkit.getDefaultToolkit().getImage("G:\\eclipse安装包\\Java工程文件\\贪吃蛇带音乐版\\素材\\图像素材\\snake"+i+".jpg");
		}
	}
	void get_headXY(){
		int i=1;
		int j=1;
		
		for(;i<11;i++) {
			for(j=1;j<11;j++)
			{
				
				if(map[i][j]==HEADUP||map[i][j]==HEADDOWN||map[i][j]==HEADLEFT||map[i][j]==HEADRIGHT)
				{
					headX=i;
					headY=j;
					
					snake.add(new node(headX,headY,map[i][j]));
					
					break;
				}
			}
		}
		
	}

	
	public void paint(Graphics g) {
//		System.out.println("我被调用了");
		int i=0;
		int j=0;
		printMap();
		for(i=0;i<12;i++) {
			for(j=0;j<12;j++) {
				g.drawImage(image[map[i][j]], leftX+j*30, leftY+i*30,30,30,this);
			}
		}
		g.clearRect(120, 60 , 360, 40);
		g.setColor(Color.RED);
		g.setFont(new Font("楷体_2312",Font.BOLD,30));
		g.drawString("现在分数:", 180, 90);
		g.drawString("得分为:"+String.valueOf(score),300,90);
	}
	
	public void initMap() {
		map=new Map().getMap();
		int i=0;
		int j=0;
//		
		snake.clear();
		head_dir=4;
		flag=1;
		int [][] tem=map;
		
		for(i=0;i<tem.length;i++)
		{
			
			for(j=0;j<tem[i].length;j++)
				System.out.print(tem[i][j]+" ");
			System.out.println();
		}
//		System.out.println("map___________________________________________________________________");
//		for(;i<map.length;i++)
//		{
//			
//			for(j=0;j<map[i].length;j++)
//				System.out.print(map[i][j]+" ");
//			System.out.println();
//		}
		
		get_left_XY();
		get_headXY();
		
	}
	
	

	private void get_left_XY() {
		// TODO Auto-generated method stub
		leftX=(width-map[0].length*30)/2;
		leftY=(height-map.length*30)/2;
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Game();

	}







	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true) {
			if(counttime<overtime) {
				counttime+=50;
			}
			else
			{
				counttime=0;
				move();
				repaint();
				
			}
			try {
				if(flag==2)
				{
					judge();
				}
				else
				{
					Thread.sleep(50);
				}
			}
			catch(InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.getKeyCode()==KeyEvent.VK_A)
		{
			if(overtime>200)
			overtime-=100;
		}
		
		if(e.getKeyCode()==KeyEvent.VK_D) {
			if(overtime<1000)
				overtime+=100;
		}
		if(e.getKeyCode()==KeyEvent.VK_UP)
		{
			if(map[snake.get(0).x][snake.get(0).y]==HEADDOWN)
				return;
			head_dir=UP;	
		}
		if(e.getKeyCode()==KeyEvent.VK_DOWN)
		{
			System.out.println(snake.size());
			System.out.println(snake.get(0).x+snake.get(0).y);
			if(map[snake.get(0).x][snake.get(0).y]==HEADUP)
				return;
			head_dir=DOWN;	
		}
		if(e.getKeyCode()==KeyEvent.VK_LEFT)
		{
			if(map[snake.get(0).x][snake.get(0).y]==HEADRIGHT)
				return;
			head_dir=LEFT;	
		}
		if(e.getKeyCode()==KeyEvent.VK_RIGHT)
		{
			if(map[snake.get(0).x][snake.get(0).y]==HEADLEFT)
				return;
			head_dir=RIGHT;	
		}
//		repaint();
		if(flag==2){
			String msg = "游戏结束,你的得分是"+score+"\n是否重来?";
			int type = JOptionPane.YES_NO_OPTION;
			String title = "Game Over";
			int choice = 0;
			choice = JOptionPane.showConfirmDialog(null, msg,title,type);
			if(choice==1){
				System.exit(0);
			}else if(choice == 0){
				initMap();
				score=0;
				
				counttime = 0;
				overtime = 500;
				repaint();
				
		    }
		}
	}
	public void move() {
		ArrayList<node> temp=new ArrayList<node>();
		int i=0;
		if(snake.size()==0)
			return ;
		else
		{
			System.out.println("蛇的大小:"+snake.size());
			int x=snake.get(0).x;
			int y=snake.get(0).y;
			switch(head_dir) {
			case UP   :x=x-1 ;break;
			case DOWN :x=x+1 ;break;
			case LEFT :y=y-1 ;break;
			case RIGHT:y=y+1 ;break;
			}
			
			if(map[x][y]==wall||map[x][y]==BODYUP||map[x][y]==BODYDOWN||map[x][y]==BODYLEFT||map[x][y]==BODYRIGHT)
			{
				flag=2;
				
			}
				
			else 
				if(map[x][y]==food)
				{
					score++;
					resetFood();
					switch(head_dir) {
					case UP:
						temp.add(new node(x,y,HEADUP));
					    map[x][y]=HEADUP;
					    break;
					case DOWN:
						temp.add(new node(x,y,HEADDOWN));
					    map[x][y]=HEADDOWN;
					    break;
					case LEFT:
						temp.add(new node(x,y,HEADLEFT));
					    map[x][y]=HEADLEFT;
					    break;
					case RIGHT:
						temp.add(new node(x,y,HEADRIGHT));
					    map[x][y]=HEADRIGHT;
					    break;
					}
					
					for(i=0;i<snake.size();i++)
					{
						if(i==0) {
							switch(snake.get(i).dir) {
							case HEADUP   :snake.get(i).dir=BODYUP     ;break;
							case HEADDOWN :snake.get(i).dir=BODYDOWN   ;break;
							case HEADLEFT :snake.get(i).dir=BODYLEFT   ;break;
							case HEADRIGHT:snake.get(i).dir=BODYRIGHT  ;break;
							}
						}
						temp.add(snake.get(i));
						map[snake.get(i).x][snake.get(i).y] = snake.get(i).dir;
					}
					snake=temp;
				}
				else
				{
					switch(head_dir) {
					case UP:
						temp.add(new node(x,y,HEADUP));
					    map[x][y]=HEADUP;
					    break;
					case DOWN:
						temp.add(new node(x,y,HEADDOWN));
					    map[x][y]=HEADDOWN;
					    break;
					case LEFT:
						temp.add(new node(x,y,HEADLEFT));
					    map[x][y]=HEADLEFT;
					    break;
					case RIGHT:
						temp.add(new node(x,y,HEADRIGHT));
					    map[x][y]=HEADRIGHT;
					    break;
					}
					for(i=0;i<snake.size()-1;i++)
					{
						if(i==0) {
							switch(snake.get(i).dir) {
							case HEADUP   :snake.get(i).dir=BODYUP     ;break;
							case HEADDOWN :snake.get(i).dir=BODYDOWN   ;break;
							case HEADLEFT :snake.get(i).dir=BODYLEFT   ;break;
							case HEADRIGHT:snake.get(i).dir=BODYRIGHT  ;break;
							}
						}
						temp.add(snake.get(i));
						map[snake.get(i).x][snake.get(i).y]=snake.get(i).dir;
					}
					map[snake.get(snake.size()-1).x][snake.get(snake.size()-1).y]=0;
					snake=temp;
				}
		}
		
		
			
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void printMap() {
		int i;
		int j;
		for(i=0;i<map.length;i++) {
			for(j=0;j<map[0].length;j++)
			{
				System.out.print(map[i][j]+" ");
			}
			System.out.println();
		}
		System.out.println("______________________________________________");
		
	}
	public void resetFood()
	{
		int i;
		int j;
		do {
			i=(int)(Math.random()*10)+1;
			j=(int)(Math.random()*10)+1;
		}while(map[i][j]!=0);
		map[i][j]=food;
	}
	public void judge()
	{
		if(flag==2) {
			int option=JOptionPane.YES_NO_OPTION;
			int choice;
			String message="游戏结束,你的得分为:"+score+"分,是否重来?";
			choice=JOptionPane.showConfirmDialog(null, message,"游戏结束",option);
			if(choice==1)
			{
				System.exit(0);
			}
			else
			if(choice==0)
			{
				score=0;
				initMap();
				System.out.println("暂停ssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
				
				flag=1;
				counttime=0;
				overtime=500;
				repaint();
			}
			
		
		}
	}
	

}




有想要代码素材包的,私聊。
2.png
1.png

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

SupperQS 发表于 2020-12-25 00:57
谢谢分享                        
paulz2018 发表于 2019-2-16 13:33
骑狗的猴子 发表于 2019-2-16 13:37
头像被屏蔽
810086218 发表于 2019-2-16 13:42
提示: 作者被禁止或删除 内容自动屏蔽
xiaobaibaibai 发表于 2019-2-16 13:43
可以参考一下 我自己也做一个
win_ai 发表于 2019-2-17 13:37
这个 图片很可爱啊
7kjccom 发表于 2019-2-17 15:32
野生程序员
 楼主| 20175101019 发表于 2019-2-17 19:05

何出此言?
reeye 发表于 2019-2-21 18:15
哈哈, nb, 逗乐我了
Ruyimg 发表于 2019-2-28 19:33
有趣,看起来挺好玩
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 21:20

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表