弹球游戏分步解析(三)——滑块动起来_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 弹球游戏分步解析(三)——滑块动起来

弹球游戏分步解析(三)——滑块动起来

 2013/7/22 18:41:45  香煎马鲛鱼  程序员俱乐部  我要评论(0)
  • 摘要:弹球游戏分步解析(三)——滑块动起来看过弹球游戏分步解析(一)、弹球游戏分步解析(二)的朋友应该知道,弹球游戏做到这一步可以实现小球在界面上“飞”了,不过既然是游戏,就要让玩家跟程序互动,没有互动就不能称之为游戏。接下来我们就给我们的一个游戏添加一个滑块,用鼠标控制滑块横向移动,让滑块碰撞小球使其弹回。做到控制滑块横向移动就需要使用鼠标监听器——MouseMotionListener来记录鼠标坐标,根据鼠标坐标位置来绘出滑块,移动时,部分朋友使用repant()的方法来删除原位置的滑块
  • 标签:游戏 解析

class="MsoNormal">弹球游戏分步解析(三)——滑块动起来

看过弹球游戏分步解析(一)、弹球游戏分步解析(二)的朋友应该知道,弹球游戏做到这一步可以实现小球在界面上“飞”了,不过既然是游戏,就要让玩家跟程序互动,没有互动就不能称之为游戏。

接下来我们就给我们的一个游戏添加一个滑块,用鼠标控制滑块横向移动,让滑块碰撞小球使其弹回。

做到控制滑块横向移动就需要使用鼠标监听器——MouseMotionListener来记录鼠标坐标,根据鼠标坐标位置来绘出滑块,移动时,部分朋友使用repant()的方法来删除原位置的滑块,可是这样做的结果是,滑块一直在闪,接下来我会介绍另一种方法来绘制滑块,这样滑块在移动的过程中就不会闪了;

和小球一样,先要建一个滑块类:

文件名BandThread

package jumpingBall;

?

import java.awt.Graphics;

?

import javax.swing.JPanel;

?

publicclass BandThread extends Thread {

??? privateint? length;

??? privateint? width;

??? privateintx;

??? privateinty;

??? privateintxlast;

??? privateintylast;

???

??? private JPanel jp;

??? private Graphics g;

??? public BandThread(JPanel jp,int x,int y){

??? ??? this.jp = jp;

??? ??? g =jp.getGraphics();

??? ??? this.x = x;

??? ??? this.y = y;

??? }

??? publicvoid run(){

??? ??? this.drawBand();

??? ??? this.clearBand();

?

??? }

??? publicvoid drawBand(){

??? ???

??? ??? g.fillRect(x, 500, 100, 10);

??? }

??? publicvoid clearBand(){

??? ??? if(xlast!= x){

??? ??? g.setColor(jp.getBackground());

??? ??? g.fillRect(0, 500, x, 10);

??? ??? g.fillRect(x+100, 500,jp.getWidth()-x-100,10 );

??? ??? }

??? }

?

}

加监听器:

文件名:bandListener

package jumpingBall;

?

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;

?

import javax.swing.JPanel;

?

public class banLlistener implements MouseMotionListener {

?

????????

???????? private? int x;

???????? private? int y;

???????? private JPanel jp;

???????? private Graphics g;

???????? public banLlistener(JPanel jp){

?????????????????? this.jp = jp;

?????????????????? this.g = jp.getGraphics();

???????? }

???????? @Override

???????? public void mouseDragged(MouseEvent e) {

?????????????????? // TODO Auto-generated method stub

??????????????????

???????? }

?

???????? @Override

???????? public void mouseMoved(MouseEvent e) {

?????????????????? // TODO Auto-generated method stub

?????????????????? x = e.getX();

?????????????????? y = e.getY();

?????????????????? Date.xband = x;

?????????????????? Date.yband = y;

?????????????????? BandThread band = new BandThread(jp,x,y);

??????? band.start();

?

??????????????????

??????????????????

???????? }

}

监听器所加位置:

文件名:BallThread

?

package jumpingBall;

?

import java.awt.Color;

import java.awt.Graphics;

import java.util.List;

import java.util.Random;

?

import javax.swing.JPanel;

?

