• 大小: 12KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-12
  • 语言: Java
  • 标签: ballgame  

资源简介

用户打开此游戏后,窗口会有一个球移动,每次落到下面的时候,下面的板子必须接住。当接住一次后,球的速度越来越快。难度越来越大。如果接不住球,则游戏结束。

资源截图

代码片段和文件信息

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Timer;
import java.util.TimerTask;
import java.util.zip.Inflater;

import javax.swing.Jframe;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * 
 * @author xieyuan
 * 1.窗口  面板  窗口位置固定
 * 2.在窗口上画一个球
 * 3.让小球自己动
 * 4.对小球的运动方向进行判断,并进行方向的处理,实现小球的反弹效果
 * 5.添加挡板  实现跟随鼠标来控制挡板移动
 * 6.判断小球接触到挡板,或者接触到边缘的时候的处理
 */

public class Game extends Jframe implements MouseMotionListener{
//设置窗口的宽、高
private int fw=800;
private int fh=600;

//设置球的位置
private int bx=0;
private int by=0;
private int b2r=100;
private String direction=“right_down“;//初始方向是右下

//定时器:在指定时间间隔内,反复触发指定窗口的事件
private Timer timer=new Timer();

//设置挡板  back plate
private int block_x=200;
private int block_y=580;
private int block_w=150;
private int block_h=20;

//设置球的速度、分数
private int speed=1;
private int score=0;
private Inner inner=null;

public Game() {
//窗口置顶,永远在前
this.setAlwaysOnTop(true);
//禁用此frame边框修饰
this.setUndecorated(true);
//初始化容器
this.getContentPane().setBackground(Color.BLACK);
this.setSize(fw fh);
//添加面板
inner=new Inner();
this.add(inner);
//窗口位置:居中
this.setLocationRelativeTo(null);
//设置窗口能否改变大小
this.setResizable(false);
this.addMouseMotionListener(this);
this.setVisible(true);
}

//面板容器类
class Inner extends JPanel {
public Inner() {
//控件:设置不透明
this.setOpaque(false);
//定时器的方法
timer.schedule(new TimerTask(){

@Override
public void run() {
/*
 * 通过坐标定时改变实现小球的移动
 */
if(“right_down“.equalsIgnoreCase(direction)) {//右下
bx+=speed;
by+=speed;
}
if(“right_up“.equalsIgnoreCase(direction)) {//右上
bx+=speed;
by-=speed;
}
if(“left_up“.equalsIgnoreCase(direction)) {//左上
bx-=speed;
by-=speed;
}
if(“left_down“.equalsIgnoreCase(direction)) {//左下
bx-=speed;
by+=speed;
}

/*
 * 判断小球什么时候进行方向的改变
 */
//游戏结束
if(by+b2r>=fh) {
JOptionPane.showMessageDialog(inner “GAME OVER!!!“ “提示信息“ JOptionPane.DEFAULT_OPTION);
Runtime.getRuntime().exit(0);//运行结束
}

//接触到顶部
if(by<=0) {
if(“left_up“.equalsIgnoreCase(direction)) {//左上
direction=“left_down“;
}else{//右上
direction=“right_down“;
}
}

//接触到左边
if(bx<=0) {
if(“left_down“.equalsIgnoreCase(direction)) {//左下
direction=“right_down“;
}else{//左上
direction=“right_up“;
}
}

//接触到右边
if(bx+b2r>=fw) {
if(“right_up“.equalsIgnoreCase(direction)) {//右上
direction=“left_up“;
}else{//右下
direction=“left_down“;
}
}

//接触到底部、挡板
if(bx+b2r/2>=block_x&&bx+b2r/2<=block_x+block_w&&by+b2r>=block_y) {

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-07-31 16:29  弹球游戏\
     文件         301  2018-07-31 15:39  弹球游戏\.classpath
     文件         388  2018-07-31 15:39  弹球游戏\.project
     目录           0  2018-07-31 19:10  弹球游戏\.settings\
     文件         598  2018-07-31 15:39  弹球游戏\.settings\org.eclipse.jdt.core.prefs
     目录           0  2018-11-18 07:04  弹球游戏\bin\
     文件        2379  2018-11-18 07:04  弹球游戏\bin\Game$Inner$1.class
     文件        1563  2018-11-18 07:04  弹球游戏\bin\Game$Inner.class
     文件        3469  2018-11-18 07:04  弹球游戏\bin\Game.class
     文件        4137  2018-11-18 07:04  弹球游戏\bin\JFraStart.class
     文件         406  2018-11-18 07:04  弹球游戏\bin\StartAction.class
     目录           0  2018-07-31 19:12  弹球游戏\src\
     文件        4211  2018-07-31 16:26  弹球游戏\src\Game.java
     文件        3465  2018-07-31 19:55  弹球游戏\src\StartAction.java

评论

共有 条评论

相关资源