JAVA课程设计扑克游戏.docx

上传人:b****5 文档编号:7263735 上传时间:2023-01-22 格式:DOCX 页数:25 大小:346.08KB
下载 相关 举报
JAVA课程设计扑克游戏.docx_第1页
第1页 / 共25页
JAVA课程设计扑克游戏.docx_第2页
第2页 / 共25页
JAVA课程设计扑克游戏.docx_第3页
第3页 / 共25页
JAVA课程设计扑克游戏.docx_第4页
第4页 / 共25页
JAVA课程设计扑克游戏.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

JAVA课程设计扑克游戏.docx

《JAVA课程设计扑克游戏.docx》由会员分享,可在线阅读,更多相关《JAVA课程设计扑克游戏.docx(25页珍藏版)》请在冰豆网上搜索。

JAVA课程设计扑克游戏.docx

JAVA课程设计扑克游戏

课程设计说明书NO.1

扑克游戏

1.课程设计的目的

Java语言是当今流行的网络编程语言,它具有面向对象、跨平台、分布应用等特点。

面向对象的开发方法是当今世界最流行的开发方法,它不仅具有更贴近自然的语义,而且有利于软件的维护和继承。

为了进一步巩固课堂上所学到的知识,深刻把握Java语言的重要概念及其面向对象的特性,使我们能够熟练的应用面向对象的思想和设计方法解决实际问题的能力。

通过此次课程设计,巩固所学Java语言基本知识,增进Java语言编辑基本功,掌握JDK、Editplus、Eclipse、JCreator等开发工具的运用,拓宽常用类库的应用。

使学生通过该教学环节与手段,把所学课程及相关知识加以融会贯通,全面掌握Java语言的编程思想及面向对象程序设计的方法,为今后从事实际工作打下坚实的基础。

本设计使用JAVA语言开发扑克游戏程序,将电脑多次分发给你的牌按照相同的花色由大至小排列起来。

2.设计方案论证

2.1设计思路

用java语言,编程实现纸牌游戏,拥有如下规则,将电脑多次分发给你的牌按照相同的花色由大至小排列起来。

游戏分为三个难度,简单,普通,困难。

简单为单一花色。

困难所分发给的牌有四种花色。

将大小相邻的纸牌依次排列到一起当每种花色的全部纸牌都按顺序排列到一起,则游戏结束。

2.2设计方法

将程序设计成为4个类,AboutDialog类用于实现全部的对话。

PKCard类用于实现游戏的规则。

SpiderMenuBar类用于实现各个模块的功能。

Spider为主界面。

在SpiderMenuBar建立主界面菜单,通过构造函数publicSpiderMenuBar构造函数,生成JMenuBar的图形界面,对菜单中各按钮进行事件监听。

在该方法中调用spider

的构造方法,在其中生成SpiderMenuBar对象,并放置在框架之上,同时设置框架标题

沈阳大学

课程设计说明书NO.2

框架大小背景颜色,布局为空。

publicSpider(){

setTitle("陶时扑克");

setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

setSize(1024,742);

setJMenuBar(newSpiderMenuBar(this));

pane=this.getContentPane();

pane.setBackground(newColor(14,25,26));

pane.setLayout(null);

clickLabel=newJLabel();

clickLabel.setBounds(883,606,121,96);

pane.add(clickLabel);

}

在Spider类中有如下方法:

newGame新游戏的方法,setGrade设置等级方法,初始化等级方法。

setGrade,randomCards随即函数。

PKCardgetPreviousCard获得card上面的那张牌的方法等。

由PKCardgetPreviousCard,PKCardgetNextCard,getLastCardLocation方法对类PKCard调用,代码如下:

publicPKCardgetPreviousCard(PKCardcard){

Pointpoint=newPoint(card.getLocation());

point.y-=5;

card=(PKCard)table.get(point);

if(card!

=null){

returncard;

}

point.y-=15;

card=(PKCard)table.get(point);

沈阳大学

课程设计说明书NO.3

returncard;

}

publicPKCardgetNextCard(PKCardcard){

Pointpoint=newPoint(card.getLocation());

point.y+=5;

card=(PKCard)table.get(point);

if(card!

=null)

returncard;

point.y+=15;

card=(PKCard)table.get(point);

returncard;

}

publicPointgetLastCardLocation(intcolumn){

Pointpoint=newPoint(20+column*101,25);

PKCardcard=(PKCard)this.table.get(point);

if(card==null)returnnull;

while(card!

=null){

point=card.getLocation();

card=this.getNextCard(card);

}

returnpoint;

}

publicPointgetGroundLabelLocation(intcolumn){

returnnewPoint(groundLabel[column].getLocation());

}

