在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Java语言基础 → 基于java的蜘蛛纸牌小游戏开发

基于java的蜘蛛纸牌小游戏开发

Java语言基础

下载此实例
  • 开发语言:Java
  • 实例大小:0.08M
  • 下载次数:39
  • 浏览次数:1409
  • 发布时间:2017-12-22
  • 实例类别:Java语言基础
  • 发 布 人:个浪费了健康
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 游戏 蜘蛛纸牌 java开发

实例介绍

【实例简介】

该游戏通过鼠标操作,讲电脑多次分发的拍,按照相同的花色由大至小排列起来,直到桌面上的牌全部消失,即为胜利。它主要包括如下功能。

l  难度选择:分为简单(单色)、中级(双色)、和高级(四色)。

l  开局:任何时候都可以重新开始一盘新的游戏。

l  发牌。

l  显示可执行操作:提示当前可执行操作。

【实例截图】

from clipboardfrom clipboardfrom clipboard

【核心代码】

PKCard.java


package game;

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

public class PKCard extends JLabel implements MouseListener,
   MouseMotionListener{
Point point = null;
   Point initPoint = null;

   int value = 0;
   int type = 0;

   String name = null;
   Container pane = null;

   Spider main = null;

   boolean canMove = false;
   boolean isFront = false;
   PKCard previousCard = null;

   public void mouseClicked(MouseEvent arg0){}

   public void flashCard(PKCard card){

      new Flash(card).start();

      if(main.getNextCard(card) !=null){
 card.flashCard(main.getNextCard(card));
 }
   }

   class Flash extends Thread{
  private PKCard card = null;

  public Flash(PKCard card){
  this.card = card;
   }

   /**
   *线程的run()方法
   *为纸牌的正面设置白色图片
   */
   public void run(){
  boolean is = false;
  ImageIcon icon = new ImageIcon("images/white.gif");
  for (int i = 0; i < 4; i ){
  try{
  Thread.sleep(200);
  }
  catch (InterruptedException e){
  e.printStackTrace();
  }

  if (is){
               this.card.turnFront();
          is = !is;
  }
  else{
  this.card.setIcon(icon);
  is = !is;
  }
            card.updateUI();
  }
   }
}

/**
 *按下鼠标
 */
 public void mousePressed(MouseEvent mp){
point = mp.getPoint();
main.setNA();
this.previousCard = main.getPreviousCard(this);
 }

 /**
  *释放鼠标
  */
 public void mouseReleased(MouseEvent mr){
Point point = ((JLabel) mr.getSource()).getLocation();

int n = this.whichColumnAvailable(point);
if (n == -1 || n == this.whichColumnAvailable(this.initPoint)){
    this.setNextCardLocation(null);
main.table.remove(this.getLocation());
this.setLocation(this.initPoint);
main.table.put(this.initPoint,this);
return;
}

point = main.getLastCardLocation(n);
boolean isEmpty = false;
PKCard card = null ;
if (point == null){
point = main.getGroundLabelLocation(n);
isEmpty = true;
}
else{
card = (PKCard) main.table.get(point);
}

if (isEmpty || (this.value 1 == card.getCardValue())){
   point.y = 40;
if (isEmpty) point.y -= 20;
this.setNextCardLocation(point);
        main.table.remove(this.getLocation());
point.y -= 20;
this.setLocation(point);
main.table.put(point, this);
this.initPoint = point;
if (this.previousCard != null){
this.previousCard.turnFront();
this.previousCard.setCanMove(true);
}

this.setCanMove(true);
}
else{
this.setNextCardLocation(null);
main.table.remove(this.getLocation());
this.setLocation(this.initPoint);
main.table.put(this.initPoint, this);
return;
}
point = main.getLastCardLocation(n);
card = (PKCard) main.table.get(point);
if (card.getCardValue() == 1){
point.y -= 240;
card = (PKCard) main.table.get(point);
if (card != null && card.isCardCanMove()){
main.haveFinish(n);
}
}
}

/**
*方法:放置纸牌
*/
public void setNextCardLocation(Point point){
PKCard card = main.getNextCard(this);
if (card !=null){
if (point == null){
card.setNextCardLocation(null);
main.table.remove(card.getLocation());
card.setLocation(card.initPoint);
main.table.put(card.initPoint, card);
}
else{
point = new Point(point);
point.y = 20;
card.setNextCardLocation(point);
point.y -=20;
main.table.remove(card.getLocation());
card.setLocation(point);
main.table.put(card.getLocation(), card);
card.initPoint = card.getLocation();
}
}
}//daxian
         public int whichColumnAvailable(Point point){
int x=point.x;
int y=point.y;
int a=(x-20)/101;
int b=(x-20)%101;
if (a!=9){
if (b>30 && b<=71){
a=-1;
}
else if (b>71){
a ;
}
}
    else if (b>71){
    a=-1;
}
if (a!=-1){
Point p=main.getLastCardLocation(a);
if (p==null) p=main.getGroundLabelLocation(a);
b=y-p.y;
if (b<=-96 || b>=96){
a=-1;
}
}
return a;
}

public void mouseEntered(MouseEvent arg0){
}
public void mouseExited(MouseEvent arg0){
}
 public void mouseDragged(MouseEvent arg0){
  if (canMove){
  int x=0;
  int y=0;
  Point p=arg0.getPoint();
  x=p.x-point.x;
  y=p.y-point.y;
  this.moving(x,y);
}
}

public void moving(int x,int y){
PKCard card=main.getNextCard(this);
Point p=this.getLocation();
pane.setComponentZOrder(this,1);
main.table.remove(p);
p.x =x;
p.y =y;
this.setLocation(p);
main.table.put(p,this);
if (card!=null) card.moving(x,y);
}

public void mouseMoved(MouseEvent arg0){
}
public PKCard(String name, Spider spider){
super();
this.type=new Integer(name.substring(0,1)).intValue();
this.value=new Integer(name.substring(2)).intValue();
this.name=name;
this.main=spider;
this.pane=this.main.getContentPane();
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setIcon(new ImageIcon("images/rear.gif"));
this.setSize(71,96);
this.setVisible(true);
}

public void turnFront(){
this.setIcon(new ImageIcon("images/" name ".gif"));
this.isFront=true;
}

public void turnRear(){
this.setIcon(new ImageIcon("images/rear.gif"));
this.isFront=false;
this.canMove=false;
}

public void moveto(Point point){
this.setLocation(point);
this.initPoint=point;
}
public void setCanMove(boolean can){
this.canMove=can;
PKCard card=main.getPreviousCard(this);
if (card!=null&&card.isCardFront()){
if(!can){
if (!card.isCardCanMove()){
return;
}
else{
card.setCanMove(can);
}
}
else{
if(this.value 1==card.getCardValue()
&& this.type==card.getCardType()){
card.setCanMove(can);
}
else{
card.setCanMove(false);
}
}
}
}

public boolean isCardFront(){
return this.isFront;
}
public boolean isCardCanMove(){
return this.canMove;
}
public int getCardValue(){
return value;
}
public int getCardType(){
return type;
}}


