如何使用BlueJ进行随机生成器和运动?

huangapple 未分类评论46阅读模式
英文:

How to do bluej random generators and motion?

问题

import cha.*;
import cha.action.*;
import java.awt.*;
import java.util.*;

import static java.lang.Math.*;
import static java.awt.Color.*;

public class NightScene
extends CHApplet {
    Random gen;
    int horizon;
    Sky sky;
    CHLabel name;
    CHOval [] stars;
    Color offwhite; 
    Twinkle tw;
    CHOval shooter;
    ShootMany stm; // Add this line
    CHClip shootingstar;
     
    // ... (other code remains unchanged)
    
    private class ShootMany
    extends CHAction {
        public void step () {
            Random gen5;
            gen5 = new Random ();
            int rs = gen.nextInt(100 + 4900);
            sleep(rs);
            Shoot st = new Shoot(); // Create a new instance of Shoot
            add(st);
            int rl = gen.nextInt(10 + 20);
            st.setLimit(rl);
            st.start();
        }
    }

    // ... (other code remains unchanged)

    public void init() {
        setBackground(black);
        
        // ... (other initialization code remains unchanged)
        
        stm = new ShootMany(); // Initialize ShootMany
        add(stm);
        stm.setLimit(5000);
        
        // ... (other code remains unchanged)
    }
    
    public void start( ) {
        tw.start();
        stm.start(); // Start the ShootMany action
    }
    
    // ... (other code remains unchanged)
}

This modified code includes the necessary changes to implement the ShootMany class and integrate it into the NightScene class. The stm instance of the ShootMany class is created in the init() method and started in the start() method. This should enable the infinite loop of shooting stars as instructed.

英文:

I'm currently in an intro to CSC class, and am struggling with a lab assignment.

We are using BlueJ to learn basic Java functions and our Professor has not taught us what anything really means or how to actually understand it. We are given a package with instructions on what to add to an already existing scene. Currently, I am to add twinkling stars and shooting stars to a horizon. I was able to add one shooting star and the code looks like this:

import cha.*;
import cha.action.*;
import java.awt.*;
import java.util.*;

import static java.lang.Math.*;
import static java.awt.Color.*;

public class NightScene
extends CHApplet {
     Random gen;
     int horizon;
     Sky sky;
     CHLabel name;
     CHOval [] stars;
     Color offwhite; 
     Twinkle tw;
     CHOval shooter;
     Shoot st;
     CHClip shootingstar;
         
    private class Twinkle
    extends CHAction {
        public void step() {
            Random gen3;
            gen3 = new Random();
            int v;
            v = gen3.nextInt(stars.length);
            stars[v].setVisible(false);
            sleep(500);
            // 1000 didn't show any twinkle.  Lower number to show.
            stars[v].setVisible(true);
        }
    }
    private class Shoot
    extends CHAction {
        public void step() {
            shooter.moveOneStep();
            
        }
        public void before() {//THIS IS A METHOD
            Random gen4;
            gen4 = new Random();
            int xl = gen.nextInt(getWidth());
            int yl = gen.nextInt(horizon);
            shooter.setBounds(xl,yl,5,5);
            shooter.setVisible(true);
            int xv = gen.nextInt(-10 + 20);
            int yv = gen.nextInt(-10 + 20);
            shooter.setVelocity(xv,yv);
            shootingstar.play();
        }  
        public void after() {
            shooter.setVisible(false);
        }
    }       
    
    public void init() {
        setBackground(black);
        
        gen = new Random();
        horizon = getHeight() * 4 / 5;
                
        sky = new Sky();
        add(sky);
        sky.setBounds(0, 0, getWidth(), horizon);
                 
        Color darkGreen;
        darkGreen = new Color(0, 102, 0);
        
        CHRectangle lawn;
        lawn = new CHRectangle();
        add(lawn, 0);
        int lh;
        lh = getHeight() - horizon;
        lawn.setBounds(0, horizon, getWidth(), lh);
        lawn.setBackground(darkGreen);
        lawn.setForeground(darkGreen);
        
        offwhite = new Color(255, 255, 204);
        
        Random gen2;
        gen2 = new Random();
        stars = new CHOval[500];
        for(int i = 0; i <stars.length; i++) {
            stars[i] = new CHOval();
            //CHOval oval; FA incorrect
            add (stars[i],0);
            int x;
            x = gen2.nextInt(800);
            // Prof used x = gen2.nextInt(getWidth());
            int y;
            y = gen2.nextInt(horizon);
            // Prof used y = gen2.nextInt(horizon);
            stars[i].setBounds(x,y,1,1);
            stars[i].setBackground(offwhite);
            stars[i].setForeground(offwhite);
            
        }
        
        tw = new Twinkle();
        add(tw);
        tw.setLimit(1000);
        
        shooter = new CHOval();
        add(shooter,0);
        shooter.setBackground(offwhite); 
        shooter.setForeground(offwhite);
        shooter.setVisible(false);
        
        st = new Shoot();
        add(st);
        Random gen5;
        gen5 = new Random();
        int sl = gen.nextInt(10 + 20);
        st.setLimit(sl);
        
        shootingstar = new CHClip();
        add(shootingstar);
        shootingstar.setFile("shootingstar.wav");
        
        House house;
        house = new House();
        add(house, 0);
        int hy = horizon - house.getHeight() * 3 / 4;
        house.setLocation(100, hy);
    } // end of init - DO NOT REMOVE
    
    public void start( ) {
        tw.start();
        st.start();
    }
    
    /***********************************************************
     * DO NOT Change the code below this line.
     ***********************************************************/
    public static void run() {
        int width = 800;
        int height = 600;
        CHApplet applet = new NightScene();
        applet.run(width, height);
    }
    
    private NightScene(){}
}

