Java打砖块游戏源码.doc

上传人:doc321 文档编号:14927064 上传时间:2022-02-24 格式:DOC 页数:8 大小:72KB
返回 下载 相关 举报
Java打砖块游戏源码.doc_第1页
第1页 / 共8页
Java打砖块游戏源码.doc_第2页
第2页 / 共8页
Java打砖块游戏源码.doc_第3页
第3页 / 共8页
Java打砖块游戏源码.doc_第4页
第4页 / 共8页
Java打砖块游戏源码.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《Java打砖块游戏源码.doc》由会员分享,可在线阅读,更多相关《Java打砖块游戏源码.doc(8页珍藏版)》请在三一文库上搜索。

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 MyPanel();f.ad

2、d(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; / 暂停的毫秒数Bal

3、l 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; im; i+ )for (int j = 0; j n; j+) blocksij = new Block(i,j);t = new Thread(this); / 产生线程t.start(); /

4、 启动线程/* * 绘图 */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; im; i+ )for (int j = 0; j n; j+) if(blocksij.visible)g.fillRoundRect(blocksij.x, blocksij.y, blocksij.w, blocksij.h, blocksij.r, b

5、locksij.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; im; i+ )for (int j = 0; j n; j+) if(blocksij.visibl

6、e)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() = KeyEvent.VK_RIGHT)pane.right();public

7、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 = 10; / 直径/* 容器的参数 */int xContainer = 40;

8、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+this.width - d )dx *= -1;if(y this.yContainer+this.height - d )dy *= -1;4 挡板package com.game;/* * 挡板类 - 圆角矩形 *

9、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 Pane()/* * 移动 */public void left()if(xthis.xContainer)x -= dx;/* * 移动 */public void right()if(x this.y & ball.y this.x & ball.x this.x + this.w / 纵向区间& (ball.y + ball.d = this.y | ball.y = this.y + this.h ) / 横向区间 )ball.dy *= -1;this.visible = false;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1