public class BallThread extends Thread {

private static final int North = 1;//用四个整形的数据代表四个方向

private static final int West = 2;

private static final int South = 3;

private static final int East = 4;

private int Xdirection = West;//记录小球在X轴上的方向

private int Ydirection = North;//记录小球在y轴上的方向

private int radios;//半径

private Color color;//颜色

private int Xspeed;//x方向速度

private int Yspeed;//y方向速度

private int X;//当前位置x

private int Y;//当前位置y

private int LastX;//最后位置x

private int LastY;//最后位置y

private int xband;

private int yband;

private JPanel jp;//面板

private Graphics g;//画布

private boolean pauseFlag = false;//暂停标志(默认值否)

private boolean stopFlag = false;//停止标志(false——表示存在)

private boolean beread = false;//是否被储存在文件里过(默认值否)

?????? public BallThread(JPanel jp){

??? ??? ???this.jp = jp;

??? ??? ???this.g = jp.getGraphics();

?????? }

?

?????? public void run(){

??? ??? ???banLlistener band =new banLlistener(jp);//定义滑块

??? ??? ???jp.addMouseMotionListener(band);//给滑块加入鼠标监听器

??? ??? ???if(beread==false){//如果没有被储存在文件里过

??? ??? ???this.setprivate();//方法作用:随机生成它的部分属性

??? ??? ???}

??? ??? ???while(true){//永真表示球会一直运动

??????? ??? ???

??? ?????? ???try{

??? ?????????? ???sleep(30);

??? ?????? ???}catch(InterruptedException e){

??? ?????????? ???e.printStackTrace();

??? ?????? ???}

??? ?????? ???if(stopFlag==true){//如果停止标示为true,停止该线程

??? ?????????? ???clearBall();

??? ?????????? ???return;

??? ?????? ???}

??? ?????? ???if(pauseFlag==true){//如果暂停标示为真,跳过之后的步骤进入下次循环

??? ?????????? ???continue;

??? ?????? ???}

??? ?????? ???

??? ?????? ???this.clearBall();//清除原来位置的小球

??? ?????? ???this.move();//小球运动

??? ?????? ???this.drawBall();//在新的位置画出小球

?

??? ?????? ???

??? ??? ???}

??? ??? ?????? ???

}

?????? public void clearBall(){//清除小球

??? ??? ???Color color = jp.getBackground();//

??? ??? ???g.setColor(color);//

??? ??? ???g.fillOval(X, Y, radios*2, radios*2);//用背景色,填充之前小球的印记

?????? }

?????? public void drawBall(){

??? ??? ???g.setColor(color);//

??? ??? ???g.fillOval(X, Y, radios*2, radios*2);

?????? }

?????? public void setprivate(){//设置小球属性

??? ??? ???Random random = new Random();//随机

??? ??? ????radios = 10+random.nextInt(10);//半径

??? ??? ????color =new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));//颜色

??? ??? ????Xspeed = 2 + random.nextInt(10);//x方向速度

??? ??? ????Yspeed = 2+random.nextInt(10);//y方向速度

?????? }

?????? public void exit(boolean flag){//设置停止标示

??? ??? ???stopFlag = flag;

?????? }

?????? public void setpause(boolean flag){//设置暂停标示

??? ??? ???pauseFlag = flag;

?????? }

?????? public void move(){

?

??? ??? ???switch(Xdirection){//对于x方向

??? ??? ???case 2:X = X - Xspeed;break;//如果小球的运动方向向西,X值减小

??? ??? ???case 4:X = X + Xspeed;break;//如果小球的运动方向向东,X值增大

??? ??? ???}

??? ??? ???switch(Ydirection){//对于y方向

??? ??? ???case 1:Y = Y - Yspeed;break;//如果小球的运动方向向北,Y值减小

??? ??? ???case 3:Y = Y + Yspeed;break;//如果小球的运动方向向难,Y值增大

??? ??? ???}

??? ??? ???//如果碰到边界,方向改变

??? ??? ???if(X<0+radios*2){

??? ?????? ???Xdirection = 4;

??? ??? ???}

??? ??? ???if(X>jp.getWidth()-radios*2){

??? ?????? ???Xdirection = 2;

??? ??? ???}

??? ??? ???if(Y<0+radios*2){

??? ?????? ???Ydirection = 3;

??? ??? ???}

??? ??? ???//如果掉到下面,该小球线程结束

??? ??? ???if(Y>jp.getHeight()-radios*2){

??? ?????? ???//Ydirection = 1;

??? ?????? ???BallThread ball = new BallThread(jp);

??? ?????? ???this.exit(true);

??? ?????? ???for(int i = 0;i<Date.allBall.size();i++){

??? ?????????? ??ball=Date.allBall.get(i);

??? ?????????? ??if(X == ball.X&&Y == ball.Y){

??? ????????????? ??Date.allBall.remove(i);

??? ?????????? ??}

??? ?????? ???}

??? ??? ???}

?? ???? ???//如果碰到滑块,弹回

??? ??? ???if(Y<500+radios/2&&Y>490-radios*2&&X>=Date.xband&&X<=(Date.xband+100)){

??? ?????? ???Ydirection = 1;

??? ??? ???}

?????? }

……………………………………

??? (之后的代码见弹球游戏分步解析(二))

?

发表评论
用户名: 匿名