用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

桌面冰球

2017-08-22 作者: tockchen举报

[java]代码库

package cn.edu.hnnu;

/**
 * 1-->矩形框   挡板
 * 2-->要求我们鼠标能够控制挡板
 * 3-->小球撞上挡板能够反弹
 * 
 */
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 * 1-->小球与挡板碰撞--banW-5 ---->50 2-->小球与挡板碰撞--speed-1 ----1
 * 3-->小球与挡板碰撞-->修改颜色----随机色
 * 4-->小球与挡板碰撞-->score+1---画出来--->g.drawString("分数"+score)
 * 5-->小球没接住-->gameover-->画出来-->线程停止运行()
 * 
 * @author
 *
 */
public class MyBallPanel extends JPanel implements Runnable, MouseListener, KeyListener {
	static BufferedImage background;
	static BufferedImage qiu;
	static BufferedImage ZUOBAN;
	static BufferedImage YOUBAN;
	static BufferedImage kaishi;
	static BufferedImage jieshu1;
	static BufferedImage jieshu2;
	boolean startGame = false;
	
	ArrayList<Rect>rectList=new ArrayList<Rect>();
	
	private void InitRectList()
	{
		//初始化	矩形rectList 
		rectList.add( zuoban);
		rectList.add(youban);
		rectList.add(new Rect(0,0,1200,40));
		rectList.add(new Rect(0,0,75,250));
		rectList.add(new Rect(0,550,75,250));
		rectList.add(new Rect(1120, 0, 75, 250));
		rectList.add(new Rect(1120, 550, 75, 250));
		rectList.add(new Rect(0, 760, 1200, 40));
		
	}
	private void hitBall()
	{
		rectList.remove(zuoban);
		rectList.remove(youban);
		zuoban= new Rect(zuobanX,zuobanY,20,200);
		youban=new Rect(youbanX,youbanY,20,200);
		rectList.add(zuoban);
		rectList.add(youban);
	
		Rect ball = new Rect(ballx,bally,60,60);
		Rect temp;
		for(int i = 0;i<rectList.size();i++)
		{
			temp =rectList.get(i);
			if(hit(ball.x,ball.y,ball.width,ball.height,temp.x,temp.y,temp.width,temp.height))
			{
		
				switch(f)
				{
				case 1:
					if(ball.x-3<temp.x+temp.width&&ball.x+3>temp.x+temp.width)
						f = 2;
					else 
						f = 4;
					break;
				case 2:
					if(ball.x-3<temp.x+temp.width&&ball.x+3>temp.x+temp.width)
						f = 3;
					else 
						f = 1;
					break;
				case 3:
					if(ball.x-3<temp.x+temp.width&&ball.x+3>temp.x+temp.width)
						f = 4;
					else 
						f = 2;
					break;
				case 4:
					if(ball.x-3<temp.x+temp.width&&ball.x+3>temp.x+temp.width)
						f = 1;
					else 
						f = 3;
					break;
				
				}
			}
		}
	}
	int ballx = 600;
	int bally = 400;
	int f = 1;
	int speed = 6;
	// 声明挡板的变量
	
	int zuobanX = 100;
	int zuobanY = 330;
	int zuobanW = 15;
	int zuobanH = 150;
	int youbanX = 1100;
	int youbanY = 330;
	int youbanW = 15;
	int youbanH = 150;
	int kaishiY = 0;
	int jj1 = 0;
	int jj2 = 0;
     int jjY = -100;

	boolean flag = true;
	Color color = null;
	int backY;
	
	Rect zuoban=new Rect(zuobanX,zuobanY,20,200);
	Rect youban=new Rect(youbanX,youbanY,20,200);
	
	private MyBallPanel Unit;
	static {
		try {
			background = ImageIO.read(MyBallPanel.class.getResource("BG_1.jpg"));
			qiu = ImageIO.read(MyBallPanel.class.getResource("qiu.png"));
			YOUBAN = ImageIO.read(MyBallPanel.class.getResource("11.jpg"));
			ZUOBAN = ImageIO.read(MyBallPanel.class.getResource("222.jpg"));
			kaishi = ImageIO.read(MyBallPanel.class.getResource("kaishi.jpg"));
			jieshu1 = ImageIO.read(MyBallPanel.class.getResource("1p win.png"));
			jieshu2 = ImageIO.read(MyBallPanel.class.getResource("2p win.png"));
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

	}

	public MyBallPanel(){
		this.InitRectList();
		this.addMouseListener(this);
	 }
	public void paint(Graphics g) {
		super.paint(g);
		paintBackground(g);
		paintQiu(g);
		paintBan(g);
		paintKaiShi(g);
		paintJieShu(g);
		g.setColor(Color.RED);
        paintWeiZhi(g);
		// 画
		// 是用数组方式存储,以及判断
		// 每次运动都需要和每一个砖块进行对比 ,以及砖块四边进行对比
	}
	private void paintWeiZhi(Graphics g) {
		// TODO 自动生成的方法存根
		if(flag==true){
		Font f1 = new Font(Font.SANS_SERIF,Font.BOLD,40);
		g.setFont(f1);
		g.setColor(Color.WHITE);
		g.drawString(" 1P", 80, 80);
		g.drawString(" 2P",1060, 80);
		}
	}
	private void paintJieShu(Graphics g) {
		// TODO 自动生成的方法存根
		if(flag==false){
			if(jj1==1)
			g.drawImage(jieshu1, 0,jjY, 1200, 1000, null);
			else 
			g.drawImage(jieshu2, 0,jjY, 1200, 1000, null);
	}

	}
	private void paintKaiShi(Graphics g) {
		// TODO 自动生成的方法存根
		g.drawImage(kaishi, 0,kaishiY, 1200, 800, null);
	}
	public boolean hit(int x1,int y1, int x2, int y2, int w2, int h2)
	{
		if(x1>=x2&&x1<=x2+w2&&y1>=y2&&y1<=y2+h2)
		return true;
		return false;
	}
	public boolean hit(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2 )
	{
		if(x1>=x2&&x1>=x2+w2)
		{
			return false;
		}else if(x1<=x2&&x1+w1<=x2)
		{
			return false;
		}else if(y1>=y2&&y1>=y2+h2)
		{
			return false;
		}else if(y1<=y2&&y1+h1<=y2)
		{
			return false;
		}
		return true;
	}
	private void paintBan(Graphics g) {
		// TODO 自动生成的方法存根 
    g.drawImage(ZUOBAN, zuobanX, zuobanY,zuobanW,zuobanH,null);
     g.drawImage(YOUBAN, youbanX, youbanY,youbanW,zuobanH,null);	 
	}
	private void paintQiu(Graphics g) {
		// TODO 自动生成的方法存根
		g.drawImage(qiu, ballx, bally, 60, 60, null);
	}

	private void paintBackground(Graphics g) {
		// TODO 自动生成的方法存根
		g.drawImage(background, 0, 0, 1200, 800, null);
	}


[代码运行效果截图]


桌面冰球

[源代码打包下载]




网友评论    (发表评论)

共5 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...