java課程設(shè)計(jì)貪吃蛇游戲設(shè)計(jì)_第1頁(yè)
java課程設(shè)計(jì)貪吃蛇游戲設(shè)計(jì)_第2頁(yè)
java課程設(shè)計(jì)貪吃蛇游戲設(shè)計(jì)_第3頁(yè)
java課程設(shè)計(jì)貪吃蛇游戲設(shè)計(jì)_第4頁(yè)
java課程設(shè)計(jì)貪吃蛇游戲設(shè)計(jì)_第5頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余9頁(yè)可下載查看

下載本文檔

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

文檔簡(jiǎn)介

1、刖百Java最初被命名為 Oak,目標(biāo)設(shè)定在家用電器等小型系統(tǒng)的編程語(yǔ)言,來(lái)解決諸 如電視機(jī)、電話、鬧鐘、烤面包機(jī)等家用電器的控制和通訊問(wèn)題。由于這些智能化家 電的市場(chǎng)需求沒(méi)有預(yù)期的高,Sun放棄了該項(xiàng)計(jì)劃。就在Oak幾近失敗之時(shí),隨著互聯(lián)網(wǎng)的發(fā)展,Sun看到了 Oak在計(jì)算機(jī)網(wǎng)絡(luò)上的廣闊應(yīng)用前景。于是改造了Oak,以“Java”的名稱正式發(fā)布。Java編程語(yǔ)言的風(fēng)格十分接近C、C+語(yǔ)言。Java是一個(gè)純的面向?qū)ο蟮某绦蛟O(shè)計(jì)語(yǔ)言,它繼承了 C+語(yǔ)言面向?qū)ο蠹夹g(shù)的核心。Java舍棄了 C +語(yǔ)言中容易引起錯(cuò)誤的指針(以引用取代)、運(yùn)算符重載( operator overloading )、多重繼

2、承(以 接口取代)等特性,增加了垃圾回收器功能用于回收不再被引用的對(duì)象所占據(jù)的內(nèi)存空間,使得程序員不用再為內(nèi)存管理而擔(dān)憂。在Java SE 1.5版本中,Java又引入了泛型編程(Generic Programming )、類型安全的枚舉、不定長(zhǎng)參數(shù)和自動(dòng)裝/拆箱等語(yǔ)言特性。Java不同于一般的編譯執(zhí)行計(jì)算機(jī)語(yǔ)言和解釋執(zhí)行計(jì)算機(jī)語(yǔ)言。它首先將源代碼編譯成二進(jìn)制字節(jié)碼( bytecode ),然后依賴各種不同平臺(tái)上的虛擬機(jī)來(lái)解釋執(zhí)行字節(jié)碼。從而實(shí)現(xiàn)了 “一次編譯、到處執(zhí)行”的跨平臺(tái)特性。不過(guò),每次的編譯執(zhí)行需要消耗一定的時(shí)間,這同時(shí)也在一定程度上降低了Java 程序的運(yùn)行效率。但在J2SE 1.

3、4.2發(fā)布后,Java的執(zhí)行速度有了大幅提升。與傳統(tǒng)程序不同,Sun公司在推出 Java 之際就將其作為一種開(kāi)放的技術(shù)。全球數(shù)以萬(wàn)計(jì)的 Java 開(kāi)發(fā)公司被要求所設(shè)計(jì)的Java 軟件必須相互兼容。"Ja va語(yǔ)言靠群體的力量而非公司的力量”是Sun公司的口號(hào)之一,并獲得了廣大軟件開(kāi)發(fā)商的認(rèn)同。這與微軟公司所倡導(dǎo)的注重精英和封閉式的模式完全不同。Sun公司對(duì)Java 編程語(yǔ)言的解釋是:Java編程語(yǔ)言是個(gè)簡(jiǎn)單、面向?qū)ο?、分布式、解釋性、健壯、安全與系統(tǒng)無(wú)關(guān)、可移植、高性能、多線程和動(dòng)態(tài)的語(yǔ)言。f back e to s you hwork ul play you hempi oyees

