This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dudimus
Dudimus wrote ...

2016/8/20

What are the steps to coding/making an "extra ammo" collectable thing so that when the character gets it, the ammo becomes full again?

Dudimus Dudimus

2016/8/20

#
//Here's the code for the character I'm using (which is from danpost's game) so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import greenfoot.*;
import java.awt.Color;
 
/**
 * Class Chased (subclass of Actor): a user-controlled object with a mouse-controlled gun
 */
public class Chased extends Actor
{
    boolean touchingZ = false;
  
    boolean touchingFire = false;
    Gun gun = new Gun(); // the gun for this actor
    Chaser chaser = new Chaser();
  
 
     
    int ammo=10;
  
     
    int reloadTime=50;
    int reload=0;
    /**
     * creates image for actor
     */
    public Chased()
    {
        // the image for this actor
        GreenfootImage image = new GreenfootImage(50, 50);
        image.fillOval(0, 0, 50, 50);
        image.setColor(Color.green);
        image.fillOval(7, 7, 36, 36);
        setImage(image);
    }
     
    /**
     * adds the gun for this actor into the world
     *
     * @param world the world this actor was added into
     */
    public void addedToWorld(World world)
    {
        world.addObject(gun, getX(), getY()); // add the gun belonging to this actor into the world
    }
     
 
      /**
     * move user-controlled actor and its gun; also, check for game over
     */
    public void act()
    {
        // user-controlled movement
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("a")) dx--;
        if (Greenfoot.isKeyDown("d")) dx++;
        if (Greenfoot.isKeyDown("w")) dy--;
        if (Greenfoot.isKeyDown("s")) dy++;
        setLocation(getX()+3*dx, getY()+3*dy);
        // limit to world bounds
        if (getX() < 25) setLocation(25, getY());
        if (getY() < 25) setLocation(getX(), 25);
        if (getX() > getWorld().getWidth()-25) setLocation(getWorld().getWidth()-25, getY());
        if (getY() > getWorld().getHeight()-25) setLocation(getX(), getWorld().getHeight()-25);
        // position gun
        gun.setLocation(getX(), getY());
        // check game over
       
         Actor chaser = getOneIntersectingObject(Chaser.class);
        
          
        if (chaser != null)
                {
            World world = getWorld();
                    Welt welt = (Welt)world;
                    HealthBar healthbar = welt.getHealthBar();
                    if(touchingZ == false)
                    {
                        healthbar.loseHealth();
                        touchingZ = true;
                        if(healthbar.health <=0)
                        {
                         world.removeObjects(world.getObjects(null));
                          Greenfoot.setWorld(new Stats(((Welt)world).score));
                        }
                    }
        }else{
                    touchingZ = false;
                }
                
    }
    
    /**
     * Class: Gun (subclass of Actor -- inner class of Chased): the gun for this actor
     */
    private class Gun extends Actor
    {
        /**
         * creates image for actor
         */
        public Gun()
        {
            // the image for this actor
            GreenfootImage image = new GreenfootImage("Picture1.png");
            
            setImage(image);
        }
          
    public boolean Munition(){
        if(ammo>0){
            return true;
        }
        else{
            return false;
        }
    }
 
    private boolean mouseclick;
    /**
         * responds to mouse movement and mouse button clicks
         */
        public void act()
        {
            // turn towards mouse when mouse moves
            if (Greenfoot.mouseMoved(null) || Greenfoot.mouseDragged(null))
            {
                MouseInfo mouse = Greenfoot.getMouseInfo();
                if (mouse != null) turnTowards(mouse.getX(), mouse.getY());
            }
            // detect mouse clicks to fire shot  
            
            World world = getWorld();
                    Welt welt = (Welt)world;
                    AmmoBar ammobar = welt.getAmmoBar();
            if(!mouseclick && (Greenfoot.mouseClicked(null)))
            {
                if(Munition()==true)
                {
                    mouseclick=true;
                    getWorld().addObject(new Shot(), getX(), getY());
                    ammo--;
                    ammobar.loseAmmo();
                }
            }
            if(mouseclick&& !Greenfoot.isKeyDown("space"))
            {
                mouseclick=false;
            }
             
          
             
        }
        
        /**
         * Class Shot (subclass of QActor -- inner class of Chased.Gun): the shots from this gun
         */
        private class Shot extends QActor
        {
            /**
             * sets bounds fields and creates the image for this actor
             */
            public Shot()
            {
                setBoundedAction(QActor.REMOVE, 5); // set bounds fields
                // create image for this actor
                GreenfootImage image = new GreenfootImage("FIRE.png");
                 
                setImage(image);
 
            }
             
            /**
             * initializes rotation and position of actor
             *
             * @param world the world this actor was added into
             */
            public void addedToWorld(World world)
            {
                setRotation(Gun.this.getRotation()); // set rotation (to that of gun)
                move(5000); // set position (at end of gun)
            }
             
            /**
             * moves actor and checks for removal of actor
             */
            public void act()
            {
                setVX(0);
                setVY(0);
                addForce(0, getRotation()*QVAL);
                move(); // moving (equivalent to 'move(5)' for a non-QActor)
                if (hits(Chaser.class) || atWorldEdge()) getWorld().removeObject(this); // removing
            }
             
                  /**
             * internal method to detect object collision; returns true if collision detected, else false
             *
             * @param cls the class of object to check for collision with
             * @return a flag indicating whether an object of given class was detected or not
             */
            private boolean hits(Class cls)
            {
                // get intersecting object and return result
                Actor clsActor = getOneIntersectingObject(cls);
                if (clsActor != null)
                {
                    // remove intersector and bump score
                    getWorld().removeObject(clsActor);
                     
                    return true;
                }
                return false;
            }
            /**
             * internal method that returns a flag indicating world edge encroachment
             *
             * @return a flag indicating whether the actor has encroached on a world edge or not
             */
            private boolean atWorldEdge()
            {
                // return state of encroachment on world edge
                return getX() <= 0 || getY() <= 0 ||
                    getX() >= getWorld().getWidth()-1 ||
                    getY() >= getWorld().getHeight()-1;
            }
        }
    }
}
danpost danpost

2016/8/20

#
Just create a class for the "powerup" and have the Chased object "look" for it and when one is found (a) remove it from the world and (b) replenish the ammo (maximize the value of the 'ammo' field. How (when, where, and limitations) these "powerup" objects are placed into the world depends on your desires.
You need to login to post a reply.