When instructed to change that code to create an 'infinite loop' of shooting stars,
>Create another private class ShootMany which extends CHAction. The only method in this class will be a step() method. In the method, sleep for a random amount of time - create a random number between 100 and 5000, then use the random number as a parameter in the call to sleep. Then cut the statements to create, name, add and start the object of type Shoot out of the init() and start() methods and place them inside the new step().
>
>In the init() method, declare, create, add and start an object of type ShootMany. Use .setLimit(5000) to repeat the shooting action more or less indefinitely.

From the private class created, my code now looks like:

private class Shoot
    extends CHAction {
        public void step() {
            shooter.moveOneStep();
            
        }
        public void before() {//THIS IS A METHOD
            Random gen4;
            gen4 = new Random();
            int xl = gen.nextInt(getWidth());
            int yl = gen.nextInt(horizon);
            shooter.setBounds(xl,yl,5,5);
            shooter.setVisible(true);
            int xv = gen.nextInt(-10 + 20);
            int yv = gen.nextInt(-10 + 20);
            shooter.setVelocity(xv,yv);
            shootingstar.play();
        }  
        public void after() {
            shooter.setVisible(false);
        }
    }       
    private class ShootMany
    extends CHAction {
        public void step () {
            Random gen5;
            gen5 = new Random ();
            int rs = gen.nextInt(100 + 4900);
            sleep(rs);
            st = new Shoot();
            add(st);
            int rl = gen.nextInt(10 + 20);
            st.setLimit(rl);
            st.start();
        }
    }
    public void init() {
        setBackground(black);
        
        gen = new Random();
        horizon = getHeight() * 4 / 5;
                
        sky = new Sky();
        add(sky);
        sky.setBounds(0, 0, getWidth(), horizon);
                 
        Color darkGreen;
        darkGreen = new Color(0, 102, 0);
        
        CHRectangle lawn;
        lawn = new CHRectangle();
        add(lawn, 0);
        int lh;
        lh = getHeight() - horizon;
        lawn.setBounds(0, horizon, getWidth(), lh);
        lawn.setBackground(darkGreen);
        lawn.setForeground(darkGreen);
        
        offwhite = new Color(255, 255, 204);
        
        Random gen2;
        gen2 = new Random();
        stars = new CHOval[500];
        for(int i = 0; i <stars.length; i++) {
            stars[i] = new CHOval();
            //CHOval oval; FA incorrect
            add (stars[i],0);
            int x;
            x = gen2.nextInt(800);
            // Prof used x = gen2.nextInt(getWidth());
            int y;
            y = gen2.nextInt(horizon);
            // Prof used y = gen2.nextInt(horizon);
            stars[i].setBounds(x,y,1,1);
            stars[i].setBackground(offwhite);
            stars[i].setForeground(offwhite);
            
        }
        tw = new Twinkle();
        add(tw);
        tw.setLimit(1000);
        
        shooter = new CHOval();
        add(shooter,0);
        shooter.setBackground(offwhite); 
        shooter.setForeground(offwhite);
        shooter.setVisible(false);
        
        ShootMany stm;
        stm = new ShootMany();
        add(stm);
        stm.setLimit(5000);
        
        shootingstar = new CHClip();
        add(shootingstar);
        shootingstar.setFile("shootingstar.wav");
        
        House house;
        house = new House();
        add(house, 0);
        int hy = horizon - house.getHeight() * 3 / 4;
        house.setLocation(100, hy);
    } // end of init - DO NOT REMOVE
    
    public void start( ) {
        tw.start();
    }
    
    /***********************************************************
     * DO NOT Change the code below this line.
     ***********************************************************/
    public static void run() {
        int width = 800;
        int height = 600;
        CHApplet applet = new NightScene();
        applet.run(width, height);
    }
    
    private NightScene(){}
}

No shooting stars are showing up. I've tried a few different things that I thought would work but I'm a huge noob and know nothing about actually coding. Could someone please help me?

huangapple
  • 本文由 发表于 2020年4月9日 11:04:35
  • 转载请务必保留本文链接:https://java.coder-hub.com/61113290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定