欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java编写的简单移动方块小游戏代码

程序员文章站 2024-03-08 16:12:10
本文实例讲述了java编写的简单移动方块小游戏代码。分享给大家供大家参考,具体如下: 运行效果截图如下: 第一次用java编写图形化的界面,还是有些青涩。。以后继续...

本文实例讲述了java编写的简单移动方块小游戏代码。分享给大家供大家参考,具体如下:

运行效果截图如下:

java编写的简单移动方块小游戏代码

第一次用java编写图形化的界面,还是有些青涩。。以后继续努力!!具体代码如下:

//little box game by alexyui
//game.java by 1093710210@ hit
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.*;
import java.util.*;
public class game{
 public static void main(string[] agrs){
  myframe gameframe = new myframe();
  gameframe.settitle("java box game by alexyui");
  gameframe.setdefaultcloseoperation(jframe.exit_on_close);
  gameframe.setvisible(true);
 }
}
//框架
class myframe extends jframe {
 private int flag = 0;
 protected double x[] = {0,0,270,0,270};
 protected double y[] = {0,0,0,270,270}; 
 protected int winner[]={0,0,0,0,0};
 public static final int default_width = 317;
 public static final int default_height = 339;
 public myframe(){
 mypanel a = new mypanel();
 addkeylistener(new mykeylistener());
 this.add(a);
 setsize(default_width,default_height);
 }
class mypanel extends jpanel{
 public mypanel(){}
 public void paint(graphics g){
 super.paint(g);
 graphics2d g2 = (graphics2d)g;
 rectangle2d rect1 = new rectangle2d.double(x[1],y[1],30f,30f);
 rectangle2d rect2 = new rectangle2d.double(x[2],y[2],30f,30f);
 rectangle2d rect3 = new rectangle2d.double(x[3],y[3],30f,30f);
 rectangle2d rect4 = new rectangle2d.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! you won!",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();
 }
}
 class mykeylistener implements keylistener{
   //overide
   public void keypressed(keyevent event){ 
  int keycode = 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();}
     }
   public void keyreleased(keyevent event){}
   public void keytyped(keyevent event){}
   }
   //用于判别基本规则,移动方块
   void move(int mx,int my,int mflag){
     int i;
     boolean teller = 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;
       }
     }
     //用于判别已经进入符合条件区域的方块
     void win(){
       int j1,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程序设计有所帮助。