会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 3646楼

一、代码

package com.bjsxt;


import java.awt.*;

import javax.swing.*;


public class BallGame extends JFrame {


    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");

    Image desk = Toolkit.getDefaultToolkit().getImage("imges/desk.png");


    double x =200;

    double y =200;


    double degree = 3.14/3;  //弧度。 3.14=180°。      60度。

    //绘制窗口

    public void paint(Graphics g) {

        System.out.println("窗口被画了一次!");

        g.drawImage(desk,0,0,null);

        g.drawImage(ball,(int) x,(int) y,null);



        x = x + 10*Math.cos(degree);

        y = y + 10*Math.sin(degree);


        //碰到上下边界

        if(y>501-40-30||y<40+40){

            degree = 3.14 - degree;

        }


        //碰到右边界

        if(x>856-40-30||x<40){

            degree = 3.14 - degree;

        }

    }


    //创建窗口

    void launchFrame() {

        setSize(856, 501);

        setLocation(100, 100);

        setVisible(true);



        //实现动画,每秒绘制窗口25次

        while(true){

            repaint();


            try {

                Thread.sleep(40);//1s =1000ms;大约1秒绘制1000/40=25次

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

    }






    public static void main(String[] args){

         System.out.println("我的小游戏开始了!");

         BallGame  game = new BallGame();

         game.launchFrame();

    }


}

二、用了懒加载还是不行


blob.png

JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 3647楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 3648楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 3649楼
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 3650楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 3652楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 3654楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 3656楼
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 3657楼

老师,getRect这个方法没有效果,导弹碰到飞机没有提示,我给飞机类设置的live布尔类型,也没有被触发

//GameObjec类
package com.bjsxt.plane08;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

/** 
 * <p>Title: 游戏根类</p>  
 * <p>Description: 尚学堂</p>  
 * @author xiaoding
 * @date Apr 17, 2020  
 * @version 1.0 
 */
public class GameObject {
    Image img;            //该物体对应的图片对象
    double x,y;            //该物体的坐标
    int speed;            //该物体的速度
    int width,height;    //该物体所在的矩形区域宽高
    
    public GameObject() {}
    
    public GameObject(Image img,double x,double y,int speed,int width,int height) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.width = width;
        this.height = height;
    }
    
    public GameObject(Image img, double x, double y, int speed) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = speed;
        
        if (img != null) {
             this.width = img.getWidth(null);
             this.height = img.getHeight(null);
        }
    }
    
    /*
     *     怎么样绘制文本对象
     *     @param g
     */
    public void drawMySelf(Graphics g) {
        g.drawImage(img,(int)x,(int)y,null);
    }
    
    /*
     *     返回物体对应矩形区域,便于后续在碰撞检测中使用
     *     @return
     */
    public Rectangle getRect() {
        return new Rectangle((int)x,(int)y,width,height);    
    }
}
//Plane飞机类
/**
 * <p>Title: Plane.java</p>  
 * <p>Description: </p>  
 * <p>Copyright: Copyright (c) 2018</p>  
 * <p>Company: www.baidudu.com</p>  
 * @author xiaoding
 * @date Apr 17, 2020  
 * @version 1.0 
 */ 
package com.bjsxt.plane08;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;

/** 
 * <p>Title: Plane</p>  
 * <p>Description: </p>  
 * @author xiaoding
 * @date Apr 17, 2020  
 * @version 1.0 
 */
public class Plane extends GameObject {
    //飞机方向
    boolean left,right,up,down;
    boolean live = true;    //或者
    
    //构造器
    public Plane() {}
    public Plane(Image img, double x, double y, int speed) {
        super(img,x,y,speed);
    }
    
    //绘画本类
    public void drawMyself(Graphics g) {
        if (live) {
             super.drawMySelf(g);
             //飞机上下左右飞行方向
             if(left) {
                 x -= speed;
             }
             if(right){
                 x += speed;
             }
             if(up){
                 y -= speed;
             }
             if(down) {
                 y += speed;
             }
        }
    }
    
