package com.tarena.elts.test;
創(chuàng)新互聯(lián)公司專注于涼城企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,商城網(wǎng)站開發(fā)。涼城網(wǎng)站建設(shè)公司,為涼城等地區(qū)提供建站服務(wù)。全流程按需定制開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
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;//顯示游戲結(jié)束的標(biāo)簽
private JButton reset = null;//從新開始游戲的按鈕
private JButton control = null;//控制方向的按鈕
private ListPoint worm = new ArrayListPoint();//貪吃蛇
//將整個(gè)面板劃分成節(jié)點(diǎn),蛇走到那里,就那整個(gè)扒雹節(jié)點(diǎn)棗此慎點(diǎn)亮
private MapPoint,JButton nodes = new HashMapPoint,JButton();
private int dir= LEFT;//方向
private Point food;//食物
private boolean isContinue=false;//判斷蛇是否行動的標(biāo)記
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);
//不停止內(nèi)循環(huán),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;
}
//獲取游戲面板的方法,面板中有記錄游戲時(shí)間的標(biāo)簽,
//代表游戲角色的按鈕和 重新開始游戲的按鈕
private JPanel getJPanel() {
if (jPanel == null) {
//顯示游戲結(jié)束的標(biāo)簽
getOver();
//從新開始的按鈕
getReset();
//控制按鈕
getControl();
//游戲面板
jPanel = new JPanel();
jPanel.setLayout(null);//設(shè)置面板布局為空
//jPanel.setForeground(new Color(255,255,255));//設(shè)置面板前景色
jPanel.setBackground(new Color(0,0,0));//設(shè)置面板背景色
jPanel.add(jLabel,null);//面板中添加 顯示游戲結(jié)束的標(biāo)簽
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);//設(shè)置面板可見
}
return jPanel;
}
//游戲結(jié)束標(biāo)簽
private void getOver() {
jLabel = new JLabel();
jLabel.setBounds(170, 200, 200, 50);//設(shè)置標(biāo)簽大小和位置
jLabel.setFont(new Font("Dialog", Font.BOLD, 24));//設(shè)置標(biāo)簽字體
jLabel.setForeground(new Color(250, 2, 2));//設(shè)置標(biāo)簽前景顏色
jLabel.setText(" 游戲結(jié)束");
jLabel.setEnabled(true);//設(shè)置此標(biāo)簽可用
jLabel.setVisible(false);//設(shè)置此標(biāo)簽不可見
}
//從新開始按鈕
private void getReset() {
if (reset == null) {
reset = new JButton();//新建一個(gè)按鈕
reset.setBounds(170, 300, 164, 51);//設(shè)置按鈕的大小
reset.setText("重新開始");//設(shè)置按鈕的內(nèi)容
reset.setVisible(false);//設(shè)置按鈕不可見
//添加按鈕的時(shí)間監(jiān)聽器
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
reset.setVisible(false);// 點(diǎn)擊重新開始按鈕后,按鈕消失
jLabel.setVisible(false);// 記錄游戲時(shí)間的按鈕也消失
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();
//橫坐標(biāo)最大480,縱坐標(biāo)最大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);
}
}
}
//給分