4、 i i- p-" Cmm-nagats corruin work leve, bu-e ss- ,Any TUrthe the over, ega syse>. must streghe managebl-h a nd m prove suev_n, ad >Sm.o cnsCniiSy .the Oympc _ cuiy cntrl manage higher"、he .* a higher le-l of deve I Employee* today s lunar e on Dee -a atths tme of ye. ary fee geeaia c

5、eayhe a xx power breathing .ng p-t one正文1.1課程設(shè)計(jì)目的JAVA程序設(shè)計(jì)是計(jì)算機(jī)相關(guān)專業(yè)的必修專業(yè)基礎(chǔ)課程,其實(shí)踐性、應(yīng)用性很強(qiáng)。 實(shí)踐教學(xué)環(huán)節(jié)是必不可少的一個(gè)重要環(huán)節(jié)。本課程的程序設(shè)計(jì)專題實(shí)際是計(jì)算機(jī)相關(guān)專業(yè)學(xué)生學(xué)習(xí)完JAVA程序設(shè)計(jì)課程后,進(jìn)行的一次全面的綜合訓(xùn)練,JAVA程序設(shè)計(jì)的設(shè)計(jì)目的是加深對(duì)理論教學(xué)內(nèi)容的理解和掌握,使學(xué)生較系統(tǒng)地掌握程序設(shè)計(jì)及其在網(wǎng)絡(luò)開(kāi)發(fā)中的廣泛應(yīng)用,基本方法及技巧,為學(xué)生綜合運(yùn)用所學(xué)知識(shí),利用軟件工程為基礎(chǔ)進(jìn)行軟件開(kāi)發(fā)、并在實(shí)踐應(yīng)用方面打下一定基礎(chǔ)。2.1 工程概況2.1.1 案例提由及工程用時(shí)經(jīng)過(guò)兩天在圖書(shū)館以及網(wǎng)絡(luò)上

