版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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);/ 鍵盤監(jiān)聽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 / 縱向區(qū)間&& (ball.x + ball.d = this.x | ball.x = this.x + this.w ) / 橫向區(qū)間 )ball.dx *= -1;this.visible = false;if(ball.x +
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 項目管理與團隊合作能力培養(yǎng)實踐課程
- 教育心理學在提升學習興趣中的應用
- 線上展覽活動的營銷新模式
- 智能家居安防系統(tǒng)設備的普及與應用
- 2025年紫外玻璃濾光片項目可行性研究報告
- 2025年洗衣機齒輪軸項目可行性研究報告
- 2025年大腳盤項目可行性研究報告
- 微服務架構研究-第1篇-深度研究
- 2025年不含氣液體瓶裝生產線項目可行性研究報告
- 2025至2031年中國固定滅火系統(tǒng)行業(yè)投資前景及策略咨詢研究報告
- 尿道狹窄護理PPT課2
- 教育家精神六個方面專題PPT
- 2023年河北廊坊市三河市金創(chuàng)產業(yè)投資有限公司招聘筆試題庫含答案解析
- 醫(yī)學專題-脛骨高位截骨術
- 中國減肥行業(yè)市場分析與發(fā)展趨勢講義
- 海通食品集團楊梅汁產品市場營銷
- 圍術期下肢深靜脈血栓預防的術中護理
- DBJ51-T 151-2020 四川省海綿城市建設工程評價標準
- GB/T 12996-2012電動輪椅車
- 小象學院深度學習-第7講遞歸神經網絡
- 三方采購協(xié)議范本
評論
0/150
提交評論