




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、打砖块1 窗体package com.game;import java.awt.*;import java.awt.event.*;import javax.swing.*;/* * 打砖块游戏 * author 孙沛林 * */public class MyWindow public static void main(String args) JFrame f = new JFrame("打砖块");f.setSize(500,500);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);MyPanel mp = new My
2、Panel();f.add(mp);/ 键盘监听mp.addKeyListener(mp);f.addKeyListener(mp);f.setVisible(true);2 面板package com.game;import java.awt.*;import java.awt.event.*;import javax.swing.*;/* * 游戏界面 * author 孙沛林 * */public class MyPanel extends JPanel implements Runnable, KeyListenerThread t = null; / 线程int speed = 8;
3、 / 暂停的毫秒数Ball ball; / 小球Pane pane; / 挡板int m = 8; / 横向砖块int n = 5; / 纵向砖块Block blocks; / 砖块数组/* * 构造器 */public MyPanel()ball = new Ball();/ 生成小球pane = new Pane(); / 生成挡板blocks = new Blockmn;/ 生成砖块数组for(int i = 0; i<m; i+ )for (int j = 0; j < n; j+) blocksij = new Block(i,j);t = new Thread(this
4、); / 产生线程t.start(); / 启动线程/* * 绘图 */public void paint(Graphics g)g.clearRect(0, 0, 500, 500);/ 边界g.setColor(Color.blue);g.drawRect(40, 40, 400, 400);/ 挡板g.setColor(Color.gray);for(int i = 0; i<m; i+ )for (int j = 0; j < n; j+) if(blocksij.visible)g.fillRoundRect(blocksij.x, blocksij.y, blocksi
5、j.w, blocksij.h, blocksij.r, blocksij.r);/ 小球g.setColor(Color.black);g.fillOval(ball.x, ball.y, ball.d, ball.d);/ 挡板g.setColor(Color.orange);g.fillRoundRect(pane.x, pane.y, pane.w, pane.h, pane.r, pane.r);/* * 运行 */public void run() while(true)ball.move();/ 小球移动一步for(int i = 0; i<m; i+ )for (int
6、j = 0; j < n; j+) if(blocksij.visible)blocksij.crash(ball);/ 碰撞判断+处理repaint();try t.sleep(speed); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();/* * 按下键盘 */public void keyPressed(KeyEvent e) if(e.getKeyCode() = KeyEvent.VK_LEFT)pane.left();if(e.getKeyCode() =
7、 KeyEvent.VK_RIGHT)pane.right();public void keyReleased(KeyEvent arg0) / TODO Auto-generated method stubpublic void keyTyped(KeyEvent arg0) / TODO Auto-generated method stub3 小球package com.game;/* * 小球类 * author 孙沛林 * */public class Ball /* * 属性 */int x =100;int y =200;int dx = 1;int dy = 1;int d =
8、10; / 直径/* 容器的参数 */int xContainer = 40;int yContainer = 40;int width = 400;int height = 400;/* * 构造器 */public Ball()/* * 移动一步 */public void move()x += dx;y += dy;this.wall();/* * 碰四周边界反弹 */public void wall()if(x < this.xContainer | x > this.xContainer+this.width - d )dx *= -1;if(y < this.yC
9、ontainer | y > this.yContainer+this.height - d )dy *= -1;4 挡板package com.game;/* * 挡板类 - 圆角矩形 * author 孙沛林 * */public class Pane int x = 200;int y = 440;int dx = 4;int w = 80; / 自宽int h = 10; / 自高int r = 5; / 圆角弧度/* 容器的参数 */int xContainer = 40; / 容器起点横坐标int width = 400; / 容器宽度/* * 构造器 */public Pa
10、ne()/* * 移动 */public void left()if(x>this.xContainer)x -= dx;/* * 移动 */public void right()if(x<this.xContainer + this.width - this.w)x += dx;5 砖块package com.game;/* * 砖块类 - 圆角矩形 * author 孙沛林 * */public class Block int x;int y;int w = 49;int h = 20;int r = 5;int xContainer = 40; / 容器起点横坐标boolea
11、n visible = true;/* * 构造器 */public Block()/* * 构造器 * param x * param y */public Block(int x, int y)this.x = x*(w+1) + xContainer + 1;this.y = y*(h+1) + xContainer + 1;/* * 计算小球是否接触的砖块 * 如果接触了左右两边,则改变小球的dx方向 * 如果接触了上下两边,则改变小球的dy方向 * 同时,本砖块消失 * param ball 小球 */public void crash(Ball ball)if(ball.y + ball.d > this.y && ball.y < this.y + this.h / 纵向区间&& (ball.x + ball.d = this.x | ball.x = this.x + this.w ) / 横向区间 )ball.dx *= -1;this.visible = false;if(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 智能门锁与锁具安装考核试卷
- 体育设备租赁业务的市场竞争策略分析考核试卷
- 八一勋章面试题及答案
- 行政许可考试试题及答案
- 汗巾与古代宫廷服饰的互动关系考核试卷
- 数据安全策略考核试卷
- 餐饮劳工面试题及答案
- java分表分库面试题及答案
- 湖南省长沙市岳麓实验中学2024-2025学年高二下学期6月月考数学试卷
- 《数据流通区块链存证技术规范》征求意见稿
- 山东济南天桥区2024-2025 学年第二学期七年级地理期末考试试题含答案
- 山西省2025年中考第三次模拟考试语文试卷(含答案)
- 肾上腺疾病讲课件
- 2025年甘肃省中考道德与法治试卷(含标准答案)
- 毕业设计(论文)-8m3连续搅拌釜式反应器设计
- (高清版)DB13∕T 2937-2019 非煤矿山双重预防机制建设规范
- 2025年餐饮管理与服务技能考试卷及答案
- 2025吉林长春市轨道交通集团有限公司校园招聘693人笔试参考题库附带答案详解析
- 合同账户变更补充协议书
- 2025国开电大《管理英语1》综合测试形考任务答案
- 广东省深圳市2021-2022学年高一下学期英语期末调研考试(含答案)
评论
0/150
提交评论