6、查閱的資料,然后提出這次課程設(shè)計(jì)的目標(biāo)貪吃蛇游 戲的設(shè)計(jì)。然后接下來(lái)就是查閱更多的資料,制定接下來(lái)的詳細(xì)計(jì)劃,最終計(jì)劃用兩周的 時(shí)間完成此次課程設(shè)計(jì)提出的案例。2.1.2 開(kāi)發(fā)環(huán)境硬件環(huán)境:CPU Intel Pentium ( RR 4主頻:2.40GHz內(nèi)存:256MB軟件環(huán)境:操作系統(tǒng):Windows XP編程環(huán)境:JDK 1.6開(kāi)發(fā)工具:Eclipse jdkf back e to s you hwork ul play you hempi oyees i i- p-" Cmm-nagats corruin work leve, bu-e ss- ,Any T uie - n

7、.en theover, ega syse>. must streghe managebl-h a nd m prove suev_n, ad >Sm.o cnsCniiSy .the Oympc _ cuiy cntrl manage higher"、 he .* a higher le-l of deve I Employee* today s lunar e on Dee -a atths tme of ye. ary fee geeaia ceayhe a xx power breathing .ng p-t onea ossa al ng and ful of

8、nfienceF uure unities - moe -.ng . - tgehe ulof ale ngs aesto rae a gee, l ow s opeai on, ofa worda - powe ge nea-n mpay and w ork had! I heoas my sieews ood 3.1 設(shè)計(jì)方案論證3.1.1 設(shè)計(jì)思路貪吃蛇游戲設(shè)計(jì)與實(shí)現(xiàn), 主要分為以下二個(gè)模塊: 游戲主界面模塊和游戲控制模塊。 在 此只實(shí)現(xiàn)游戲的游戲主界面模塊、游戲控制模塊。并且只是實(shí)現(xiàn)移動(dòng)速度、暫停、重新開(kāi)始等功能。游戲主界面模塊:游戲的主界面是進(jìn)入游戲后,能夠給玩家第一感官的部分,主要

9、包括游戲圖形區(qū)域界面、 游戲的速度選擇更新界面、新生方塊的預(yù)覽、暫停游戲按鈕、重新開(kāi)始游戲按鈕。從很大程度上決定了玩家對(duì)游戲的興趣問(wèn)題,因此,游戲的主界面應(yīng)該力求美觀,爽心悅目。游戲控制模塊:這個(gè)模塊是游戲的中心環(huán)節(jié),主要完成控制游戲的開(kāi)始、暫停、重新開(kāi)始等功能。為了能夠給玩家一個(gè)很好的游戲環(huán)境,這部分應(yīng)該做到易懂、易操作。本設(shè)計(jì)所開(kāi)發(fā)的是基于 Java的一個(gè)貪吃蛇游戲軟件,主要實(shí)現(xiàn)游戲的速度控制、游戲的暫停、游戲的退出等功能,分為 :游戲主界面模塊、游戲控制模塊以二個(gè)模塊。3.1.2 程序概述F3鍵即本程序是一個(gè)利用 Java應(yīng)用軟件制作的貪食蛇小游戲。在游戲過(guò)程中,用戶通過(guò)點(diǎn)擊小鍵盤區(qū)的方

10、向鍵來(lái)控制蛇的運(yùn)行方向;游戲運(yùn)行過(guò)程中,如果想暫停,只需要按 可。重新開(kāi)始游戲的按鍵為 F2。當(dāng)蛇沒(méi)有吃到食物且碰到墻壁或自己的身體時(shí)游戲結(jié)束。本程序的另一個(gè)功能是在程序運(yùn)行窗口的左上角顯示,用戶在游戲過(guò)程中的移動(dòng)和長(zhǎng)3.1.3 程序流程圖本次游戲設(shè)計(jì)的處理流程如圖 1所示。3圖1.程序流程圖3.1.4 總設(shè)計(jì)模塊的劃分根據(jù)游戲需求中游戲所要實(shí)現(xiàn)的功能,整個(gè)游戲分為二個(gè)模塊進(jìn)行具體的設(shè)計(jì),如圖 所示。 brrating Real ng p_t onef back e to s you hwork ul play you hempi oyees i i- p-" Cmm-nagats

11、corruin work leve, bu-e ss- ,Any T ui- ste n.en theover, ega syse>. must streghe managebl-h a nd m prove suev_n, ad >Sm.o cnsCniiSy .the Oympc _ cuiy cntrl manage higher"、 he .* a higher le-l of deve IEmployees today s lunar e on Dee -a atths tme of ye. ary fee geeaia ceayhe a xx power a

12、ossa al ng and ful of nfienceF uure unities - moe -.ng . - tgehe ulof ale ngs aesto rae a gee, l ow s opeai on, ofa worda - powe ge nea-n mpay and w ork had! I heoas my sieews ood 貪吃蛇游戲游戲主界面模塊游戲控制模塊圖2.設(shè)計(jì)總模塊游戲控制模塊的主要框架如圖 3所示。游戲控制模塊游戲的暫停功能游戲的重新開(kāi)始游戲的退出圖3.控制模塊框架游戲暫??刂疲狐c(diǎn)擊“ F3”按鈕,游戲暫停;再單擊,游戲繼續(xù)。游戲重新開(kāi)始控制:點(diǎn)擊

13、“ F2”鍵后,點(diǎn)擊確定,即游戲重新開(kāi)始。游戲退出控制:點(diǎn)擊“ F4”鍵好,點(diǎn)擊確定,退出游戲界面。f bacbone backone rol e;to ful ste ngthenig member s yout hwk, ul play youth empl oyees i company ve opme nt i Ie of force roet mprovei ndepe de nt C.mmin . crupIon wk levlsregte ning in e nteprse buSne - ky ik of efetvees monite d , And miantatmb&qu

14、ot;y. To fute se ngghe npublciy a nd e dbliS a id m p. te e ducaton, iupenisin, and i i on ni_.connsa “ou up te Oy mpc - cury cntol s, promoigi ntgatd m.ng-et t ahige eve, hgher stadia- ahghe eve of deve opment. Emplooee, t s ua cal . on be 24, t he ox Bl s abou t riI g, a ts tme of yea, ce ay fee t

15、e puse of the X pe .neaioncmpay t olur s, to moe ceay he ar XX po r geeai in c ompa niis maue a nd lsmmetybr - hing. Raal ng pa. oneaote - os a ra1ng we ae e “u a nd ul I f icnlideie. F uue opmes moe eciig ” more si - dEmploee, le - tgeher acoos ,3 ful of c ngm aduntIs, to c e a gren, l owcs I peat

16、on, ulofhhmae cae of a word casspowe ge neain compay aoc Spri ng IFstval my sinceeh tta yu a I d teffm of te staf i the new yar g3.1.5 類、方法、屬性說(shuō)明(1)成員變量表i主要成員變量(屬性)成員變量描述變量類型名稱文件菜單中子菜單 “新建”JMenuItemmFile_New編輯菜單中子菜單 “撤消”JMenuItemmEdit_Undo格式菜單中子菜單 “顏色”JMenuformatMenu_Color幫助菜單中子菜單 “幫助主題”MenuitemmHelp

17、_HelpTopics(2)方法表2主要方法方法名功能備注Notepad4()描述記事本無(wú)Text.setBackground(Color.black)設(shè)置編輯區(qū)默認(rèn) 背景色無(wú)Text.setForeground(Color.black)設(shè)置編輯區(qū)默認(rèn) 前景色無(wú)4.1 設(shè)計(jì)結(jié)果與分析本程序在運(yùn)行后,能夠正常的相應(yīng)按鍵事件。由于程序的主要部分已經(jīng)集成為一個(gè)對(duì)象SnakeModel,所以利用鍵盤就可以實(shí)現(xiàn)操作。蛇前行的過(guò)程中,可以任意向其左或右方向改變方向。點(diǎn)擊 F3鍵后游戲就暫彳了。點(diǎn)擊 F2鍵游戲重新開(kāi)始。點(diǎn)擊 F4游戲退出。運(yùn)行結(jié)果如下圖4所示:|MyPaintBI3BI設(shè)置壅度幫助開(kāi)始游戲

18、F2暫停/磁續(xù)F3退出F4移動(dòng):5圖4程序運(yùn)行圖當(dāng)蛇沒(méi)有吃到食物且碰到邊界或碰撞到自己的身體時(shí),游戲結(jié)束。如果想重新開(kāi)始即點(diǎn)擊“確定”后按F2鍵,運(yùn)行結(jié)果如下圖 5所示:f bacbone ba ckone rol e;to ful ste ngthenig member s yout hwk, ul play youth empl oyees i company ve opme nt i Ie of force roe ; t mprove i ndepe de nt C.mm.nagats crrutonwok evel,sregthe ning in e nteprSe buSne ss

19、 ky ik of e eess monite d , And maintatb" y. To further se ngghe n publcly a nd e in, improve theoveai lega system. <W must steghe safety maagemet, eSablis a id m prove Ie e d_aion, supervi sin, and evaluati on as one if the tafc management meCsce i lousy sum up Ie Oy mpc se curiy co

20、ntrol s, pr omoigi ntegated management tahigher level hgher sta.ar- a hgher eve of .eve opme nt. Empl oyees, tdy s lunar cal e.a oncm be 24, t he ox Bel s about trii g, a ths time of year, ce ay fee Ie pUSe of the iX power generaion cmpay t o.u. s, to more ceary he ar XX power geneai in c ompa nies

21、mature a nd symmetybr hi ng. Recal ng pas oneaote - os a ral ng, we ae e “u a nd ul I f ccnldece. Futue opmes - moe eciig ,ht more siiedEmploee, le - tgeher acroos *3 ful of cng- adtuniiis, to creae a ggren, l owcs .peat on, ulof h ae cae of a word cass powe ge neain compay aocccs Spring Fstval my s

22、iceewsh tta yu a I d teffmiis of te staf i the new ya. g7圖5重新開(kāi)始圖4.1.1 程序異常處理在蛇的運(yùn)行線程過(guò)程中,當(dāng)蛇撞到墻或自己的身體時(shí), 將拋出一個(gè)Exception類的異常對(duì)象,然后使用try-catch 去捕捉這個(gè)異常,異常的處理如下:tryThread.sleep(timelnterval); catch(Exception e)break;心得體會(huì)通過(guò)這次課程設(shè)計(jì),我對(duì)這學(xué)期所學(xué)的JAVA語(yǔ)言程序設(shè)計(jì)有一個(gè)更深刻的了解,將所學(xué)的知識(shí)應(yīng)用于實(shí)踐,由于所學(xué)知識(shí)有限,為了使游戲能夠?qū)崿F(xiàn)自己預(yù)先的構(gòu)想,我通過(guò)上網(wǎng)和去圖書(shū)館找程序,

