java别踩白块_java别踩白块(基础功能)

首先理一下思路:

1.创建界面                      创建一个背景类,主函数入口。

2.创建方块,从顶端出现。               创建一个节点类。

3.使方块下降,每秒一次。                 改变坐标位置。每秒重画一次。

4.在下降的同时,出现新的方块。                  将节点相连,并每次重画的时候创建一个新的节点。

5.实现触碰效果,碰到最下面的方块以后,消去。      设置鼠标监听事件和鼠标点击点的坐标,当点击在范围内将消去最下面的方块。

6.漏点或者有方块降下去,游戏结束。                      设置边界,头部超出,或点击的不是头部区域则失败。

写的有点烂,大神勿喷,只实现了基本功能。

再运行时,反应有点慢,只能等线程阻塞完成后他才会消去方块,线程问题没有解决,到最后只会越来越多

有什么好的见解可以告诉我一声。

界面背景类:

public class frame extends Frame{

private int vk=100;

private int x=-200,y=-200;

private boolean flag=true;

queue qq=new queue();

public frame(){  //基本界面

super("别踩白块");

this.setBounds(600, 300, 400, 700);

this.setVisible(true);

this.addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

System.exit(-1);

}

});

new MyThread().start();   //创建一个线程,用于更新画布

addMouseListener(new Mouse());  //设立鼠标监听事件

}

public void paint(Graphics g){

g.setColor(Color.BLACK);

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

g.drawLine(0+i*vk, 0, 0+i*vk, 700);

}

for(int i=0;i<=7;i++){

g.drawLine(0, 100+i*vk, 400, 100+i*vk);

}

qq.draw(g);

qq.add();

}

public void carsh(){

int cx=x-x%100;          //取整百数操作

int cy=y-y%100;

if ((cx==qq.getX())&&(cy==qq.getY())) {   //判断点击是否在方块内,如果在则消去方块

qq.delete();

}

else {

flag=false;

}

}

public void gameover(){  //游戏结束函数

Label label=new Label("游戏结束");

label.setBounds(100, 200, 300, 100);

label.setFont(new Font("宋体", 1, 50));

this.add(label);

this.setLayout(null);

this.setVisible(true);

}

class Mouse extends MouseAdapter{

@Override

public void mouseClicked(MouseEvent e) {

if (e.getButton()==MouseEvent.BUTTON1) {  //获得鼠标点击点的坐标

x=e.getX();

y=e.getY();

carsh();

}

}

}

public static void main(String[] args) {

frame bg=new frame();

}

class MyThread extends Thread{

@Override

public void run() {

while(true){

try {

if(flag==true){

repaint();  //重画

qq.headxy();  //获得最下面的方块坐标

}

else {

break;

}

Thread.sleep(1000);

} catch (Exception e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

gameover();

}

}

}

队列类:用于将每个方块连起来,用于判断哪个是头,哪个是尾。

public class queue {

private node tail;

private node head;

private node block;

private int x,y;

public queue(){

block=new node();

head=block;

tail=block;

}

//将每个方块都画出来

public void draw(Graphics g){

for(node n=head;n!=null;n=n.next){

n.draw(g);

}

}

// 新增node节点,新增的node的后驱节点为原来的头部,令原来的头部的前驱节点等于新节点,使方块连起来,最后令新节点变为头部。

public void add(){

node block=new node();

block.next=head;

head.prex=block;

head=block;

}

//删除尾部方块,尾部删除则是另尾部变为尾部的前驱节点,接着使新的尾部的后驱节点置空。

public void delete(){

tail=tail.prex;

tail.next=null;

}

//这个是尾部删除,之前写错了,方法名没有更改

public void headxy(){

x=tail.getX();

y=tail.getY();

}

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

}

方块类:

public class node {

node prex;

node next;

private int vk=100;

private int x,y;

public node(){

x=(new Random().nextInt(4))*vk;

y=-100;

}

public void draw(Graphics g){  //画方块,每次向下移一格

g.setColor(Color.blue);

g.fillRect(x, y, 100, 100);

y=y+vk;

}

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值