广东海洋大学Java课程设计.docx
- 文档编号:28697600
- 上传时间:2023-07-19
- 格式:DOCX
- 页数:14
- 大小:200.64KB
广东海洋大学Java课程设计.docx
《广东海洋大学Java课程设计.docx》由会员分享,可在线阅读,更多相关《广东海洋大学Java课程设计.docx(14页珍藏版)》请在冰豆网上搜索。
广东海洋大学Java课程设计
本科生课程设计
课程名称
Java程序设计与开发技术课程设计
课程编号
j1620204
学号
201411671111
学生姓名
李逸凡
所在专业
信息管理与信息系统
所在班级
1141
指导教师
刘大召
成绩
教师签字
评语:
目录
摘要I
1需求分析1
1.1游戏需求1
2游戏设计2
2.1数据库逻辑结构设计2
2.2游戏界面设计............................................................................................................................3
3游戏实现3
3.1游戏界面实现3
3.2游戏功能实现6
3.3游戏结束标志实现10
摘要
在当今手机与pc电脑流行的的情况下,电子游戏市场拥有许多的发展潜力,游戏市场潜在价值已超过200亿,许多人都在玩游戏,特别是青少年。
所以本次我模仿手机版的2048,做了个pc版的2048.
规则,本游戏是通过键盘来实现方块的移动的(通过w,a,s,d或上下左右),每当向上移动时如果数字相同的相加,不同的累积(只能加一次),其他同理。
如果上下左右都不相同时游戏借宿
2048
1需求分析
2048是手机游戏中一款很火爆的游戏。
2游戏设计
1.数据库逻辑结构设计
2游戏实现
游戏界面
游戏界面代码
publicMy2048(){
super();
setResizable(false);//禁止调整窗体大小
getContentPane().setLayout(null);//设置空布局
setBounds(500,50,500,615);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("2048PC版");//设置窗体标题
scoresPane=newJPanel();//创建分数显示面板
scoresPane.setBackground(Color.green);//设置分数显示面板的背景色
scoresPane.setBounds(20,20,460,40);
scoresPane.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.YELLOW));//设置得分面板的边框
getContentPane().add(scoresPane);//将得分面板添加到窗体
scoresPane.setLayout(null);//设置面板空布局
labelMaxScores=newJLabel("最高分:
");//最高分标签
labelMaxScores.setFont(font);//设置字体类型和大小
labelMaxScores.setBounds(10,5,50,30);//设置最懂啊分标签的位置尺寸
scoresPane.add(labelMaxScores);//将最高分标签添加到得分容器中
textMaxScores=newJTextField("0");//得分标签
textMaxScores.setBounds(60,10,87,20);
textMaxScores.setFont(font);
textMaxScores.setEditable(false);
scoresPane.add(textMaxScores);//将得分标签添加到分数面板中
labelScores=newJLabel("得分:
");
labelScores.setFont(font);//设置字体类型和大小
labelScores.setBounds(175,5,50,30);
scoresPane.add(labelScores);
textScores=newJLabel(String.valueOf(scores));
textScores.setFont(font);
textScores.setBounds(234,10,58,20);
scoresPane.add(textScores);
choice=newChoice();
choice.setSize(50,25);
choice.setLocation(298,10);
scoresPane.add(choice);//玩法选择
for(inti=0;i choice.add(wanfa[i]); } choice.addItemListener(this); choice1=newChoice(); choice1.setSize(50,25); choice1.setLocation(372,10); scoresPane.add(choice1);//难度选择 for(inti=0;i choice1.add(nandu[i]); } choice1.addItemListener(this); mainPane=newJPanel();//创建游戏主面板 mainPane.setBounds(20,70,460,500);//设置主面板位置尺寸 this.getContentPane().add(mainPane); mainPane.setLayout(null);//设置空布局 texts=newJLabel[4][4];//创建文本框二维数组 for(inti=0;i<4;i++){//遍历数组 for(intj=0;j<4;j++){ texts[i][j]=newJLabel();//创建标签 texts[i][j].setFont(font2);//设置字体 texts[i][j].setHorizontalAlignment(SwingConstants.CENTER); texts[i][j].setText(""); texts[i][j].setBounds(120*j,120*i,100,100);//设置方块的大小位置 setColor(i,j,""); texts[i][j].setOpaque(true); texts[i][j].setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.green));//设置方块边框颜色 mainPane.add(texts[i][j]);//将创建的文本框放在 } } tips=newJLabel("Tips: 使用上、下、左、右键或者W、S、A、D键控制"); tips.setFont(font); tips.setBounds(60,480,400,20); mainPane.add(tips); db=newDB();//设计数据库连接类 textMaxScores.setText(""+db.geti()); 游戏功能实现代码 protectedvoiddo_label_keyPressed(finalKeyEvente){ intjianpan=e.getKeyCode();//获取按键代码 inta;//a的引入是为了防止连加的情况出现 Stringstr; Stringstr1; intnum; switch(jianpan){ caseKeyEvent.VK_LEFT: caseKeyEvent.VK_A: //如果按键代码是左方向键或者A键 for(inti=0;i<4;i++){ a=5; for(intk=0;k<3;k++){//查看下一个是否为空 for(intj=1;j<4;j++){//遍历16个方块 str=texts[i][j].getText();//获取当前方块标签文本字符 str1=texts[i][j-1].getText();//获取当前左1方块标签文本字符 if(pareTo("")==0){//如果左1方块文本为空字符 texts[i][j-1].setText(str);//字符左移 setColor(i,j-1,str); texts[i][j].setText("");//当前方块字符置空 setColor(i,j,""); }elseif((pareTo(str1)==0)&&(j! =a)&&(j! =a-1)){//如果当前方块和左1方块文本字符相等 num=Integer.parseInt(str); //scores+=2*num; //panduan=db.bijiao(scores); //panduan1(panduan);//更改标签的值 //times++; //str=String.valueOf(2*num); str=xuanzhe(num,str); texts[i][j-1].setText(str);//左1方块文本字符变为两方块之和 setColor(i,j-1,str); texts[i][j].setText("");//当前方块字符置空 setColor(i,j,""); a=j; } } } } l1=1;//用于判断游戏是否失败 canshengshu(); break; caseKeyEvent.VK_RIGHT: caseKeyEvent.VK_D: for(inti=0;i<4;i++){ a=5; for(intk=0;k<3;k++){ for(intj=2;j>=0;j--){ str=texts[i][j].getText(); str1=texts[i][j+1].getText(); if(pareTo("")==0){ texts[i][j+1].setText(str); setColor(i,j+1,str); texts[i][j].setText(""); setColor(i,j,""); } elseif(pareTo(str1)==0&&j! =a&&j! =a+1){ num=Integer.parseInt(str); //scores+=2*num; //panduan=db.bijiao(scores); //panduan1(panduan);//更改标签的值 //times++; //str=String.valueOf(2*num); str=xuanzhe(num,str); texts[i][j+1].setText(str); setColor(i,j+1,str); texts[i][j].setText(""); setColor(i,j,""); a=j; } } } } l2=1; canshengshu(); break; caseKeyEvent.VK_UP: caseKeyEvent.VK_W: for(intj=0;j<4;j++){ a=5; for(intk=0;k<3;k++){ for(inti=1;i<4;i++){ str=texts[i][j].getText(); str1=texts[i-1][j].getText(); if(pareTo("")==0){ texts[i-1][j].setText(str); setColor(i-1,j,str); texts[i][j].setText(""); setColor(i,j,""); } elseif(pareTo(str1)==0&&i! =a&&i! =a-1){ num=Integer.parseInt(str); //scores+=2*num; //panduan=db.bijiao(scores); //panduan1(panduan);//更改标签的值 //times++; //str=String.valueOf(2*num); str=xuanzhe(num,str); texts[i-1][j].setText(str); setColor(i-1,j,str); texts[i][j].setText(""); setColor(i,j,""); a=i; } } } } l3=1; canshengshu(); break; caseKeyEvent.VK_DOWN: caseKeyEvent.VK_S: for(intj=0;j<4;j++){ a=5; for(intk=0;k<3;k++){ for(inti=2;i>=0;i--){ str=texts[i][j].getText(); str1=texts[i+1][j].getText(); if(pareTo("")==0){ texts[i+1][j].setText(str); setColor(i+1,j,str); texts[i][j].setText(""); setColor(i,j,""); } elseif(pareTo(str1)==0&&i! =a&&i! =a+1){ num=Integer.parseInt(str); //scores+=2*num; //panduan=db.bijiao(scores); //panduan1(panduan);//更改标签的值 //times++; //str=String.valueOf(2*num); str=xuanzhe(num,str); texts[i+1][j].setText(str); setColor(i+1,j,str); texts[i][j].setText(""); setColor(i,j,""); a=i; } } } } l4=1; canshengshu(); break; default: break; } textScores.setText(String.valueOf(scores)); } 游戏结束判断代码 publicbooleanjieshu(){ intl=0,m=0; booleanbool_c=true; Stringstr,str1; booleanbool_a; for(inti=0;i<4;i++){ for(intj=1;j<4;j++){//遍历16个方块 str=texts[i][j].getText();//获取当前方块标签文本字符 str1=texts[i][j-1].getText();//获取当前左1方块标签文本字符 bool_a=str.equals(str1); if(bool_a){ l=1; break; } } if(l==1){ break; } } for(inti=1;i<4;i++){ for(intj=0;j<4;j++){//遍历16个方块 str=texts[i][j].getText();//获取当前方块标签文本字符 str1=texts[i-1][j].getText();//获取当前左1方块标签文本字符 bool_a=str.equals(str1); if(bool_a){ m=1; break; } } if(m==1){ break; } } if(m==1||l==1){ bool_c=false; } returnbool_c; } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 广东 海洋大学 Java 课程设计