23、比較好的流程圖及功能模塊,不斷閱讀修改代碼使程序達(dá)到預(yù)期所要實(shí)現(xiàn)的目標(biāo),完成課程設(shè)計(jì)后,可以感覺(jué)到自己對(duì)Java程序的又有了新的認(rèn)識(shí)??傊?,通過(guò)本次的課程設(shè)計(jì),使我平時(shí)學(xué)到的理論知識(shí)得到了很好地升華,理論與實(shí)際得到一次很好的結(jié)合,為我今后參加工作打下了堅(jiān)實(shí)的基礎(chǔ),使我受益匪淺。參考文獻(xiàn)1趙文靖編著.Java程序設(shè)計(jì)基礎(chǔ)與上機(jī)指導(dǎo).北京:清華大學(xué)出版社, 20062趙毅主編.跨平臺(tái)程序設(shè)at語(yǔ)言一一Java.西安:西安電子科技大學(xué)出版社,3王路群.Java高級(jí)程序設(shè)計(jì).北京:中國(guó)水利水電出版社, 20064雍俊海編著.Java程序設(shè)計(jì)習(xí)題集.北京:清華大學(xué)出版社,20065朱福喜編著.Java語(yǔ)

24、言習(xí)題與解析.北京:清華大學(xué)出版社,20066吳其慶編著.Java程序設(shè)計(jì)實(shí)例教程.北京:冶金工業(yè)出版社,2006f back e to s you hwork ul play you hempi oyees i i- p-" Cmm-nagats corruin work leve, bu-ess- ,Any T ui- ste n.en theover, ega syse>. must streghe managebl-h a nd m prove suev_n, ad >Sm.o cnsCniiSy .the Oympc _ cuiy cntrl manage hi

