会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132709个问题
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 38161楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 38163楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 38164楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 38167楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 38168楼
JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/Maven 38169楼
Python 全系列/第一阶段:Python入门/编程基本概念 38170楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 38172楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 38173楼




我用了数组来画炮弹,炮弹发射的角度是任意的,但运行了好几次却发现炮弹就成一条直线了,是哪里出了问题呢

image.png


Shell.java


package zzu.baizhan.Feb.Day20th;

import java.awt.*;
import java.util.Random;

public class Shell extends GameObject {
   double degree;

   public Shell() {
       x = 200;
       y = 200;
       width = 10;
       height = 10;
       speed = 3;

       degree = Math.random()*Math.PI*2;
   }

   public void draw(Graphics g) {
       Color c = g.getColor();
       g.setColor(Color.YELLOW);
       g.fillOval((int) x, (int) y, width, height);
       //炮弹沿着任意角度去飞
       x += speed * Math.cos(degree);
       y += speed * Math.sin(degree);

       if (x < 0 || x > Constant.GAME_WIDTH - width) {
           degree = Math.PI - degree;
       }
       if (y < 30 || y < Constant.GAME_HEIGHT - height) {
           degree = -degree;
       }

       g.setColor(c);
   }

}


package zzu.baizhan.Feb.Day20th;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Feb20thPlane extends JFrame {

    Image ball = Feb20thGameUtil.getImage("Images/ball.png");
    Image bg = Feb20thGameUtil.getImage("Images/bg.jpg");
    Image plane = Feb20thGameUtil.getImage("Images/plane.png");
    Plane p=new Plane(plane,250,250);

    Shell []shells=new Shell[50];

    int planeX=250;
    int planeY=250;

    @Override
    public void paint(Graphics g) { //自动被调用,相当于画笔

        g.drawImage(bg, 0, 0, null);
        g.drawImage(plane, planeX, planeY, null);
        p.drawSelf(g);
        for (int i = 0; i < shells.length; i++) {
            shells[i].draw(g);
        }
//        Color c=g.getColor();
//
//        Font f=g.getFont();
//        g.setColor(Color.BLUE);
//
//        g.drawLine(100,100,300,300);
//        g.drawRect(100,100,300,300);
//        g.drawOval(100,100,300,300);
////        g.drawArc(100,100,200,200,300,300);
//        g.fillRect(100,100,100,100);
//        g.setColor(Color.RED);
//        g.setFont(new Font("宋体",Font.BOLD,50));
//        g.drawString("我是谁?",200,200);
//
//        g.drawImage(ball,250,250,null);
//
//        g.setColor(c);
//        g.setFont(f);


    }
    class keyMonitor extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            p.addDirection(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            p.minusDirection(e);
        }
    }

    public void launchFrame() {
        this.setTitle("Feb20th");
        this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
        this.setLocation(100, 100);
        setVisible(true);

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        new PaintThread().start();//启动重画窗口的线程
        addKeyListener(new keyMonitor());//给窗口增加键盘的监听

        //初始化50个炮弹
        for (int i = 0; i < shells.length; i++) {
            shells[i] = new Shell();
        }
    }


    public static void main(String[] args) {
        Feb20thPlane f = new Feb20thPlane();
        f.launchFrame();
    }


    class PaintThread extends Thread {
        @Override
        public void run() {
            while (true) {
                repaint();
                try {
                    Thread.sleep(40); //1s=1000ms
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 38174楼

我用了数组来画炮弹,炮弹发射的角度是任意的,但运行了好几次却发现炮弹就成一条直线了,是哪里出了问题呢


image.png



Shell.java

zzu.baizhan.Feb.Day20th;

java.awt.*;
java.util.Random;

Shell GameObject {
    ;

    Shell() {
        = ;
        = ;
        = ;
        = ;
        = ;

        = Math.()*Math.*;
    }

    draw(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.);
        g.fillOval(() , () , , );
        += * Math.();
        += * Math.();

        (< || > Constant.- ) {
            = Math.- ;
        }
        (< || < Constant.- ) {
            = -;
        }

        g.setColor(c);
    }
}


main:

zzu.baizhan.Feb.Day20th;

javax.swing.*;
java.awt.*;
java.awt.event.KeyAdapter;
java.awt.event.KeyEvent;
java.awt.event.WindowAdapter;
java.awt.event.WindowEvent;

Feb20thPlane JFrame {

    Image = Feb20thGameUtil.();
    Image = Feb20thGameUtil.();
    Image = Feb20thGameUtil.();
    Plane =Plane(,,);

    Shell []=Shell[];

    =;
    =;

    paint(Graphics g) { g.drawImage(, , , );
        g.drawImage(, , , );
        .drawSelf(g);
        (i = ; i < .; i++) {
            [i].draw(g);
        }
}
    keyMonitor KeyAdapter {
        keyPressed(KeyEvent e) {
            .addDirection(e);
        }

        keyReleased(KeyEvent e) {
            .minusDirection(e);
        }
    }

    launchFrame() {
        .setTitle();
        .setSize(Constant., Constant.);
        .setLocation(, );
        setVisible();

        .addWindowListener(WindowAdapter() {
            windowClosing(WindowEvent e) {
                System.();
            }
        });
        PaintThread().start();addKeyListener(keyMonitor());(i = ; i < .; i++) {
            [i] = Shell();
        }
    }


    main(String[] args) {
        Feb20thPlane f = Feb20thPlane();
        f.launchFrame();
    }


    PaintThread Thread {
        run() {
            () {
                repaint();
                {
                    Thread.(); } (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}



JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 38175楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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