JAVA贪吃蛇!!!!!!拼多多版本

做的一个简易版贪吃蛇,没有用到多线程,有待改进,适用与新人练手,使用LinkedList集合存储蛇身。蛇身和蛇头用的字符代替,没有用到图片,下面直接放代码了,正在准备写一个多线程贪吃蛇。

拼多多版贪吃蛇,刚做好就发了上来,有待改进,有同鞋觉得蛇头和蛇身符号不好看可以改成图片,重要的是掌握核心技术。 

规则:

蛇头吃到食物蛇身+1,被吃到的食物消失,新食物出现。

蛇头不能触碰到蛇身。

蛇头不能触碰到墙壁。

实例图片

主类:

package Text.cn;

import java.util.Map;

import Map.cn.MapDome;
import snakeui.cn.Snakewindow;

public class Main {
	public static void main(String[] args) throws InterruptedException {
		Snakewindow snakes = new Snakewindow();
	}
}

实现类:

package snakeui.cn;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Snakewindow extends JFrame{
	Snakemianban mianban = new Snakemianban();
	public Snakewindow() {
		this.setBounds(50, 50, 800, 850);
		this.setTitle("贪吃蛇");
		this.add(mianban);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
}
	
package snakeui.cn;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Snakemianban extends JPanel implements ActionListener,KeyListener {
	
	ImageIcon snake = new ImageIcon("jpp/111.jpg");
	
	LinkedList<Point> snak = new LinkedList<Point>();
	
	boolean suspend=false;
	
	Timer tim = new Timer(200, this);
	
	int move_left = 1;
	int move_right =-1;
	int move_up = 2;
	int move_lower = -2;
	
	int move_1 =-1;
	int move_2 =0;
	
	int foodx;
	int foody;
	Random randow = new Random();
	
	public Snakemianban() {
		this.snakes();
		tim.start();
		this.init();
		this.setFocusable(true);
		this.addKeyListener(this);
		 }
	//初始化蛇的位置
	public void snakes() {
		snak.addFirst(new Point(30,140));
		snak.addFirst(new Point(50,140));
		snak.addFirst(new Point(70,140));
		move_1 =-1;
		
		foodx =30+20*randow.nextInt(35);
		foody =140+30*randow.nextInt(22);
	}
	//舌头碰到蛇身初始化
	public void init() {
		for(int a=1;a<snak.size();a++)
		{
			Point hy = snak.getFirst();
			Point l  = snak.get(a);
			if(hy.x==l.x & hy.y==l.y)
			{
				snak.clear();
				this.snakes();
				
				repaint();//刷新界面
				suspend = !suspend;
				}
		}
		Point y = snak.getFirst();         
		if(y.x<30||y.x>730||y.y<140||y.y>800)
		{
		snak.clear();   
		this.snakes();
		
		repaint();//刷新界面
		suspend = !suspend;
		}
	}

	//画板
	public void paint(Graphics g)
	{
		super.paint(g);
		this.setBackground(Color.WHITE);
		g.fillRect(25, 100, 735, 700);
		this.snake.paintIcon(this, g, 40, 10);
		g.setColor(Color.WHITE);
		g.setFont(new Font("宋体",Font.BOLD,40));
		
		Point dome = snak.getFirst();
		
		g.drawString("@", dome.x, dome.y); //蛇头
		for(int i=snak.size()-1;i>0;i--)  //蛇身
		{
			Point hand = snak.get(i);
			g.drawString("*", hand.x, hand.y);
		}
		
		if(suspend==false)    //暂停游戏
		{
			g.setFont(new Font("宋体",Font.BOLD,50));
			g.drawString("按空格键开始游戏", 200, 400);
		}
		
		g.drawString("*", foodx, foody);
		
	}
	
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	//键盘监听
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		int KeyCode=e.getKeyCode();
		if(KeyCode == KeyEvent.VK_SPACE)
		{
			suspend = !suspend;
			repaint();//刷新界面
		}
		if(KeyCode == KeyEvent.VK_S)
		{	
			move_2=move_1;
			move_1=move_lower; 
		}
		if(KeyCode == KeyEvent.VK_W)
		{
			move_2=move_1;   
			move_1=move_up;
		}
		if(KeyCode ==KeyEvent.VK_A)
		{
			move_2=move_1; 
			move_1=move_left;
		}
		if(KeyCode ==KeyEvent.VK_D)
		{
			move_2=move_1;
			move_1=move_right;
		}
	}
	
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	//java计时器
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(suspend!=false)
		{
		Point bb = snak.getFirst();
		snak.removeLast();
		if(move_1+move_2!=0)
		{
		switch (move_1) {
		case 1:
			snak.addFirst(new Point(bb.x-20,bb.y));
			this.init();
			break;
		case -1:
			snak.addFirst(new Point(bb.x+20,bb.y));
			this.init();
			break;
		case 2:
			snak.addFirst(new Point(bb.x,bb.y-30));
			this.init();
			break;
		case -2:
			snak.addFirst(new Point(bb.x,bb.y+30));
			this.init();
			break;
		default:
			break;
		}
		}
		else {
			switch (move_2) {
			case 1:
				snak.addFirst(new Point(bb.x-20,bb.y));
				move_1 = move_1*(-1);
				break;
			case -1:
				snak.addFirst(new Point(bb.x+20,bb.y));
				move_1 = move_1*(-1);
				break;
			case 2:
				snak.addFirst(new Point(bb.x,bb.y-30));
				move_1 = move_1*(-1);
				break;
			case -2:
				snak.addFirst(new Point(bb.x,bb.y+30));
				move_1 = move_1*(-1);
				break;
			default:
				break;
		}
	}
		Point point = snak.getFirst();
		if(point.x==foodx & point.y==foody)
		{
				foodx =30+20*randow.nextInt(35);
				foody =140+30*randow.nextInt(22);
				snak.add(point);
		}
		
//		if(bb.x>730)
//		{
//			snak.addFirst(new Point(30,140));
//		}
//		else {
//			snak.addFirst(new Point(bb.x+20,bb.y));
//		}
		repaint();//更新屏幕
		}
		 
		tim.start(); //计时器
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值