25、gher"、 he .* a higher le-l of deve IEmployees today s lunar e on Dee -a atths tme of ye. ary fee geeaia ceayhe a xx power breathing l ng p-t onea ossa al ng and ful of nfienceF unities - moe -.ng . - tgehe ulof ale ngs aesto rae a gee, l ow s opeai on, ofa worda - powe ge neain mpay and w ork h

26、ad! Theoas my sieews ood 程序代碼:-othe - os arm a nd ul .f -nlde-e.Fuue s -moe - g *si.一Emploee, le - tge her a-oos " ful of-ng_ aunt -,to -_ea ggren, l ow-s .peaton.ulof hhmae cae of a word -sspowe ge ay an.workad TheocSri ng Fstval my si-eewsh tta yarg9import java.awt.*;import javax.swing.*;impo

27、rt java.util.*;import java.awt.event.*;public class She extends JFrame implements ActionListener, WindowListener JLabel bt1;JLabel bt2;MyPaint mp;JMenuItem kaishi;JMenuItem zanting;JMenuItem tuichu;JRadioButton chu;JRadioButton zhong;JRadioButton gao;JRadioButton chao;JMenuItem guanyu;Font f;public

28、She() f =new Font("宋體",F(xiàn)ont. PLAIN12);mp = new MyPaint(); mp.setShe( this );JMenuBar caidan = new JMenuBar();JMenu shezhi = new JMenu("設(shè)置"); shezhi.setFont(f);JMenu nandu = new JMenu("難度"); nandu.setFont(f);JMenu bangzhu = new JMenu("幫助");bangzhu.setFont(f);ca