    //飞机什么时候开始飞行
    public void addDirection(KeyEvent e) {
        switch(e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                left = true;
                break;
            case KeyEvent.VK_RIGHT:
                right = true;
                break;
            case KeyEvent.VK_UP:
                up = true;
                break;
            case KeyEvent.VK_DOWN:
                down = true;
                break;
        }
    }
    //什么时候停止飞机飞行
    public void minusDirection(KeyEvent e) {
        switch(e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                left = false;
                break;
            case KeyEvent.VK_RIGHT:
                right = false;
                break;
            case KeyEvent.VK_UP:
                up = false;
                break;
            case KeyEvent.VK_DOWN:
                down = false;
                break;
        }
    }
}
//MyGameFrame游戏主窗口类
package com.bjsxt.plane08;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/** 
 * <p>Title: 游戏主窗口(飞机大战1.0版)</p>  
 * <p>Description: 尚学堂</p>  
 * @author xiaoding
 * @date Apr 16, 2020  
 * @version 1.0 
 */

public class MyGameFrame extends Frame{
    //调用GameUtil类的静态方法,把图片的地址传过去
    Image feiJi = GameUtil.getImage("images/plane.png");
    Image bj = GameUtil.getImage("images/bg.jpg");
    
    //创建飞机类
    Plane plane = new Plane(feiJi,100,100,9);
    
    //创建炮弹数组
    Shell[] shell = new Shell[3];
    
    //画出窗口
    @Override
    public void paint(Graphics g) {    //g当做是一支画笔
        g.drawImage(bj,0,0,500,500,null);
        plane.drawMyself(g);
        for (int i = 0;i<shell.length;i++) {
            shell[i].drawMySelf(g);
            
            //炮弹碰撞飞机检测
            boolean peng = shell[i].getRect().intersects(plane.getRect());
            if (peng) {
                System.err.println("飞机挂了");
                plane.live = false;
            }
        }
    }
    
    //初始化窗口
        public void launchFrame() {
            //游戏名称
            this.setTitle("飞机大战-尚学堂");        
            setVisible(true);    //窗口是否可见
            
            setSize(Constant.GAME_WIHTH,Constant.GAME_HEIGHT);    //窗口大小
            setLocation(400,400);    //窗口打开位置
            
            //增加关闭窗口的动作
            this.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);    //正常退出程序
                }
            });
            //启动重画窗口内部类方法
            new PaintThread().start();
            //启动键盘监听
            this.addKeyListener(new KeyMonitor());
            
            //循环赋值炮弹数组
            for (int i = 0;i<shell.length;i++) {
                shell[i] = new Shell();
            }
        }
    
    /*
     *     定义一个重画窗口的线程类
     *     定义内部类是类为方便直接使用窗口类的相关方法
     */
    class PaintThread extends Thread {
        public void run() {
            while(true) {
                repaint();        //内部类可以直接使用外部类的成员!
                
                try {
                    Thread.sleep(50);        //1s=1000ms  (1000 / 50) = 20
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    /*
     *     创建一个键盘控制内部类
     *     实现键盘的监听处理
     */
    class KeyMonitor extends KeyAdapter {
        //当键盘按下时
        @Override
        public void keyPressed(KeyEvent e) {
            plane.addDirection(e);
        }
        //当键盘抬起时
        @Override
        public void keyReleased(KeyEvent e) {
            plane.minusDirection(e);
        }
    }
    
    
    //解决屏幕闪烁缓冲技术
    private Image offScreenImage = null; 
    public void update(Graphics g) { 
        if(offScreenImage == null) 
            //这是游戏窗口的宽度和高度
            offScreenImage = this.createImage(Constant.GAME_WIHTH,Constant.GAME_HEIGHT);
        
            Graphics gOff = offScreenImage.getGraphics(); 
            paint(gOff); 
            g.drawImage(offScreenImage, 0, 0, null); 
    }

    //main主方法,程序执行入口
    public static void main(String[] args) {
        MyGameFrame gameFrame = new MyGameFrame();
        gameFrame.launchFrame();
    }
}


JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 3658楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 3659楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 3660楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637