publicvoidsetGroundLabelZOrder(){

for(inti=0;i<10;i++){

沈阳大学

课程设计说明书NO.4

pane.setComponentZOrder(groundLabel[i],105+i);

}

}

2.3功能模块图

图1功能模块图

沈阳大学

课程设计说明书NO.5

2.4程序流程图

 

图2程序流程图

3.设计结果与分析

(1)首先是对游戏中主菜单的设计,设计的菜单包括两个大部分,选项和帮助,如图3所示:

沈阳大学

课程设计说明书NO.6

图3显示菜单

通过如下代码实现:

JMenujNewGame=newJMenu("选项");

JMenujHelp=newJMenu("帮助");

(2)在选项菜单下面包含7级子菜单,如图4所示:

图4显示菜单

沈阳大学

课程设计说明书NO.7

通过如下代码实现:

JMenuItemjItemOpen=newJMenuItem("开局");

JMenuItemjItemPlayAgain=newJMenuItem("重发牌");

JRadioButtonMenuItemjRMItemEasy=newJRadioButtonMenuItem("简单");

JRadioButtonMenuItemjRMItemNormal=newJRadioButtonMenuItem("较难");

JRadioButtonMenuItemjRMItemHard=newJRadioButtonMenuItem("困难");;

JMenuItemjItemExit=newJMenuItem("退出");

JMenuItemjItemValid=newJMenuItem("显示可执行行操作");

(3)帮助下面包含2级子菜单,分别为游戏规则和声明,如图5所示:

图5显示帮助

通过如下代码实现:

JTabbedPanejTabbedPane=newJTabbedPane();

privateJPaneljPanel1=newJPanel();

privateJPaneljPanel2=newJPanel();

(4)主窗体通过类Spider实现。

将窗体名称设置为“陶时扑克”,框架的大小设置为1024*742,背景颜色设置为黑色,布局管理设置为空,通过如下代码实现:

沈阳大学

课程设计说明书NO.8

publicSpider()

{

setTitle("陶时扑克");

setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

setSize(1024,742);

setJMenuBar(newSpiderMenuBar(this));

pane=this.getContentPane();

pane.setBackground(newColor(14,25,26));

pane.setLayout(null);

}

(5)进入游戏之后,首先选择开始新游戏,通过类Spider调用它的方法newGame方法,采用随机函数随机初始化牌的顺序(这样做的目的是,使游戏性增加可玩性,使每次出现牌的顺序不同),如图6所示。

图6进入新游戏界面

用如下代码实现:

publicvoidnewGame(){

this.randomCards();

this.setCardsLocation();

this.setGroundLabelZOrder();

this.deal();

}

publicintgetC(){

returnc;

}

publicvoidsetGrade(intgrade){

this.grade=grade;

}

publicvoidinitCards(){

if(cards[0]!

=null){

for(inti=0;i<104;i++){

pane.remove(cards[i]);

}

}

intn=0;

if(this.grade==Spider.EASY){

n=1;

}

elseif(this.grade==Spider.NATURAL){

n=2;

}

else{

n=4;

}

for(inti=1;i<=8;i++)

{

沈阳大学

课程设计说明书NO.9

for(intj=1;j<=13;j++){

cards[(i-1)*13+j-1]=newPKCard((i%n+1)+"-"+j,this);

}

}

this.randomCards();

}

publicvoidrandomCards(){

PKCardtemp=null;

for(inti=0;i<52;i++){

inta=(int)(Math.random()*104);

intb=(int)(Math.random()*104);

temp=cards[a];

cards[a]=cards[b];

cards[b]=temp;

}

}

(6)在游戏界面的右下角做一张图片,显示扑克牌的背面,同时点击图片,系统自动发牌(当游戏无法继续的时候,可以点击该图片,在每副纸牌上发一张牌,使得进入僵局的纸牌游戏得以继续进行),运行界面如图7,图8所示。

沈阳大学

课程设计说明书NO.10

图7发牌功能界面

图8显示发牌

沈阳大学

课程设计说明书NO.11

用如下代码实现:

首先在界面上添加纸牌背面的图片:

publicvoidturnRear(){

this.setIcon(newImageIcon("images/6.gif"));

this.isFront=false;

this.canMove=false;

}

publicPKCardgetPreviousCard(PKCardcard){

Pointpoint=newPoint(card.getLocation());

point.y-=5;

card=(PKCard)table.get(point);

if(card!

=null){

returncard;

}

point.y-=15;

card=(PKCard)table.get(point);

returncard;

}

publicPKCardgetNextCard(PKCardcard){

Pointpoint=newPoint(card.getLocation());

point.y+=5;

card=(PKCard)table.get(point);

if(card!

=null)

returncard;

point.y+=15;

card=(PKCard)table.get(point);

returncard;

沈阳大学

课程设计说明书NO.12

}