29、idan.add(shezhi);caidan.add(nandu);caidan.add(bangzhu);kaishi =new JMenuItem("開(kāi)始游戲F2");kaishi.setFont(f);kaishi.addActionListener( this ); f bacbone ba ckone rol e; to ful ste ngtenig member s yout hwok, ul play youth empl oyees i company ve opme nt roe ; timprove i ndepe de nt Cmminagats

30、crrutonwk sregthe ning “enterprise b-ne - ky .k of e “ monite d , And maintatb" y. To fule se ngghe n publcly a nd e .n, improve the overall lega system. must stegghe safety management eSabisa - m prove Ie e d_aton, supevi sin, and e_UI on -zanting = new JMenuItem("暫停/繼續(xù) F3");zanting.

31、setFont(f);zanting.addActionListener( this );tuichu = new JMenuItem("退出 F4"); tuichu.setFont(f);tuichu.addActionListener( this );chu = new JRadioButton(" 初級(jí),最快速 度 195”, true );chu.setFont(f);chu.addActionListener( this );zhong = new JRadioButton(" 中級(jí),最快 速度225");zhong.setFont

32、(f);zhong.addActionListener( this );gao = new JRadioButton(" 高級(jí),最快速 度 255");gao.setFont(f);gao.addActionListener( this );chao = new JRadioButton(" 超級(jí),最快 速度275");chao.setFont(f);chao.addActionListener( this );ButtonGroup bg = new ButtonGroup();bg.add(chu);bg.add(zhong);bg.add(gao)

33、;bg.add(chao);nandu.add(chu);nandu.add(zhong);nandu.add(gao); nandu.add(chao);guanyu = new JMenuItem(" 關(guān)于”); guanyu.setFont(f);guanyu.addActionListener( this );shezhi.add(kaishi);shezhi.add(zanting);shezhi.add(tuichu);bangzhu.add(guanyu); ni-. con- .tou upteOy mpc - curty - n、a- mang-etta hge e

34、v"、 hers- ahg he eveofdeve tdy s ua cal eda on -be sabou to ri .g,a tstme ofyea, ayfee pe gene_i on .mp.yt olu. nd merybreahi ng. al ng pa. onebt1 = new JLabel(" 長(zhǎng)度:0"); btl.setFont; bt2 = new JLabel(" 移動(dòng):5"); bt2.setFont;JPanel p = new JPanel(); p.setLayout( new GridLayout(

35、1,2); p.add(btl); p.add(bt2);this .addWindowListener( this );/ 添 加窗體監(jiān)聽(tīng)器this .setJMenuBar(caidan);this .add(p, BorderLayout.NORT)Hthis .add(mp); this .add( new JLabel(" "), BorderLayout. WES)Tthis .setSize(335, 385);this .setResizable( false );this .setLocation(300, 150);this .setTitle(&quo

36、t;MyPaint"); this .setVisible( true ); public void setDS( int a, int b) bt1.setText(" 長(zhǎng)度:"+ a); bt2.setText(" 移動(dòng):"+ b); public void setChu() chu.setSelected( true ); mp.setSD(300, 110);this .setDS(0, 305 - 300); chu.setEnabled( true ); zhong.setEnabled( true ); gao.setEnable

37、d( true ); chao.setEnabled( true ); public void setNanDu() chu.setEnabled(false );zhong.setEnabled( false ); gao.setEnabled(false );chao.setEnabled( false ); public voidactionPerformed(ActionEvent e) if (e.getSource() = zanting) mp.setZanTing();else if (e.getSource() = kaishi) mp.setKaiShi();else if

38、 (e.getSource() = tuichu) mp.setTuiChu();else if (e.getSource() = guanyu) JLabel gy = new JLabel(" 小游戲 貪 吃蛇 V1.0");gy.setFont(f);JOptionPane. showMessageDialog( this , gy);else if (e.getSource() = chu) mp.setSD(300, 110);this .setDS(0, 305 - 300);else if (e.getSource() = zhong) mp.setSD(25

39、0, 80);this .setDS(0, 305 - 250);else if (e.getSource() = gao) mp.setSD(200, 50);this .setDS(0, 305 - 200);else if (e.getSource() = chao) mp.setSD(150, 30);this .setDS(0, 305 - 150); public static void main(String args) new She();/* Method windowOpened * embersyuthw*" play ay re ik of , AmanSb&

40、quot;y.To fute se he -eymangeteblSa > m p- tee dti.n, .n-nism.n- ”.- -mup He Oy mp. se -ry n、a- mangettaIge ev"ghersaa, ahgIe eve.f eve t S ua cal ea on be sab.utri. g, a tstme of yea, - ce ayfee pe ,ene_ion ccmpay t. nd merybr - hing.R-al ng pa. .nea.te ac.ssa al ng, - a S - more - dig ” -

41、Ie ulof die , t.rae a gee, l ow. -e of awo- Ca- po-we ge neain.mpaya . wokha T leoca. my -iee w. t la iten- yea, g 11* parame */public void windowOpened(WindowEvent e) / TODOAdd your code herepublic voidwindowClosing(WindowEvent e) System. exit (0);public void windowClosed(WindowEvent e) /TODOAdd yo

42、ur code herepublic voidwindowIconified(WindowEvent e) /TODOAdd your code herepublic voidwindowDeiconified(WindowEvent e) /TODOAdd your code herepublic voidwindowActivated(WindowEvent e) /TODOAdd your code herepublic voidwindowDeactivated(WindowEvent e) /TODOAdd your code hereclass MyPaint extends JP

43、anel implements Runnable, KeyListener, FocusListener She s;Font f = new Font("宋體", Font. PLAIN 12);int x = 150;int y = 0;Random r = new Random();int sjx = r.nextInt(300)/10*10;int sjy = r.nextInt(300)/10*10;int fx = 2;int sw = 0; int sd = 300; int cs = 0; int d = 150;String zx = new String

44、1000; booleanzanting = false ;booleanzantingl = false ;booleansiwang =false ;booleanshenti =false ;public MyPaint() Thread t = new Thread( this ); t.start();this .addKeyListener( this );this .setFocusable( true );this .addFocusListener( this ); public void setShe(She ss) s = ss;public void setZanTin

45、g() if (zanting) zanting = false ; else zanting = true ; public void setKaiShi() JLabel ks = new JLabel(" 是否重新開(kāi) 始?"); ks.setFont(f);int a =JOptionPane. showConfirmDialog (s, ks);if (a = 0) x = 150;y = 0;sjx = r.nextInt(300)/10*10;sjy = r.nextInt(300)/10*10;fx = 2;sw = 0; sd = 300;cs = 0;if

46、 (x = Integer. parseInt (s0)&&zanting =false ;y = Integer. parseInt (s1) / 蛇撞siwang = false ;到自己身上了shenti =false ;siwang = true ;s.setChu();shenti =true ;s.setDS(sw, 305 - sd);if (sjx = Integer.parseInt (s0)&& sjy = Integer. parseInt (s1) /public void setTuiChu() 食物在蛇身體上JLabel tc = n

47、ewJLabel("確定退出嗎?g.setColor(Color.BLACK");g.fillRect(sjx, sjy, 10, 10);tc.setFont(f);g.setColor(Color.REDint a =continue ;JOptionPane. showConfirmDialog (s,tc);g.fillRect(Integer.parseInt (s0),if (a = 0) Integer. parseInt (s1), 10, 10);s.dispose();System. exit (0);public void run() while (

48、true ) public void setSD( int a, int b) cs+;sd = a;d = b;if (cs = 1000) cs = 0;public void paint(Graphics g) zxcs = x + "/" + y;switch (fx) case 1:super .paint(g);y-=10;上g.setColor(Color.BLACKbreak ;g.drawRect(0, 0, 300, 300);case 2:g.setColor(Color.BLACKy+=10;/ 下 break ;g.fillRect(sjx, sjy, 10, 10);case 3:g.setColor(Color.BLUE;x-=10;左break ;g.fillRect(x, y, 10, 10);case 4:g.setColor(Color.RED;x+=10;/ 右 break ;for ( int i = 1; i <= sw; i+) int j = cs - i;this

溫馨提示

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

評(píng)論

0/150

提交評(píng)論