Spider.java


package game;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Spider extends JFrame {
public static final int EASY=1;
public static final int NATURAL=2;
public static final int HARD=3;
private int grade=Spider.EASY;//默认开始为简单
private Container pane=null;
private PKCard cards[]=new PKCard[104];
private JLabel clickLabel=null;
private int c=0;
private int n=0;
private int a=0;
private int finish=0;
Hashtable table=null;
private JLabel groundLabel[]=null;

public static void main(String[] args){
Spider spider=new Spider();
spider.setVisible(true);
}
public Spider(){
setTitle("蜘蛛牌");
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
setSize(1024,742);
setJMenuBar(new SpiderMenuBar(this));
pane=this.getContentPane();
pane.setBackground(new Color(6,204,246));
pane.setLayout(null);
clickLabel=new JLabel();
clickLabel.setBounds(883,606,121,96);
pane.add(clickLabel);
clickLabel.addMouseListener(
new MouseAdapter(){
public void mouseReleased(MouseEvent me){
if(c<60){
Spider.this.deal();
}
}
}
);
this.initCards();
this.randomCards();
this.setCardsLocation();
groundLabel=new JLabel[10];
int x=20;
for(int i=0;i<10;i ){
groundLabel[i]=new JLabel();
groundLabel[i]
.setBorder(javax.swing.BorderFactory
.createEtchedBorder(
javax.swing.border.EtchedBorder.RAISED));
groundLabel[i].setBounds(x,25,71,96);
x =101;
this.pane.add(groundLabel[i]);
}
this.setVisible(true);
this.deal();

this.addKeyListener(new KeyAdapter(){
class Show extends Thread{
public void run(){
Spider.this.showEnableOperator();
}
}
public void keyPressed(KeyEvent e){
if(finish!=8)if(e.getKeyCode()==KeyEvent.VK_D&&c<60){
Spider.this.deal();
}
else if(e.getKeyCode()==KeyEvent.VK_M){
new Show().start();
}
}
});
}
public void newGame(){
this.randomCards();
this.setCardsLocation();
this.setGroundLabelZOrder();
this.deal();
}
public int getC(){
return c;
}
public void setGrade(int grade){
this.grade=grade;
}
public void initCards(){
if (cards[0]!=null){
for(int i=0;i<104;i ){
pane.remove(cards[i]);
}
}
int n=0;
if(this.grade==Spider.EASY){
n=1;
}
else if(this.grade==Spider.NATURAL){
n=2;
}
else{
n=4;
}
for (int i=1;i<=8;i ){
for(int j=1;j<=13;j ){
cards[(i-1)*13 j-1]=new PKCard((i%n 1) "-" j,this);
}
}
this.randomCards();
}
public void randomCards(){
PKCard temp=null;
for(int i=0;i<52;i ){
int a=(int)(Math.random()*104);
int b=(int)(Math.random()*104);
temp=cards[a];
cards[a]=cards[b];
cards[b]=temp;
}
}
public void setNA(){
a=0;
n=0;
}
public void setCardsLocation(){
table=new Hashtable();
c=0;
finish=0;
n=0;
a=0;
int x=883;
int y=580;
for(int i=0;i<6;i ){
for(int j=0;j<10;j ){
int n=i*10 j;
pane.add(cards[n]);
cards[n].turnRear();
cards[n].moveto(new Point(x,y));
table.put(new Point(x,y),cards[n]);
}
x =10;
}
x=20;
y=45;
for (int i=10;i>5;i--){
for(int j=0;j<10;j ){
int n=i*10 j;
if(n>=104)continue;
pane.add(cards[n]);
cards[n].turnRear();
cards[n].moveto(new Point(x,y));
table.put(new Point(x,y),cards[n]);
x =101;
}
x=20;
y-=5;
}
}
public void showEnableOperator(){
int x=0;
out:while(true){
Point point=null;
PKCard card=null;
do{
if (point!=null){
n ;
}
point=this.getLastCardLocation(n);
while(point==null){
point=this.getLastCardLocation( n);
if(n==10)n=0;
x ;
if(x==10)break out;
}
card=(PKCard)this.table.get(point);
}
while(!card.isCardCanMove());
while(this.getPreviousCard(card)!=null
&&this.getPreviousCard(card).isCardCanMove()){
card=this.getPreviousCard(card);
}
if(a==10){
a=0;
}
for(;a<10;a ){
if(a!=n){
Point p=null;
PKCard c=null;
do{
if(p!=null){
a ;
}
p=this.getLastCardLocation(a);
int z=0;
while(p==null){
p=this.getLastCardLocation( a);
if(a==10)a=0;
if(a==n)a ;
z ;
if(z==10)break out;
}
c=(PKCard)this.table.get(p);
}
while(!c.isCardCanMove());
if(c.getCardValue()==card.getCardValue() 1){
card.flashCard(card);
try{
Thread.sleep(800);
}
catch(InterruptedException e){
e.printStackTrace();
}
c.flashCard(c);
a ;
if(a==10){
n ;
}
break out;
          }
}
}
n ;
if(n==10){
n=0;
}
x ;
if(x==10){
break out;
  }
}
}
public void deal()
{
this.setNA();
for(int i=0;i<10;i ){
if(this.getLastCardLocation(i)==null){
JOptionPane.showMessageDialog(this,"有空位不能发牌!","提示",JOptionPane.WARNING_MESSAGE);
return;
}
}
int x = 20;
for(int i=0;i<10;i ){
Point lastPoint=this.getLastCardLocation(i);
if(c==0){
lastPoint.y =5;
}
else{
lastPoint.y =20;
}
table.remove(cards[c i].getLocation());
cards[c i].moveto(lastPoint);
table.put(new Point(lastPoint),cards[c i]);
cards[c i].turnFront();
cards[c i].setCanMove(true);
this.pane.setComponentZOrder(cards[c i],1);
Point point=new Point(lastPoint);
if(cards[c i].getCardValue()==1){
int n=cards[c i].whichColumnAvailable(point);
point.y-=240;
PKCard card=(PKCard)this.table.get(point);
if(card!=null && card.isCardCanMove()){
this.haveFinish(n);
}
}
x =101;
}
c =10;
}
public PKCard getPreviousCard(PKCard card){
Point point=new Point(card.getLocation());
point.y-=5;
card=(PKCard)table.get(point);
if(card!=null)
return card;
point.y-=15;
card=(PKCard) table.get(point);
return card;
}
public PKCard getNextCard(PKCard card){
Point point = new Point(card.getLocation());
point.y =5;
card=(PKCard) table.get(point);
if(card!=null)
return card;
point.y =15;
card = (PKCard) table.get(point);
return card;
}
public Point getLastCardLocation(int column){
Point point=new Point(20 column*101,25);
PKCard card=(PKCard)this.table.get(point);
if(card==null) return null;
while(card!=null){
point=card.getLocation();
card=this.getNextCard(card);
}
return point;
}
public Point getGroundLabelLocation(int column){
return new Point(groundLabel[column].getLocation());
}
public void setGroundLabelZOrder(){
for(int i=0;i<10;i ){
pane.setComponentZOrder(groundLabel[i],105 i);
}
}
public void haveFinish(int column){
Point point = this.getLastCardLocation(column);
PKCard card=(PKCard)this.table.get(point);
do{
this.table.remove(point);
card.moveto(new Point(20 finish*10,580));
pane.setComponentZOrder(card,1);
this.table.put(card.getLocation(),card);
card.setCanMove(false);
point=this.getLastCardLocation(column);
if(point==null)
card=null;
else
card=(PKCard) this.table.get(point);
}
while(card!=null&&card.isCardCanMove());
finish ;
if(finish==8){
JOptionPane.showMessageDialog(this,"恭喜你,顺利通过!","成功",
JOptionPane.PLAIN_MESSAGE);
}
if(card!=null){
card.turnFront();
card.setCanMove(true);
}
}
}


实例下载地址

基于java的蜘蛛纸牌小游戏开发

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警