189 8069 5689

贪吃蛇简单源代码java 贪吃蛇java代码讲解

求一个java的贪吃蛇程序(打包),发我

package com.tarena.elts.test;

创新互联公司专注于凉城企业网站建设,响应式网站,商城网站开发。凉城网站建设公司,为凉城等地区提供建站服务。全流程按需定制开发,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

import java.awt.Color;

import java.awt.Font;

import java.awt.Point;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.HashSet;

import java.util.List;

import java.util.Map;

import java.util.Random;

import java.util.Set;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class WormFrame extends JFrame{

private static final long serialVersionUID = 1L;

public static final int UP = -10;

public static final int DOWN = 10;

public static final int LEFT = -200;

public static final int RIGHT = 200;

private int speed = 200;//蛇的移动速度,越凳敬小越快

private JPanel jPanel=null;//游戏面板

private JLabel jLabel=null;//显示游戏结束的标签

private JButton reset = null;//从新开始游戏的按钮

private JButton control = null;//控制方向的按钮

private ListPoint worm = new ArrayListPoint();//贪吃蛇

//将整个面板划分成节点,蛇走到那里,就那整个扒雹节点枣此慎点亮

private MapPoint,JButton nodes = new HashMapPoint,JButton();

private int dir= LEFT;//方向

private Point food;//食物

private boolean isContinue=false;//判断蛇是否行动的标记

public static void main(String[] args) {

new WormFrame();

}

public WormFrame() {

initialize();

start();

}

//游戏初始化

private void initialize() {

this.setSize(500, 500);

this.setLocation(250, 100);

this.setResizable(false);

this.add(getJPanel());//添加面板

this.setTitle("贪吃蛇,空格键暂停,回车开始");

this.setVisible(true);

}

//游戏开始

private void start() {

isContinue = true;

while (true) {

while (isContinue) {

try {

Point p = worm.get(0);

int x = (int) p.getX() + dir / 20;

int y = (int) p.getY() + dir % 100;

if (food.equals(new Point(x, y))) {

worm.add(0, food);

if(worm.size()%1==0){

speed-=10;

}

getFood();

continue;

}

p = new Point(x, y);

if(worm.contains(p)){

throw new Exception();

}

nodes.get(p).setVisible(false);

worm.add(0, p);

nodes.get(worm.remove(worm.size() - 1)).setVisible(true);

Thread.sleep(speed);

} catch (Exception e) {

jLabel.setVisible(true);

reset.setVisible(true);

//不停止内循环,jLabel和reset不消失

isContinue = false;

}

}

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

//游戏从新开始

private void reset(){

worm = new ArrayListPoint();

for(Point p :nodes.keySet()){

nodes.get(p).setVisible(false);

}

addWorm();

dir = LEFT;

getFood();

isContinue =true;

}

//获取游戏面板的方法,面板中有记录游戏时间的标签,

//代表游戏角色的按钮和 重新开始游戏的按钮

private JPanel getJPanel() {

if (jPanel == null) {

//显示游戏结束的标签

getOver();

//从新开始的按钮

getReset();

//控制按钮

getControl();

//游戏面板

jPanel = new JPanel();

jPanel.setLayout(null);//设置面板布局为空

//jPanel.setForeground(new Color(255,255,255));//设置面板前景色

jPanel.setBackground(new Color(0,0,0));//设置面板背景色

jPanel.add(jLabel,null);//面板中添加 显示游戏结束的标签

jPanel.add(reset,null);//面板中添加 从新开始 的按钮

jPanel.add(control,null);

for(int i = 0;i490;i+=10){

for (int j = 0; j 470; j+=10) {

JButton jb = new JButton();

Point p = new Point(i,j);

jb.setBounds(i,j, 10, 10);

jb.setBackground(new Color(255,255,255));

jb.setEnabled(false);

//jb.setVisible(true);

nodes.put(p, jb);

jPanel.add(jb,null);

}

}

addWorm();//添加一条蛇

getFood();//食物按钮

jPanel.setVisible(true);//设置面板可见

}

return jPanel;

}

//游戏结束标签

private void getOver() {

jLabel = new JLabel();

jLabel.setBounds(170, 200, 200, 50);//设置标签大小和位置

jLabel.setFont(new Font("Dialog", Font.BOLD, 24));//设置标签字体

jLabel.setForeground(new Color(250, 2, 2));//设置标签前景颜色

jLabel.setText(" 游戏结束");

jLabel.setEnabled(true);//设置此标签可用

jLabel.setVisible(false);//设置此标签不可见

}

//从新开始按钮

private void getReset() {

if (reset == null) {

reset = new JButton();//新建一个按钮

reset.setBounds(170, 300, 164, 51);//设置按钮的大小

reset.setText("重新开始");//设置按钮的内容

reset.setVisible(false);//设置按钮不可见

//添加按钮的时间监听器

reset.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

reset.setVisible(false);// 点击重新开始按钮后,按钮消失

jLabel.setVisible(false);// 记录游戏时间的按钮也消失

try {

Thread.sleep(1000);

} catch (InterruptedException e1) {

e1.printStackTrace();

}

reset();

}

});

}

}

//控制方向的按钮

private void getControl(){

if(control == null){

control = new JButton();

control.addKeyListener(new KeyAdapter() {

@Override

public void keyPressed(KeyEvent e) {

switch(e.getKeyCode()){

case KeyEvent.VK_LEFT :chDir(LEFT);break;

case KeyEvent.VK_RIGHT :chDir(RIGHT);break;

case KeyEvent.VK_UP :chDir(UP);break;

case KeyEvent.VK_DOWN :chDir(DOWN);break;

case KeyEvent.VK_ENTER:isContinue = true;break;

case KeyEvent.VK_SPACE:isContinue = false;break;

}

}

});

}

}

//生成食物

private void getFood(){

Random ran = new Random();

//横坐标最大480,纵坐标最大460

int x = ran.nextInt(49)*10;

int y = ran.nextInt(47)*10;

food = new Point(x,y);

SetPoint set = new HashSetPoint(worm);

while(set.contains(food)){

x = ran.nextInt(50)*10;

y = ran.nextInt(50)*10;

food = new Point(x,y);

}

nodes.get(food).setVisible(false);

}

//改变方向

private void chDir(int dir){

if(this.dir+dir!=0){

this.dir = dir;

}

}

//添加Worm的方法

private void addWorm(){

for(int i = 250;i300;i+=10){

Point p = new Point(i,250);

worm.add(p);

nodes.get(p).setVisible(true);

}

}

}

//给分


网页名称:贪吃蛇简单源代码java 贪吃蛇java代码讲解
网页URL:http://jkwzsj.com/article/dspjscd.html

其他资讯