publicPointgetLastCardLocation(intcolumn){

Pointpoint=newPoint(20+column*101,25);

PKCardcard=(PKCard)this.table.get(point);

if(card==null)returnnull;

while(card!

=null){

point=card.getLocation();

card=this.getNextCard(card);

}

returnpoint;

}

publicPointgetGroundLabelLocation(intcolumn){

returnnewPoint(groundLabel[column].getLocation());

}

publicvoidsetGroundLabelZOrder(){

for(inti=0;i<10;i++){

pane.setComponentZOrder(groundLabel[i],105+i);

}

}

(7)在游戏中每次开始游戏都由程序随机发牌,发牌的过程是,先设置纸牌的初始位置由随机函数产生,并随机生成牌号,设置纸牌的位置,初始化待展开的纸牌,将纸牌放置到固定位置(这里调用PKcard类中的setNextCardLocation方法调用图片文件夹里的纸牌图片),如图9,图10所示。

沈阳大学

课程设计说明书NO.13

图9图片库

图10图片被载入

沈阳大学

课程设计说明书NO.14

使用如下代码实现:

for(inti=0;i<6;i++){

for(intj=0;j<10;j++){

intn=i*10+j;

pane.add(cards[n]);

cards[n].turnRear();

cards[n].moveto(newPoint(x,y));

table.put(newPoint(x,y),cards[n]);

}

x+=10;

}

x=20;

y=45;

for(inti=10;i>5;i--){

for(intj=0;j<10;j++){

intn=i*10+j;

if(n>=104)continue;

pane.add(cards[n]);

cards[n].turnRear();

cards[n].moveto(newPoint(x,y));

table.put(newPoint(x,y),cards[n]);

x+=101;

}

x=20;

y-=5;

}

沈阳大学

课程设计说明书NO.15

publicvoidshowEnableOperator(){

intx=0;

out:

while(true){

Pointpoint=null;

PKCardcard=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)breakout;

}

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){

沈阳大学

课程设计说明书NO.16

Pointp=null;

PKCardc=null;

do{

if(p!

=null){

a++;

}

p=this.getLastCardLocation(a);

intz=0;

while(p==null){

p=this.getLastCardLocation(++a);

if(a==10)a=0;

if(a==n)a++;

z++;

if(z==10)breakout;

}

c=(PKCard)this.table.get(p);

}

while(!

c.isCardCanMove());

if(c.getCardValue()==card.getCardValue()+1){

card.flashCard(card);

try{

Thread.sleep(800);

}

catch(InterruptedExceptione){

e.printStackTrace();

}

c.flashCard(c);

沈阳大学

课程设计说明书NO.17

a++;

if(a==10){

n++;

}

breakout;

}

}

}

n++;

if(n==10){

n=0;

}

x++;

if(x==10){

breakout;

}

}

}

通过调用如下代码实现图片的调用:

publicvoidsetNextCardLocation(Pointpoint){

PKCardcard=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);

沈阳大学

课程设计说明书NO.18

}

else{

point=newPoint(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();

}

}

}

(8)进入游戏之后能对游戏的难度进行选择,难度分为三种,分别为简单,较难,困难,简单为单一花色的纸牌进行游戏,而较难和困难的纸牌花色相应的增多。

在设计时首先在SpiderMenuBar类中,将三种难度的菜单实现,然后通过调用PKcard的方法实现各难度的功能,如图11,12所示。

 

沈阳大学

课程设计说明书NO.19

图11难度选择

图12选择困难

沈阳大学

课程设计说明书NO.20

通过如下代码实现:

实现菜单分成三种难度:

JRadioButtonMenuItemjRMItemEasy=newJRadioButtonMenuItem("简单");

JRadioButtonMenuItemjRMItemNormal=newJRadioButtonMenuItem("较难");

JRadioButtonMenuItemjRMItemHard=newJRadioButtonMenuItem("困难");;

jRMItemEasy.addActionListener(newjava.awt.event.ActionListener(){

publicvoidactionPerformed(java.awt.event.ActionEvente){

main.setGrade(Spider.EASY);

main.initCards();

main.newGame();

}

}

分别对菜单进行事件监听,若选中相应的难度登记则触发Spider类中的方法进行实现:

jRMItemNormal.addActionListener(newjava.awt.event.ActionListener(){

publicvoidactionPerformed(java.awt.event.ActionEvente){

main.setGrade(Spider.NATURAL);

main.initCards();

main.newGame();

}

}

jRMItemHard.addActionListener(newjava.awt.event.Acti

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1