多线程:打砖块的初步想法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/givemefive8086/article/details/84459099
   多线程:打砖块的初步实现
线程:
  每个Java程序都有一个主线程,当程序执行后,主线程会自动调用程序的main()方法;
  
单线程:
  单线程的特点是,程序一步一步执行,上一步没有执行完毕,下一步就不会执行;
  
多线程:
  而多线程模型是将代码的调用放到一个独立的运行单元:线程中多步并发执行,所以有省时,并发的优点;
  线程并不占用系统资源,它与进程共享资源;
  
多线程的实现:
    每个线程对象都是一个继承了java.util.Thread类的对象或implements了java.util.Runnable接口的类的对象,线程的启运是通过调用线程对象的start()方法启动,线程运行是从线程对象的run方法开始,当线程对象的run()方法结束后,线程即运行结束。线程的创建,一般通过如下两种方法:
   一个是继承Thread类对象,然后重写run()方法,在run()方法中实现需要进行的线程,然后调用start()方法来启动,
   另一个是继承Runnable接口,实现它的run()方法,然后将这个类的实例作为Thread的构造方法的参数,创建Thread类的实例,即可做为线程启动。

多线程实例:打砖块的实现

打砖块的实现首先要实现小球的移动,这就需要用到一个线程:首先创建一个线程,然后在run方法中画一个小球,然后用while(true)无限循环来不停的改变坐标实现小球的移动,当然这样的代价是CPU的占用率非常高,所以我们没循环一次就休息10mm,方法是线程的sleep()方法,这个方法可能产生异常,所以要用try、catch来实现。小球的移动当然还需要实现重绘,在面板中需要不停的更新。具体代码:
public class boll_change implements Runnable{
mainPanel mm;
int i=100;
int j=350;
int sign1=5; // 标志位i
int sign2=5; // 标志位j
Image boll = new ImageIcon("five_images/baiqi.png").getImage();
MyKeyListener key;

public boll_change(mainPanel mm,MyKeyListener key)
{
this.mm=mm;
this.key=key;
}

@Override
public void run()
{
//循环执行小球移动
while(true){
//如果触到右边界,则转向
if(i>=560){sign1=-5;}
//左边界,转向
else if(i<=10){sign1=5;}
//如果被底座接住,则转向
if(j>=530&&j<=540&&i-key.keyi<249&&i-key.keyi>0){sign2=-5;}
//到达上顶,转向
else if(j<=50){sign2=5;}


if(mm.ml_move.getLength()!=0)
mm.ml_move.delet(0);
i=i+sign1;
j=j+sign2;
Shapes shape = new reImage(i,j,30,30,Color.white, boll,mm);
mm.ml_move.add(shape);
mm.repaint();
//睡眠
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
小球的移动实现后,还要实现砖块的多线程,砖块的多线程体现在当小球碰到时要及时消失,这也要实现重绘,砖块事件和小球的移动是同时发生的,这也体现了多线程的并发执行,同样,底座的移动也要用多线程来实现,具体不写了,初步完成后的简陋画面如下:

  
展开阅读全文

小游戏-打砖块问题-砖块消失

06-15

在球的上方若干排砖块,下方有一挡板,当球碰挡板返回碰到砖块,砖块消失,咋样使砖块消失??rn这是我想让砖块消失的方法,但是调试没错误,运行却没有让砖块消失的效果!rnBOOL CMyDoc::OnNewDocument()rnrn if (!CDocument::OnNewDocument())rn return FALSE;rn for(int i=0;iSelectObject(&brushNewRed);//画刷rnrn CRect rect1;rn GetClientRect(&rect1); rn CRect block[C_XBLOCKS][C_YBLOCKS]; rn int xDim;rn int yDim;rn xDim = rect.Width();rn yDim = rect.Height();rn int iBlockWidth;rn int iBlockHeight;rn iBlockWidth = (int)xDim/(C_XBLOCKS );rn iBlockHeight = (int)((yDim/5)/C_YBLOCKS); rn for(int i=0;i<= C_XBLOCKS;i++)rn rn for (int j=0;j<=C_YBLOCKS;j++)rn rn block[i][j].left=(int)(i*iBlockWidth);rn block[i][j].right=block[i][j].left+iBlockWidth;rn block[i][j].top = (int)(j*iBlockHeight + 20);rn block[i][j].bottom = block[i][j].top + iBlockHeight;rn pDC->Rectangle(block[i][j]);rn if(j==0 || j==2 )rn pDC->SelectObject(&brushNewRed);rn elsern pDC->SelectObject(&brushNewBlue);rn rn if(pDoc->m_nCount[i][j]==0)rn pDC->Rectangle(pDoc->block[i][j]);rn else break;rn rn rn rn。。、、、、、、、、、、、、、、、、、、rnvoid CMyView::OnTimer(UINT nIDEvent) rnrn // TODO: Add your message handler code here and/or call defaultrn CMyDoc* pDoc = GetDocument();rn ASSERT_VALID(pDoc);rnrn InvalidateRect(pDoc->m_rectBall,TRUE);rn CRect rect1;rn GetClientRect(&rect1);rn if(pDoc->m_rectBall.right>=rect1.right || pDoc->m_rectBall.left<=rect1.left)rn pDoc->m_nx=-pDoc->m_nx;rn else ;rn if(pDoc->m_rectBall.top<=rect1.top || pDoc->m_rectBall.left>pDoc->m_rectBody.left && pDoc->m_rectBall.rightm_rectBody.right && pDoc->m_rectBall.bottom>pDoc->m_rectBody.top)rn pDoc->m_ny=-pDoc->m_ny; rn pDoc->m_rectBall.left-=pDoc->m_nx;rn pDoc->m_rectBall.right-=pDoc->m_nx;rn pDoc->m_rectBall.top-=pDoc->m_ny;rn pDoc->m_rectBall.bottom-=pDoc->m_ny;rn InvalidateRect(pDoc->m_rectBall,FALSE);rnrn int i,j;rn for(i=0;i m_nCount[i][j]==0) rn rn if(pDoc->m_rectBall.left>pDoc->block[i][j].left&& pDoc->m_rectBall.rightblock[i][j].right && pDoc->m_rectBall.topblock[i][j].bottom) rn rn pDoc->m_ny=-pDoc->m_ny;rn pDoc->m_rectBall.left-=pDoc->m_nx;rn pDoc->m_rectBall.right-=pDoc->m_nx;rn pDoc->m_rectBall.top-=pDoc->m_ny;rn pDoc->m_rectBall.bottom-=pDoc->m_ny;rn pDoc-> m_nCount[i][j]=1;rn InvalidateRect(pDoc->block[i][j],TRUE);rn rn rn rnrn rn CView::OnTimer(nIDEvent);rnrnrnrn 论坛

砖块的程序无法显示

03-30

程序无错误提示,就是不能在模拟器上显示,还请各位指点指点rnrnimport javax.microedition.midlet.MIDlet;rnimport javax.microedition.lcdui.*;rnrnpublic class BlockBall extends MIDlet rnrn private Display display;rn private GameCanvas canvas;rn rn public BlockBall() rn rn display=Display.getDisplay(this);rn canvas=new GameCanvas();rn rn rnrn rn protected void startApp()rn display.setCurrent(canvas);rn rn protected void pauseApp() rn rn protected void destroyApp(boolean arg0)rnrn rnrnrnrnimport java.io.*;rnimport javax.microedition.lcdui.*;rnrnpublic class GameCanvas extends Canvas implements Runnablernrn /**rn * rn */rn private int state;rn private final static int active=1;rn private final static int clear=2;rn private final static int over=3;rn rn private final int Block_v=10;rn private final int Block_h=2;rn private final int Block_width=this.getWidth()/Block_h;rn private final int Block_height=Block_width/2;rn private boolean block[][]=new boolean[Block_h][Block_v];rn private int blockCount=Block_h*Block_v;rn rn private final int Ball_width=5;rn private final int Ball_height=5;rn private int Ball_speed=6;rn private int Ballx=0;rn private int Bally=getHeight()-Ball_height;rn rn private final int Bar_width=12;rn private final int Bar_height=4;rn private int Bar_speed=0;rn private int Barx=0;rn private int Bary=getHeight()-Bar_height;rn rn private Image Block=null;rn private Image Bar=null;rn private Image Ball=null;rn rn public GameCanvas() rn new Thread(this).start();rn rn for(int i=0;igetWidth()-Ball_width)rn Ballx=getWidth()-Ball_width;rn Ball_speed*=-1;rn rn if(Bally<0)rn Bally=0;rn Ball_speed*=-1;rn rn if(Bally>getHeight()-Ball_height)rn state=over;rn rn if(Bally+Ball_height>Bary&&Ballx+Ball_width>Barx&&Ballx0)rn Ball_speed+=2;rn rn rn rn for(int i=0;ii*Block_width&&Ballx<(i+1)*Block_width)rn if(Bally+Ball_height>(i+1)*Block_height&&Bally<(i+2)*Block_height)rn block[i][j]=false;rn Bally*=-1;rn blockCount--;rn rn if(blockCount==0)rn state=clear;rn rn rn rn rn rn repaint();rn rn rn public void moveBar()rn Barx+=Bar_speed;rn if(Barx<0)rn Barx=0;rn else if(Barx>getWidth()-Bar_width)rn Barx=getWidth()-Bar_width;rn rn rn public void keyPressed(int key)rn if(state==active)rn if(getGameAction(key)==Graphics.LEFT)rn Bar_speed=-5;rn else if(getGameAction(key)==Graphics.RIGHT)rn Bar_speed=5;rn rn repaint();rn rn rn rn public void keyReleased(int key)rn Bar_speed=0;rn rnrnrn 论坛

没有更多推荐了,返回首页