游戏的JAVA源代码.docx

上传人:b****7 文档编号:23397506 上传时间:2023-05-16 格式:DOCX 页数:14 大小:61.93KB
下载 相关 举报
游戏的JAVA源代码.docx_第1页
第1页 / 共14页
游戏的JAVA源代码.docx_第2页
第2页 / 共14页
游戏的JAVA源代码.docx_第3页
第3页 / 共14页
游戏的JAVA源代码.docx_第4页
第4页 / 共14页
游戏的JAVA源代码.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

游戏的JAVA源代码.docx

《游戏的JAVA源代码.docx》由会员分享,可在线阅读,更多相关《游戏的JAVA源代码.docx(14页珍藏版)》请在冰豆网上搜索。

游戏的JAVA源代码.docx

游戏的JAVA源代码

游戏2048的JAVA源代码

一.文档说明

a)本代码主要功能为实现2048游戏,GUI界面做到尽量简洁和原游戏相仿。

目前版本不包含计分,计时功能。

b)本代码受计算机系大神指点,经许可后发布如下,向Java_online网致敬

c)运行时请把.java文件放入Game_one文件夹中方可运行(没有的自己在workplacesrc中创建)或者直接删去第一个packageGame_one;代码即可。

二.运行截图

a)初始状态

b)进入游戏

c)游戏进行中

 

三.JAVA代码

packageGame_one;

importjava.awt.*;

importjava.awt.event.*;

importjava.util.Random;

importjavax.swing.*;

