Andriod應(yīng)用與游戲開發(fā)(紙牌小游戲)實驗報告_第1頁
Andriod應(yīng)用與游戲開發(fā)(紙牌小游戲)實驗報告_第2頁
Andriod應(yīng)用與游戲開發(fā)(紙牌小游戲)實驗報告_第3頁
Andriod應(yīng)用與游戲開發(fā)(紙牌小游戲)實驗報告_第4頁
Andriod應(yīng)用與游戲開發(fā)(紙牌小游戲)實驗報告_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

Andriod應(yīng)用與游戲開發(fā)——紙牌小游戲?qū)嵗@是胡老師給的紙牌小游戲,我用eclipse載入solitaire項目,再選擇andriod運(yùn)行方式運(yùn)行這個項目,自動啟動andriod模擬器之后,這個游戲就運(yùn)行起來了。不知道為什么游戲在andriod模擬器上運(yùn)行時是倒著的,不過還是可以玩的。這是正宗的電腦上的紙牌小游戲,可見使用java編寫的紙牌游戲仿真度也是很高的。由于是在andriod模擬器上運(yùn)行,對于我那個配置不高的本本電腦,光運(yùn)行模擬器就卡個半死,運(yùn)行這游戲卡到無語可想而知,上圖中的梅花6在移動時都可以停在紅桃7下面半天,使我已經(jīng)沒有在模擬器上玩它的欲望了。。。不說廢話了直接進(jìn)入正題,來看看編寫此游戲的java代碼。首先我們看看src文件夾下的各個類對此介紹一下各個類,因為我自己也對這些類不太熟悉,肯定會有一些錯誤的地方,所以僅供參考。AnimateCard類負(fù)責(zé)驅(qū)動游戲,連接其他類別。Card類是紙牌操作類,包括各種操作方法。CardAnchor類則與普遍的cardmain類一樣是紙牌操作的具體步驟。Deck類顧名思義是桌面。Move類與movecard類都講述了具體的鼠標(biāo)移動紙牌的事件。Options類規(guī)定了各個界面的布局。Replay類是重新開始游戲。Rules類是紙牌游戲的具體規(guī)則,反正我是看不懂。Solitaire類以及stats類的確不知道是什么類。還是看看其中規(guī)則的實現(xiàn)代碼吧publicclassCardextendsJLabelimplementsMouseListener,MouseMotionListener{ publicPointpoint,oldPoint;//point是初始鼠標(biāo)位置 CardMainm=null; publicStringname;//卡片名稱1-1 publicStringup="rear";//默認(rèn)反面 publicintcurrentCol=0; publicbooleancanmove=false;//判斷當(dāng)前卡片是否可以拖拽 publicCard(CardMainm){ this.m=m; this.setSize(71,96); this.setVisible(true); this.addMouseListener(this); this.addMouseMotionListener(this); } publicCard(CardMainm,Stringname,Stringflag){ this.m=m; =name; if(flag=="front") this.turnFront(); else{ this.turnRear(); } this.setSize(71,96); this.setVisible(true); this.addMouseListener(this); this.addMouseMotionListener(this); } publicCardgetNext(Pointpoint){ Cardcard=null; point.y+=m.min; card=(Card)m.table.get(point); if(card!=null) returncard; point.y+=m.max-m.min; card=(Card)m.table.get(point); returncard; } publicvoidmoveto(Pointfrom,Pointto){ Pointto2=newPoint(); to2=to; Cardcard=this.getNext(from); m.table.remove(from); this.setLocation(to2); m.table.put(to2,this); if(card!=null) card.moveto(card.getLocation(),newPoint(to2.x,to2.y+m.max));//遞歸 } publicvoidmoving(intx,inty)//拖拽事件 { for(Cardcard:m.dragList) { Pointp=card.getLocation(); m.table.remove(p); p.x+=x; p.y+=y; card.setLocation(p); m.container.setComponentZOrder(card,0); m.table.put(p,card); } //Cardcard=this.getNext(this.getLocation()); //解決某個bug //if(this.getCol()==currentCol) //System.out.println(this.getCol()+":"+currentCol); //Pointp=this.getLocation(); /*m.table.remove(p); p.x+=x; p.y+=y; this.setLocation(p); m.container.setComponentZOrder(this,0); m.table.put(p,this); if(card!=null) card.moving(x,y);//遞歸*/ } publicvoidturnFront(){ this.setIcon(newImageIcon("images/"+name+".gif")); this.up="front"; } publicvoidturnRear(){ this.setIcon(newImageIcon("images/rear.gif")); this.up="rear"; } //錯誤地方回滾 publicvoidrollback(MouseEvente){ Cardcard=this; Pointlocal=card.getLocation(); this.moveto(local,oldPoint); //System.out.println(this.getLocation().toString()); } //獲取當(dāng)前卡片是第幾列(1-7) publicintgetCol(){ PointnowPoint=this.getLocation(); intxx=nowPoint.x,col=0; for(inti=1;i<=7;i++){ if(Math.abs(xx-50-100*(i-1))<80){ col=i; break; } } returncol; } publicCardgetLastCard(){ Cardcard=null,lastcard=null; intcol=this.getCol();//當(dāng)前列 intx=50+100*(col-1),y=180; //獲取第一個 card=m.table.get(newPoint(x,y)); lastcard=card; while(card!=null){ lastcard=card; card=card.getNext(card.getLocation()); } returnlastcard; } publicbooleanCheck(){ //檢查范圍 CardlastCard=this.getLastCard(); intx=-1,y=-1; if(lastCard!=null){ x=Math.abs(this.getLocation().x-lastCard.getLocation().x); y=Math.abs(this.getLocation().y-lastCard.getLocation().y); } if(!((x>=0)&&(x<40)&&(y>=0)&&(y<40))){ System.out.println("范圍出錯"); returnfalse; } //檢查花色 //黑1紅2梅3方4 Stringcur=,last=lastC; intcolor1=Integer.valueOf(cur.substring(0,1)); intcolor2=Integer.valueOf(last.substring(0,1)); //System.out.println("color:"+color1+","+color2); if(Math.abs(color1-color2)%2==0){ System.out.println("colorwrong"); returnfalse; } //檢查大小 intnum1=Integer.valueOf(cur.substring(2,cur.length())); intnum2=Integer.valueOf(last.substring(2,last.length())); //System.out.println("num:"+num1+","+num2); if(num2-num1!=1){ System.out.println("num1-num2!=1"); returnfalse; } //檢查牌是否翻開 if(lastCard.up=="rear"){ System.out.println("lastcardup==rear"); returnfalse; } returntrue; } //判斷當(dāng)前是不是待發(fā)牌堆 publicbooleanisCards(){ if(this.getLocation().x==600&&this.getLocation().y==30){ returntrue; }else returnfalse; } //判斷當(dāng)前是不是在4個框框內(nèi) publicbooleanisFour(){ Pointpoint=this.getLocation(); if(this.getNext(this.getLocation())!=null) returnfalse; intx=-1,y=-1; x=Math.abs(point.x-(50+100*(this.getCol()-1))); y=Math.abs(point.y-30); if((x>=0&&x<=40)&&(y>=0&&y<=40)){ System.out.println(x+","+y); returntrue; }else{ returnfalse; } } //放入框框 publicbooleanlocateFour(){ //框框是空的時候只能放A Cardcard=null; Pointpoint=newPoint(50+100*(this.getCol()-1),30); card=m.table.get(point); if(card==null){ Strings=.substring(2,.length()); if(!s.equals("1")){ System.out.println(s+"不是A"); returnfalse;//不是A } m.table.remove(this.getLocation()); this.setLocation(point); System.out.println(point.toString()); m.table.put(point,this); returntrue; }else{ //說明//框框不是空的時候由小到大,相同花色 Strings1=.substring(0,1); Strings2=.substring(0,1); intnum1=Integer.valueOf(.substring(2, .length())); intnum2=Integer.valueOf(.substring(2, .length())); if(!((s1.equals(s2))&&(num1-num2==1))) { System.out.println("花色不同"); returnfalse; } else{ m.table.remove(this.getLocation()); this.setLocation(point); System.out.println(point.toString()); m.table.put(point,this); returntrue; } } } publicbooleanisK(){ intx=-1,y=-1; Pointpoint=this.getLocation(); x=Math.abs(point.x-(50+100*(this.getCol()-1))); y=Math.abs(point.y-180); if(this.getLastCard()!=null) { System.out.println("當(dāng)前K下面不為空"); returnfalse; } if(!((x>=0&&x<=40)&&(y>=0&&y<=40))){ System.out.println("區(qū)域錯誤"); returnfalse; } Stringstring=.substring(2,.length()); if(!string.equals("13")) { System.out.println("不是K"); returnfalse; } Pointpoint2=newPoint((50+100*(this.getCol()-1)),180); this.moveto(this.getLocation(),point2); returntrue; } @Override publicvoidmouseDragged(MouseEvente){ //TODOAuto-generatedmethodstub if(this.canmove){ Cardcard=this; Pointnow=e.getPoint(); intx,y; x=now.x-point.x; y=now.y-point.y; moving(x,y); } } @Override publicvoidmousePressed(MouseEvente){ //TODOAuto-generatedmethodstub point=e.getPoint(); oldPoint=this.getLocation(); currentCol=this.getCol(); m.dragList=newArrayList<Card>(); if(this.canmove) { m.dragList.add(this); Cardcard=this.getNext(this.getLocation()); while(card!=null) { System.out.println("list:"+); if(card.canmove) m.dragList.add(card); card=card.getNext(card.getLocation()); } } } @Override publicvoidmouseReleased(MouseEvente){ //TODOAuto-generatedmethodstub CardlastCard=this.getLastCard(); if(lastCard!=null&&lastCard.canmove==false){ if(lastCard.up=="rear"&&lastCard==this){ lastCard.canmove=true; lastCard.turnFront(); }else{ rollback(e); } } if(this.canmove)//如何能移動 { //當(dāng)移動最上面4個框框時 if(this.isFour()&&this.locateFour())//將放入四個框框 { if(oldPoint.x

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論