您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“怎么用java實(shí)現(xiàn)彈球小游戲”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么用java實(shí)現(xiàn)彈球小游戲”吧!
GUI實(shí)現(xiàn)彈球小游戲,供大家參考,具體內(nèi)容如下
先看一下游戲效果圖。
一個(gè)簡單的Demo。也比較簡單,新手試著做一做完善改進(jìn)。
源代碼
import Com.Style.FontStyle; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * @Author: 冀十三 * @DescIption: 彈球小游戲 * @Date:2021--06--10--17:08 */ public class Demo extends JFrame { //創(chuàng)建游戲窗口 public Demo() { init(); } //桌面大小 private final int TABLE_WIDTH = 450; private final int TABLE_HEIGHT = 600; //球大小 private final int BALL_SIZE = 15; //球拍大小 private final int RACKET_WIDTH = 120; private final int RACKET_HEIGHT = 20; //記錄小球坐標(biāo) private double BallX = 100; private double BallY = 200; //記錄球拍坐標(biāo) private double RACKET_X = 150; private final double RACKET_Y = 380; //記錄小球速度 private double speedX = 10; private double speedY = 20; //記錄游戲是否結(jié)束 private boolean isOver = false; //聲明一個(gè)積分器 int score= 0; //聲明一個(gè)計(jì)時(shí)器。 ActionListener run = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //更新小球坐標(biāo)讓小球動起來 BallY+=speedY; BallX+=speedX; //重繪界面 mycanvas.repaint(); //碰到兩邊的時(shí)候 if (BallX<=0||BallX>=TABLE_WIDTH-BALL_SIZE){ speedX=-speedX; } //碰到上邊界的時(shí)候 if(BallY<=0){ speedY=-speedY; } //接住小球的時(shí)候 if ( BallY>=(RACKET_Y-BALL_SIZE)&&BallX>RACKET_X&&BallX<(RACKET_X+RACKET_WIDTH)){ //得10分 score+=10; speedY=-speedY; } if ((BallY>(RACKET_Y-BALL_SIZE)&&(BallX<RACKET_X||BallX>RACKET_X+RACKET_WIDTH))){ timer.stop(); isOver=true; mycanvas.repaint(); } } } ; private Timer timer = new Timer(60, run); //內(nèi)部類繼承Canvas充當(dāng)畫布 private class myCanvas extends Canvas { public void paint(Graphics g) { //繪制內(nèi)容 //如果游戲結(jié)束 if (isOver) { //給畫筆顏色 g.setColor(new Color(0xFF9A1B44, true)); //給畫筆字體 g.setFont(new Font("宋體", FontStyle.BOLD, 25)); g.drawString("Game Over!", 120, TABLE_HEIGHT / 4); g.drawString("成績"+score+"分", 120, TABLE_HEIGHT / 3); g.drawString("按空格再來一次", 120, TABLE_HEIGHT / 2); } else { //否則游戲中 //繪制小球 g.setColor(new Color(0xFF09FAA6, true)); g.fillOval((int) BallX, (int) BallY, BALL_SIZE, BALL_SIZE); //畫一個(gè)積分系統(tǒng) //上色 g.setColor(new Color(0xE8DC0C77,true)); //給畫筆字體 g.setFont(new Font("宋體", FontStyle.BOLD, 25)); g.drawString("得分:"+score, 25, 25); //繪制球拍 g.setColor(new Color(0xFF0E51D7, true)); g.fillRect((int) RACKET_X, (int) RACKET_Y, RACKET_WIDTH, RACKET_HEIGHT); timer.start(); } } } //創(chuàng)建繪畫區(qū) myCanvas mycanvas = new myCanvas(); //窗口初始化 public void init() { JFrame jFrame = new JFrame(); jFrame.setTitle("彈球小游戲");//窗口名稱 jFrame.pack();//自適應(yīng)布局 jFrame.setVisible(true);//可見 jFrame.add(mycanvas); jFrame.setBounds(500, 200, TABLE_WIDTH, TABLE_HEIGHT); jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//關(guān)閉 mycanvas.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT));//畫布大小 jFrame.setResizable(false);//大小不可變 KeyListener keyListener = new KeyAdapter() { //重寫鍵盤被按壓的方法 @Override public void keyPressed(KeyEvent e) { //接收按下的是哪個(gè)鍵 int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) { if (RACKET_X >0) { RACKET_X -= 30; } } if (key == KeyEvent.VK_RIGHT) { if (RACKET_X < (TABLE_WIDTH - RACKET_WIDTH )) { RACKET_X += 30; } } if (key == KeyEvent.VK_SPACE) { if (isOver=true) { Demo.main(null); } } } }; //窗口獲取鍵盤監(jiān)聽 jFrame.addKeyListener(keyListener); //鍵盤獲取鍵盤監(jiān)聽 mycanvas.addKeyListener(keyListener); } public static void main(String[] args) { new Demo(); } }
到此,相信大家對“怎么用java實(shí)現(xiàn)彈球小游戲”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。