publicclassGame2048extendsJPanel{

enumState{

start,won,running,over

}

finalColor[]colorTable={

newColor(0x701710),newColor(0xFFE4C3),newColor(0xfff4d3),

newColor(0xffdac3),newColor(0xe7b08e),newColor(0xe7bf8e),

newColor(0xffc4c3),newColor(0xE7948e),newColor(0xbe7e56),

newColor(0xbe5e56),newColor(0x9c3931),newColor(0x701710)};

finalstaticinttarget=2048;

staticinthighest;

staticintscore;

privateColorgridColor=newColor(0xBBADA0);

privateColoremptyColor=newColor(0xCDC1B4);

privateColorstartColor=newColor(0xFFEBCD);

privateRandomrand=newRandom();

privateTile[][]tiles;

privateintside=4;

privateStategamestate=State.start;

privatebooleancheckingAvailableMoves;

publicGame2048(){

setPreferredSize(newDimension(900,700));

setBackground(newColor(0xFAF8EF));

setFont(newFont("SansSerif",Font.BOLD,48));

setFocusable(true);

addMouseListener(newMouseAdapter(){

@Override

publicvoidmousePressed(MouseEvente){

startGame();

repaint();

}

});

addKeyListener(newKeyAdapter(){

@Override

publicvoidkeyPressed(KeyEvente){

switch(e.getKeyCode()){

caseKeyEvent.VK_UP:

moveUp();

break;

caseKeyEvent.VK_DOWN:

moveDown();

break;

caseKeyEvent.VK_LEFT:

moveLeft();

break;

caseKeyEvent.VK_RIGHT:

moveRight();

break;

}

repaint();

}

});

}

@Override

publicvoidpaintComponent(Graphicsgg){

super.paintComponent(gg);

Graphics2Dg=(Graphics2D)gg;

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

drawGrid(g);

}

voidstartGame(){

if(gamestate!

=State.running){

score=0;

highest=0;

gamestate=State.running;

tiles=newTile[side][side];

addRandomTile();

addRandomTile();

}

}

voiddrawGrid(Graphics2Dg){

g.setColor(gridColor);

g.fillRoundRect(200,100,499,499,15,15);

if(gamestate==State.running){

for(intr=0;r

for(intc=0;c

if(tiles[r][c]==null){

g.setColor(emptyColor);

g.fillRoundRect(215+c*121,115+r*121,106,106,7,7);

}else{

drawTile(g,r,c);

}

}

}

}else{

g.setColor(startColor);

g.fillRoundRect(215,115,469,469,7,7);

g.setColor(gridColor.darker());

g.setFont(newFont("SansSerif",Font.BOLD,128));

g.drawString("2048",310,270);

g.setFont(newFont("SansSerif",Font.BOLD,20));

if(gamestate==State.won){

g.drawString("youmadeit!

",390,350);

}elseif(gamestate==State.over)

g.drawString("gameover",400,350);

g.setColor(gridColor);

g.drawString("clicktostartanewgame",330,470);

g.drawString("(usearrowkeystomovetiles)",310,530);

}

}

voiddrawTile(Graphics2Dg,intr,intc){

intvalue=tiles[r][c].getValue();

g.setColor(colorTable[(int)(Math.log(value)/Math.log

(2))+1]);

g.fillRoundRect(215+c*121,115+r*121,106,106,7,7);

Strings=String.valueOf(value);

g.setColor(value<128?

colorTable[0]:

colorTable[1]);

FontMetricsfm=g.getFontMetrics();

intasc=fm.getAscent();

intdec=fm.getDescent();

intx=215+c*121+(106-fm.stringWidth(s))/2;

inty=115+r*121+(asc+(106-(asc+dec))/2);

g.drawString(s,x,y);

}

privatevoidaddRandomTile(){

intpos=rand.nextInt(side*side);

introw,col;

do{

pos=(pos+1)%(side*side);

row=pos/side;

col=pos%side;

}while(tiles[row][col]!

=null);

intval=rand.nextInt(10)==0?

4:

2;

tiles[row][col]=newTile(val);

}

privatebooleanmove(intcountDownFrom,intyIncr,intxIncr){

booleanmoved=false;

for(inti=0;i

intj=Math.abs(countDownFrom-i);

intr=j/side;

intc=j%side;

if(tiles[r][c]==null)

continue;

intnextR=r+yIncr;

intnextC=c+xIncr;

while(nextR>=0&&nextR=0&&nextC

Tilenext=tiles[nextR][nextC];

Tilecurr=tiles[r][c];

if(next==null){

if(checkingAvailableMoves)

returntrue;

tiles[nextR][nextC]=curr;

tiles[r][c]=null;

r=nextR;

c=nextC;

nextR+=yIncr;

nextC+=xIncr;

moved=true;

}elseif(next.canMergeWith(curr)){

if(checkingAvailableMoves)

returntrue;

intvalue=next.mergeWith(curr);

if(value>highest)

highest=value;

score+=value;

tiles[r][c]=null;

moved=true;

break;

}else

break;

}

}

if(moved){

if(highest

clearMerged();

addRandomTile();

if(!

movesAvailable()){

gamestate=State.over;

}

}elseif(highest==target)

gamestate=State.won;

}

returnmoved;

}

booleanmoveUp(){

returnmove(0,-1,0);

}

booleanmoveDown(){

returnmove(side*side-1,1,0);

}

booleanmoveLeft(){

returnmove(0,0,-1);

}

booleanmoveRight(){

returnmove(side*side-1,0,1);

}

voidclearMerged(){

for(Tile[]row:

tiles)

for(Tiletile:

row)

if(tile!

=null)

tile.setMerged(false);

}

booleanmovesAvailable(){

checkingAvailableMoves=true;

booleanhasMoves=moveUp()||moveDown()||moveLeft()||moveRight();

checkingAvailableMoves=false;

returnhasMoves;

}

publicstaticvoidmain(String[]args){

SwingUtilities.invokeLater(()->{

JFramef=newJFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setTitle("2048");

f.setResizable(true);

f.add(newGame2048(),BorderLayout.CENTER);

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

});

}

}

classTile{

privatebooleanmerged;

privateintvalue;

Tile(intval){

value=val;

}

intgetValue(){

returnvalue;

}

voidsetMerged(booleanm){

merged=m;

}

booleancanMergeWith(Tileother){

return!

merged&&other!

=null&&!

other.merged&&value==other.getValue();

}

intmergeWith(Tileother){

if(canMergeWith(other)){

value*=2;

merged=true;

returnvalue;

}

return-1;

}

}

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

当前位置:首页 > 初中教育 > 中考

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

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