志在指尖
用双手敲打未来

java游戏代码(编程一个最简单游戏代码)

java游戏代码
//LittleBoxGamebyAlexYui
//Game.javaBy1093710210@HIT
importjavax.swing.*;
importjava.awt.event.*;
importjava.awt.geom.*;
importjava.awt.*;
importjava.util.*;
publicclassGame{
publicstaticvoidmain(String[]agrs){
MyFramegameframe=newMyFrame();
gameframe.setTitle(“JavaBoxGameByAlexYui”);
gameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameframe.setVisible(true);
}
}
//框架
classMyFrameextendsJFrame{
privateintflag=0;
protecteddoublex[]={0,0,270,0,270};
protecteddoubley[]={0,0,0,270,270};
protectedintwinner[]={0,0,0,0,0};
publicstaticfinalintDEFAULT_WIDTH=317;
publicstaticfinalintDEFAULT_HEIGHT=339;
publicMyFrame(){
MyPanela=newMyPanel();
addKeyListener(newMyKeyListener());
this.add(a);
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
classMyPanelextendsJPanel{
publicMyPanel(){}
publicvoidpaint(Graphicsg){
super.paint(g);
Graphics2Dg2=(Graphics2D)g;
Rectangle2Drect1=newRectangle2D.Double(x[1],y[1],30f,30f);
Rectangle2Drect2=newRectangle2D.Double(x[2],y[2],30f,30f);
Rectangle2Drect3=newRectangle2D.Double(x[3],y[3],30f,30f);
Rectangle2Drect4=newRectangle2D.Double(x[4],y[4],30f,30f);
//画中间的四个方框
g2.setPaint(Color.RED);
g2.drawRect(120,120,30,30);
g2.drawRect(120,150,30,30);
g2.drawRect(150,120,30,30);
g2.drawRect(150,150,30,30);
//填充四个角上的方块
g2.setPaint(Color.BLUE);
if(winner[1]==0)g2.fill(rect1);
if(winner[2]==0)g2.fill(rect2);
if(winner[3]==0)g2.fill(rect3);
if(winner[4]==0)g2.fill(rect4);
g2.setPaint(Color.RED);
//将已经选择的方块涂成红色
if(flag==1)g2.fill(rect1);
if(flag==2)g2.fill(rect2);
if(flag==3)g2.fill(rect3);
if(flag==4)g2.fill(rect4);
g2.setPaint(Color.BLUE);
if(winner[1]==1)g2.fill(rect1);
if(winner[2]==1)g2.fill(rect2);
if(winner[3]==1)g2.fill(rect3);
if(winner[4]==1)g2.fill(rect4);
g2.setPaint(Color.RED);
if(winner[1]==1&&winner[2]==1&&winner[3]==1&&winner[4]==1)
g2.drawString(“Succes!YouWon!”,100,75);
//保持边框的颜色
g2.drawRect(120,120,30,30);
g2.drawRect(120,150,30,30);
g2.drawRect(150,120,30,30);
g2.drawRect(150,150,30,30);
repaint();
}
}
classMyKeyListenerimplementsKeyListener{
//overide
publicvoidkeyPressed(KeyEventevent){
intkeyCode=event.getKeyCode();
if(keyCode==KeyEvent.VK_1){flag=1;}
if(keyCode==KeyEvent.VK_2){flag=2;}
if(keyCode==KeyEvent.VK_3){flag=3;}
if(keyCode==KeyEvent.VK_4){flag=4;}
if(keyCode==KeyEvent.VK_UP){move(0,-30,flag);win();}
if(keyCode==KeyEvent.VK_DOWN){move(0,30,flag);win();}
if(keyCode==KeyEvent.VK_LEFT){move(-30,0,flag);win();}
if(keyCode==KeyEvent.VK_RIGHT){move(30,0,flag);win();}
}
publicvoidkeyReleased(KeyEventevent){}
publicvoidkeyTyped(KeyEventevent){}
}
//用于判别基本规则,移动方块
voidmove(intmx,intmy,intmflag){
inti;
booleanteller=false;
x[mflag]+=mx;
y[mflag]+=my;
if(!((x[flag]>=0&&x[flag]<=270)&&(y[flag]>=0&&y[flag]<=270))){
x[mflag]-=mx;
y[mflag]-=my;
}
for(i=1;i<5;i++){
if(mflag!=i)
if(x[mflag]==x[i]&&y[mflag]==y[i])
teller=true;
}
if(teller==true){
x[mflag]-=mx;
y[mflag]-=my;
}
}
//用于判别已经进入符合条件区域的方块
voidwin(){
intj1,j2;
for(j1=1;j1<5;j1++)
if((x[j1]<=150&&x[j1]>=120)&&(y[j1]<=150&&y[j1]>=120))
winner[j1]=1;
for(j2=1;j2<5;j2++)
if(!((x[j2]<=150&&x[j2]>=120)&&(y[j2]<=150&&y[j2]>=120)))
winner[j2]=0;
}
}
java游戏代码编程一个最简单游戏代码
连连看的小源码
packageLianliankan;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclasslianliankanimplementsActionListener
{
JFramemainFrame;//主面板
ContainerthisContainer;
JPanelcenterPanel,southPanel,northPanel;//子面板
JButtondiamondsButton[][]=newJButton[6][5];//游戏按钮数组
JButtonexitButton,resetButton,newlyButton;//退出,重列,重新开始按钮
JLabelfractionLable=newJLabel(“0”);//分数标签
JButtonfirstButton,secondButton;//分别记载两次62616964757a686964616fe59b9ee7ad9431333335326239被选中的按钮
intgrid[][]=newint[8][7];//储存游戏按钮方位
staticbooleanpressInformation=false;//判别是否有按钮被选中
intx0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV;//游戏按钮的方位坐标
inti,j,k,n;//消除方法操控
publicvoidinit(){
mainFrame=newJFrame(“JKJ连连看”);
thisContainer=mainFrame.getContentPane();
thisContainer.setLayout(newBorderLayout());
centerPanel=newJPanel();
southPanel=newJPanel();
northPanel=newJPanel();
thisContainer.add(centerPanel,”Center”);
thisContainer.add(southPanel,”South”);
thisContainer.add(northPanel,”North”);
centerPanel.setLayout(newGridLayout(6,5));
for(intcols=0;cols<6;cols++){
for(introws=0;rows<5;rows++){
diamondsButton[cols][rows]=newJButton(String.valueOf(grid[cols+1][rows+1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=newJButton(“退出”);
exitButton.addActionListener(this);
resetButton=newJButton(“重列”);
resetButton.addActionListener(this);
newlyButton=newJButton(“再来一局”);
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
publicvoidrandomBuild(){
intrandoms,cols,rows;
for(inttwins=1;twins<=15;twins++){
randoms=(int)(Math.random()*25+1);
for(intalike=1;alike<=2;alike++){
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0){
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms;
}
}
}
publicvoidfraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
publicvoidreload(){
intsave[]=newint[30];
intn=0,cols,rows;
intgrid[][]=newint[8][7];
for(inti=0;i<=6;i++){
for(intj=0;j<=5;j++){
if(this.grid[i][j]!=0){
save[n]=this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n>=0){
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0){
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=save[n];
n–;
}
mainFrame.setVisible(false);
pressInformation=false;//这儿必定要将按钮点击信息归为初始
init();
for(inti=0;i<6;i++){
for(intj=0;j<5;j++){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
publicvoidestimateEven(intplaceX,intplaceY,JButtonbz){
if(pressInformation==false){
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else{
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsg&&secondButton!=firstButton){
xiao();
}
}
}
publicvoidxiao(){//相同的情况下能不能消去。仔细分析,不一条条注释
if((x0==x&&(y0==y+1||y0==y-1))||((x0==x+1||x0==x-1)&&(y0==y))){//判别是否相邻
remove();
}
else{
for(j=0;j<7;j++){
if(grid[x0][j]==0){//判别榜首个按钮同行哪个按钮为空
if(y>j){//如果第二个按钮的Y坐标大于空按钮的Y坐标阐明榜首按钮在第二按钮左面
for(i=y-1;i>=j;i–){//判别第二按钮左边直到榜首按钮中心有没有按钮
if(grid[x][i]!=0){
k=0;
break;
}
else{k=1;}//K=1阐明通过了榜首次验证
}
if(k==1){
linePassOne();
}
}
if(y
for(i=y+1;i<=j;i++){//判别第二按钮左边直到榜首按钮中心有没有按钮
if(grid[x][i]!=0){
k=0;
break;
}
else{k=1;}
}
if(k==1){
linePassOne();
}
}
if(y==j){
linePassOne();
}
}
if(k==2){
if(x0==x){
remove();
}
if(x0
for(n=x0;n<=x-1;n++){
if(grid[n][j]!=0){
k=0;
break;
}
if(grid[n][j]==0&&n==x-1){
remove();
}
}
}
if(x0>x){
for(n=x0;n>=x+1;n–){
if(grid[n][j]!=0){
k=0;
break;
}
if(grid[n][j]==0&&n==x+1){
remove();
}
}
}
}
}
for(i=0;i<8;i++){//列
if(grid[i][y0]==0){
if(x>i){
for(j=x-1;j>=i;j–){
if(grid[j][y]!=0){
k=0;
break;
}
else{k=1;}
}
if(k==1){
rowPassOne();
}
}
if(x
for(j=x+1;j<=i;j++){
if(grid[j][y]!=0){
k=0;
break;
}
else{k=1;}
}
if(k==1){
rowPassOne();
}
}
if(x==i){
rowPassOne();
}
}
if(k==2){
if(y0==y){
remove();
}
if(y0
for(n=y0;n<=y-1;n++){
if(grid[i][n]!=0){
k=0;
break;
}
if(grid[i][n]==0&&n==y-1){
remove();
}
}
}
if(y0>y){
for(n=y0;n>=y+1;n–){
if(grid[i][n]!=0){
k=0;
break;
}
if(grid[i][n]==0&&n==y+1){
remove();
}
}
}
}
}
}
}
publicvoidlinePassOne(){
if(y0>j){//榜首按钮同行空按钮在左面
for(i=y0-1;i>=j;i–){//判别榜首按钮同左边空按钮之间有没按钮
if(grid[x0][i]!=0){
k=0;
break;
}
else{k=2;}//K=2阐明通过了第二次验证
}
}
if(y0
for(i=y0+1;i<=j;i++){
if(grid[x0][i]!=0){
k=0;
break;
}
else{k=2;}
}
}
}
publicvoidrowPassOne(){
if(x0>i){
for(j=x0-1;j>=i;j–){
if(grid[j][y0]!=0){
k=0;
break;
}
else{k=2;}
}
}
if(x0
for(j=x0+1;j<=i;j++){
if(grid[j][y0]!=0){
k=0;
break;
}
else{k=2;}
}
}
}
publicvoidremove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==newlyButton){
intgrid[][]=newint[8][7];
this.grid=grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(intcols=0;cols<6;cols++){
for(introws=0;rows<5;rows++){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
publicstaticvoidmain(String[]args){
lianliankanllk=newlianliankan();
llk.randomBuild();
llk.init();
}
}
//old998lines
//new318lines

未经允许不得转载:IT技术网站 » java游戏代码(编程一个最简单游戏代码)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载