别踩白块儿java代码_我的JAVA游戏之旅--别踩白块儿

package one;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Toolkit;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import java.util.Random;

public class MainTwo extends JFrame implements

MouseListener,Runnable {

public static int WIDTH =

Toolkit.getDefaultToolkit().getScreenSize().width;

public static int HEIGHT =

Toolkit.getDefaultToolkit().getScreenSize().height;

private int F_Width=400,F_Height=600;

private int block_r=100;

BufferedImage Backgroud=null;//背景

BufferedImage White_Img=null;

BufferedImage Black_Img = null;

BufferedImage Gray_Img=null;

BufferedImage

Right_up=null,Right_down=null,Lift_up=null,Lift_down=null;

BufferedImage GameMain=null,GameOver=null,GameStop=null;

BufferedImage trophy=null,result_ground=null;

Random Rd=new Random();

Thread time = new Thread(this);

private int[][] block=new int [30][2];//对应x,y

private int[][] block_color=new int [30][4];//对应颜色

private int[] run=new int[30];

private int block_num=0;

private int block_out=0,block_into=0;

private boolean

B_Main=false,B_Game=false,B_Mouse=false,B_Win=false,B_Donghua=true;

private boolean[] B_block=new boolean [30];

private int[] List_into=new int[30];

private int time_i=0;

private int Main_i=0;

private int Game_v=2;

private int Game_result=0,BestResult=0;

public static void main(String[] args) {

new MainTwo();

}

private MainTwo()

{

this.setTitle("黑白块");

this.setIconImage(Toolkit.getDefaultToolkit().createImage("Image/icon.png"));

this.setSize(F_Width, F_Height);

this.setLocation((WIDTH-F_Width)/2,

(HEIGHT-F_Height)/2);

this.setResizable(false);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口的同时关闭程序

this.setLayout(null);

{

try {

Backgroud=ImageIO.read(new

File("Image/blackgroud.jpg"));

White_Img=ImageIO.read(new File("Image/While.jpg"));

Black_Img=ImageIO.read(new File("Image/Black.jpg"));

Gray_Img=ImageIO.read(new File("Image/Gray.jpg"));

Right_up=ImageIO.read(new File("Image/Right_up.png"));

Right_down=ImageIO.read(new

File("Image/Right_down.png"));

Lift_up=ImageIO.read(new File("Image/Lift_up.png"));

Lift_down=ImageIO.read(new File("Image/Lift_down.png"));

GameMain=ImageIO.read(new File("Image/GameMain.png"));

GameOver=ImageIO.read(new File("Image/GameOver.png"));

GameStop=ImageIO.read(new File("Image/GameStop.png"));

result_ground=ImageIO.read(new File("Image/分数背景.png"));

} catch (IOException e) {

e.printStackTrace();

}

}

{//事件

this.addMouseListener(this);

}

inis_game();//初始化游戏

time.start();

this.setVisible(true);

}

private void setIconImage(ImageIcon imageIcon) {

// TODO Auto-generated method stub

}

public  void paint(Graphics g)

{

BufferedImage bi=new

BufferedImage(F_Width,F_Height,BufferedImage.TYPE_INT_RGB);

Graphics g2=bi.createGraphics();

if(B_Game)

for(int i=0;i

{

int x=block[List_into[i]][0];

int y=block[List_into[i]][1];

for(int j=0;j<4;j++)

{

int color=block_color[List_into[i]][j];

if(color==2)

{

g2.drawImage(Black_Img,x*j, y,this);//黑块

}

else if(color!=0&&color!=-1)

{

g2.drawImage(White_Img,x*j, y,this);//白块

}

else if(color==0)

{

g2.drawImage(Gray_Img,x*j, y,this);//灰块

}

else

{

g2.setColor(Color.red);

g2.fillRect(x*j, y, 100, 100);

}

}

Show_string(g2);

}

if(B_Donghua)

{

Game_Main(g2);

}

if(B_Win)

Game_Over(g2);

if(B_Game&&!B_Main)

g2.drawImage(GameStop,(400-116)/2,

(600-116)/2,this);//暂停

g.drawImage(bi, 0, 0,this);

}

public void inis_game()//游戏初始化

{

for(int i=0;i<30;i++)

{

List_into[i]=i;

B_block[i]=true;

}

for(int i=0;i<8;i++)

{

block[i][0]=block_r;

block[i][1]=-block_r;

for(int j=0;j<4;j++)

{

int temp=Rd.nextInt(4)+1;

block_color[i][j]=temp;

}

}

}

private void inis_block(int num)//初始化方块

{

for(int i=0;i<4;i++)

{

int temp=Rd.nextInt(8)+1;

if(temp==1||temp==2||temp==3)

block_color[num][i]=2;

else

block_color[num][i]=1;

}

block[num][1]=-block_r;

}

private void list_into()//加入方块

{

block_num++;

if(block_num>7)

block_num=7;

}

private void block_run()//方块运动

{

for(int i=0;i

{

block[List_into[i]][1]+=1;

if(block[List_into[i]][1]>=600)

{

for(int j=0;j<4;j++)

if(block_color[List_into[i]][j]==2)

{

Main_i=0;

B_Win=true;

}

inis_block(List_into[i]);

}

}

}

private void Game_Main(Graphics g)//游戏开始动画

{

int r=98,b_Lift=100,b_Height=242;

g.drawImage(Backgroud,0, Main_i,this);//背景

g.drawImage(Right_up,b_Lift+r+Main_i,b_Height-Main_i,this);

g.drawImage(Right_down,b_Lift+r+Main_i,

b_Height+r+Main_i,this);

g.drawImage(Lift_up,b_Lift-Main_i,

b_Height-Main_i,this);

g.drawImage(Lift_down,b_Lift-Main_i,b_Height+r+Main_i,this);

g.drawImage(GameMain,140, 282+Main_i,this);

if(Main_i>550)

{

B_Game=true;

Game_v=10;

}

if(Main_i>600)

{

B_Donghua=false;

}

if(B_Main)

Main_i++;

}

private void Show_string(Graphics g2)//显示分数和速度

{

g2.drawImage(result_ground, 8, 25, this);

g2.setColor(Color.white);

g2.setFont(new Font("微软雅黑",0,17));

g2.drawString(Game_result+"", 53, 41);

g2.drawString((10-Game_v)+"",53, 57);

if(Game_result<1000)

Game_v=10;

else if(Game_result<2000)

Game_v=9;

else if(Game_result<3500)

Game_v=8;

else if(Game_result<5500)

Game_v=7;

else if(Game_result<8000)

Game_v=6;

else if(Game_result<10500)

Game_v=5;

else if(Game_result<13000)

Game_v=4;

else if(Game_result<18000)

Game_v=3;

else if(Game_result<25000)

Game_v=2;

else if(Game_result<35000)

Game_v=2;

}

private void Game_Over(Graphics g)//游戏结束

{

Main_i+=4;

String trophy_URL="";

if(500-Main_i<=(600-250)/2)

{

g.drawImage(GameOver,(400-318)/2, (600-250)/2,this);

Main_i=500;

g.drawString(Game_result+"", 258, 292);

g.drawString(BestResult+"", 258, 362);

if(Game_result<10000&&Game_result>1000)

trophy_URL="铜杯.png";

else if(Game_result<20000)

trophy_URL="银杯.png";

else

trophy_URL="金杯.png";

if(Game_result>1000)

{

try {

trophy=ImageIO.read(new File("Image/"+trophy_URL));

} catch (IOException e) {

e.printStackTrace();

}

g.drawImage(trophy,75, 269,this);

}

}

else

{

g.drawImage(GameOver,380-Main_i , 500-Main_i,this);

}

}

private void Game_Again()//继续游戏

{

block_num=0;

for(int i=0;i<8;i++)

{

block[i][0]=block_r;

block[i][1]=-block_r;

for(int j=0;j<4;j++)

{

int temp=Rd.nextInt(4)+1;

block_color[i][j]=temp;

}

}

Game_result=0;

B_Win=false;

B_Main=true;

}

@Override

public void mouseClicked(MouseEvent e) {//单击事件

int M_x=e.getX(),M_y=e.getY();

if(!B_Main&&!B_Game)

{

if(M_x>140&&M_x<256&&M_y>282&&M_y<398)

B_Main=true;

}

if(B_Win)

{

if(M_x>82&&M_x<160&&M_y<417&&M_y>394)

System.exit(0);

else

if(M_x>238&&M_x<318&&M_y<417&&M_y>394)

Game_Again();

}

}

@Override

public void mousePressed(MouseEvent e) {//按下事件

int M_x=e.getX(),M_y=e.getY();

for(int i=0;i

{

int y=block[List_into[i]][1];

if(M_y>y&&M_y

{

int b_i=M_x/block_r;

if(block_color[List_into[i]][b_i]==2&&!B_Win)

{

block_color[List_into[i]][b_i]=0;

Game_result+=100;

if(Game_result>BestResult)

BestResult=Game_result;

}

if(block_color[List_into[i]][b_i]!=2&&block_color[List_into[i]][b_i]!=0&&!B_Win)

{

block_color[List_into[i]][b_i]=-1;

B_Win=true;

Main_i=0;

}

}

}

}

@Override

public void mouseReleased(MouseEvent e) {//弹起事件

// TODO Auto-generated method stub

}

@Override

public void mouseEntered(MouseEvent e) {//进入事件

if(B_Game)

B_Main=true;

}

@Override

public void mouseExited(MouseEvent e) {//离开事件

if(B_Game&&!B_Win)

B_Main=false;

}

@Override

public void run() {

while(true)

{

try {

time.sleep(Game_v);

} catch (InterruptedException e) {

e.printStackTrace();

}

if(B_Main)

{

if(!B_Win&&B_Game)

{

block_run();

if(time_i==0)

list_into();

time_i=(++time_i)%(block_r);

}

}

this.repaint();

}

}

}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值