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

2023/3/31

HELP ASAP

envxity envxity

2023/3/31

#
so i ruined my game that i was making and now it barely works like it was meant to here is the source code its due today. so pls help
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
import greenfoot.*;
public class MyWorld extends World
{
    int count;
    int spawnSpeed = 50;
    int randomSpawn;   
    int wave = 1;   
    int pause;
    public Player mainPlayer = new Player();
    Counter counter = new Counter();
    HealthBar Healthbar = new HealthBar(mainPlayer);
    WeaponButton weaponButton = new WeaponButton(counter);
    button button = new button();
    SuperPower superPower = new SuperPower();
    public MyWorld()
    {
        super(1000, 800, 1);
        mainPlayer = new Player(weaponButton, superPower);
        addObject(button,976,25);
        addObject(mainPlayer,getWidth()/2, getHeight()/2);
        addObject(counter,96,70);
        addObject(Healthbar, mainPlayer.getX()-5, mainPlayer.getY()-50);
        addObject(weaponButton, 900, 100);
        addObject(superPower, mainPlayer.getX()+10, mainPlayer.getY()-80);
         
    }
    public Player getPlayer()
    {
        return mainPlayer;
    }
    public void act(){
        count++;
        spawnZombies();
        decreaseSpawnSpeed();
    }
    public void spawnZombies(){
        if(count % spawnSpeed == 0){
            randomSpawn = Greenfoot.getRandomNumber(8);
            switch(randomSpawn){
                case 0 : addObject(new Zombie(mainPlayer, counter), 0,0); break;
                case 1 : addObject(new Zombie(mainPlayer, counter), getWidth()/2,0); break;
                case 2 : addObject(new Zombie(mainPlayer, counter), getWidth(),0); break;
                case 3 : addObject(new Zombie(mainPlayer, counter), 0,getHeight()/2); break;
                case 4 : addObject(new Zombie(mainPlayer, counter), getWidth(),getHeight()/2); break;
                case 5 : addObject(new Zombie(mainPlayer, counter), 0,getHeight()); break;
                case 6 : addObject(new Zombie(mainPlayer, counter), getWidth()/2,getHeight()); break;
                case 7 : addObject(new Zombie(mainPlayer, counter), getWidth(),getHeight()); break;
            }               
        }
    }
    public void decreaseSpawnSpeed(){
        if(count % 600 == 0)
        {
            spawnSpeed--;
        }
    }
}
import greenfoot.*;
public class HealthBar extends Actor
{
    int health = 50;
    double specificHealth = (double)health;
    Player mainPlayer;
    Zombie zombie;
    public HealthBar(Player mainPlayer)
    {
        this.mainPlayer = mainPlayer;
        createImage();
    }
    public void act()
    {
        createImage();          
        World world = getWorld();
        MyWorld game = (MyWorld)world;
        setLocation(game.getPlayer().getX() - 5, game.getPlayer().getY() - 50);
        LoseHealth();
    }
    public void createImage()
    {
        setImage(new GreenfootImage(52,12));
        getImage().drawRect(0,0,51,11);
        getImage().setColor(Color.RED);
        getImage().fillRect(1,1,health,10);
        health--;
    }
    public void LoseHealth()
    {
        World world = getWorld();
        MyWorld game = (MyWorld)world;
         
        if (game.getPlayer().hitByZombie() && zombie != null && getNeighbours(getImage().getWidth()/2, false, Zombie.class).contains(zombie) && zombie.hitsPlayer(mainPlayer))
        {
            health--;
        }
    }
}
import greenfoot.*;
import greenfoot.MouseInfo;
 
public class Player extends Actor
{
    // all the variables for the players stats
    Counter counter;
    Zombie zombie;
    public final int speed = 3;
    public int firerate = 10;
    int health = 100;
    int stamina = 10;
    int cash = 0;
    int wave = 1;
    int ammo = 20;
    int superTimer;
 
    public int pause = 100;
    int kills = 0;
    int survivedTime = 0;
    private boolean reloadKeyDown = false;
    int time = 0;
    private final int reloadTime = 10,
    maxShots = 30;
    private int reloadTimer = 0,
    shotsLeft = maxShots;
    private boolean shooting = false;
    boolean farthest = false;
    WeaponButton weaponButton;
    SuperPower superPower;
    /* BY GARRETT*/
 
    // creates the player character
    public Player(){
        setImage(new GreenfootImage(70, 50));
        getImage().setColor(Color.YELLOW);
        getImage().fillOval(0, 0, 50, 50);
        getImage().setColor(Color.BLACK);
        getImage().fillRect(50, 20, 70, 10);
    }
 
    public Player(WeaponButton weaponButton, SuperPower superPower){
        this.superPower = superPower;
        this.weaponButton = weaponButton;
        setImage(new GreenfootImage(70, 50));
        getImage().setColor(Color.YELLOW);
        getImage().fillOval(0, 0, 50, 50);
        getImage().setColor(Color.BLACK);
        getImage().fillRect(50, 20, 70, 10);
    }
    // tells the player character to use all the methods that have been created as long as the conditions are right for the given method
    public void act()
    {
        move();
        turnAround();
        shoot();
        time++;
        hitByZombie();
 
    }
    // tells the actor to turn to face the mouse
    public void turnAround(){
        button button = new button();
        if (Color.BLACK.equals(button.getCurrentColor())){
            if(Greenfoot.getMouseInfo() != null){
                turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
            }
        }
    }
    // tells the actor to do if the button is black and if it isnt black
    public void shoot() {
        button button = new button();
        if (Color.BLACK.equals(button.getCurrentColor())){
            manualShoot();           
        }
        else {
            autoShoot();
        }
    }
    // returns the buttons color to determine what the firemode is
    public static int getColor()
    {
        return button.colorValue;
    }
    // one of the firemodes that is player skilled based
    private void manualShoot()
    {       
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(ammo>0){
            if (Greenfoot.mousePressed(null) && weaponButton.WeaponUpgrade == 1)
            {
                turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
                Bullet bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.setRotation(getRotation()-0);
                bullet.move(25);
            }
            if (Greenfoot.mousePressed(null) && weaponButton.WeaponUpgrade == 2)
            {
                Bullet bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.setRotation(getRotation()-5);
                bullet.move(25);
                Bullet bullet2 = new Bullet();
                getWorld().addObject(bullet2, getX(), getY());
                bullet2.setRotation(getRotation()+5);
                bullet2.move(25);
            }
            if (Greenfoot.mousePressed(null) && weaponButton.WeaponUpgrade == 3)
            {
                Bullet bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.setRotation(getRotation()-5);
                bullet.move(25);
                Bullet bullet2 = new Bullet();
                getWorld().addObject(bullet2, getX(), getY());
                bullet2.setRotation(getRotation()+5);
                bullet2.move(25);
                Bullet bullet3 = new Bullet();
                getWorld().addObject(bullet3, getX(), getY());
                bullet3.setRotation(getRotation()-0);
                bullet3.move(25);
            }
            if (Greenfoot.mousePressed(null) && weaponButton.WeaponUpgrade == 4)
            {
                Bullet bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.setRotation(getRotation()-10);
                bullet.move(25);
                Bullet bullet2 = new Bullet();
                getWorld().addObject(bullet2, getX(), getY());
                bullet2.setRotation(getRotation()+10);
                bullet2.move(25);
                Bullet bullet3 = new Bullet();
                getWorld().addObject(bullet3, getX(), getY());
                bullet3.setRotation(getRotation()-5);
                bullet3.move(25);
                Bullet bullet4 = new Bullet();
                getWorld().addObject(bullet4, getX(), getY());
                bullet4.setRotation(getRotation()+5);
                bullet4.move(25);
            }
            if (Greenfoot.mousePressed(null) && weaponButton.WeaponUpgrade == 5)
            {
                Bullet bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.setRotation(getRotation()-10);
                bullet.move(25);
                Bullet bullet2 = new Bullet();
                getWorld().addObject(bullet2, getX(), getY());
                bullet2.setRotation(getRotation()+10);
                bullet2.move(25);
                Bullet bullet3 = new Bullet();
                getWorld().addObject(bullet3, getX(), getY());
                bullet3.setRotation(getRotation()-0);
                bullet3.move(25);
                Bullet bullet4 = new Bullet();
                getWorld().addObject(bullet4, getX(), getY());
                bullet4.setRotation(getRotation()-5);
                bullet4.move(25);
                Bullet bullet5 = new Bullet();
                getWorld().addObject(bullet5, getX(), getY());
                bullet5.setRotation(getRotation()+5);
                bullet5.move(25);
            }
        }
    }
 
    public void useSuperPower()
    {
        if(superPower.superCount>99 && superTimer< 30)
        {
            superTimer++;
        }
        if(superTimer > 30)
        {
            Bullet bullet = new Bullet();
            getWorld().addObject(bullet, getX(), getY());
            bullet.setRotation(getRotation()-60);
            bullet.move(25);
            Bullet bullet2 = new Bullet();
            getWorld().addObject(bullet2, getX(), getY());
            bullet.setRotation(getRotation()+60);
            bullet2.move(25);
            Bullet bullet3 = new Bullet();
            getWorld().addObject(bullet3, getX(), getY());
            bullet.setRotation(getRotation()-0);
            bullet3.move(25);
            Bullet bullet4 = new Bullet();
            getWorld().addObject(bullet4, getX(), getY());
            bullet.setRotation(getRotation()-180);
            bullet4.move(25);
            Bullet bullet5 = new Bullet();
            getWorld().addObject(bullet5, getX(), getY());
            bullet.setRotation(getRotation()+120);
            bullet5.move(25);
            Bullet bullet6 = new Bullet();
            getWorld().addObject(bullet6, getX(), getY());
            bullet.setRotation(getRotation()-120);
            bullet6.move(25);
            superTimer++; 
        }
    }
    // the second fire mode that is more of a auto fire to the nearest enemy
    private void autoShoot()
    {       
        if(pause>0){
            pause --;
        }
        if(pause == 0){
            if (ammo > 0) {
                if (getWorld().getObjects(Zombie.class).isEmpty()) return;
                Actor closest = null;
                int closeness = 9999;
                for (Object obj : getWorld().getObjects(Zombie.class)) {
                    Actor enemy = (Actor)obj;
                    int dist = (int)Math.hypot(getX()-enemy.getX(), getY()-enemy.getY());
                    if (dist < closeness) {
                        closeness = dist;
                        closest = enemy;
                    }
                }
                Actor bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.turnTowards(closest.getX(), closest.getY());
                setRotation(bullet.getRotation());
                ammo--;
                pause = firerate;
            }       
        }
    }
    // gets the users inputs and moves the player accordingly
    public void move(){
        if(Greenfoot.isKeyDown("W")||Greenfoot.isKeyDown("UP")){
            setLocation( getX(), getY()-speed);
        }
        if(Greenfoot.isKeyDown("A")||Greenfoot.isKeyDown("LEFT")){
            setLocation( getX()-speed, getY());
        }
        if(Greenfoot.isKeyDown("S")||Greenfoot.isKeyDown("DOWN")){
            setLocation( getX(), getY()+speed);
        }
        if(Greenfoot.isKeyDown("D")||Greenfoot.isKeyDown("RIGHT")){
            setLocation( getX()+speed, getY());
        }
    }
    // tells the character to reload his gun when the user hits r
    public void hitbyZombie()
    {
        Bullet bullet = (Bullet)getOneIntersectingObject(Bullet.class);
        health --;
        getWorld().removeObject(bullet);
 
    }
 
    public void youWin(){
        if(time >100 && !isTouching(Zombie.class)){
            getWorld().showText("You Win! - you Survived the 60 seconds ", getWorld().getWidth()/2, getWorld().getHeight()/2);
        }
    }
 
    public boolean hitByZombie()
    {
        Zombie zombie = (Zombie)getOneIntersectingObject(Zombie.class);
        if (zombie != null && getNeighbours(getImage().getWidth()/2, false, Zombie.class).contains(zombie) && zombie.hitsPlayer(this))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
import greenfoot.*;
public class Bullet extends Actor
{   
    public Bullet(){
        setImage(new GreenfootImage(10,5));
        getImage().setColor(Color.BLACK);
        getImage().fillRect(0,0,10,5);
    }
    public void act()
    {
        move(10);
        DestroyBullet();
         
         
    }
    public void move(int distance)
    {
        double radians = Math.toRadians(getRotation());
        int dx = (int) Math.round(Math.cos(radians) * distance);
        int dy = (int) Math.round(Math.sin(radians) * distance);
        setLocation(getX() + dx, getY() + dy);
    }
    private void DestroyBullet(){
        if(isAtEdge()){
            getWorld().removeObject(this);
        }
    }
    public boolean hitsZombie(Zombie zombie)
    {
        return getObjectsInRange(getImage().getWidth()/2, Zombie.class).contains(zombie);
    }
}
import greenfoot.*;
public class Zombie extends Actor
{
    int animateImage = 0;
    int animateSpeed = 5;
    int count;
    int health = 3;
    Player player;
    Counter counter;
    public Zombie(Player mainPlayer, Counter counter){
        this.counter = counter;
        player = mainPlayer;
        setImage("skeleton-idle_16.png");
        getImage().scale(80,80);   
    }
    public void act()
    {
        count++;
        animate();
        moveAround();
        hitByBullet();
    }
    // animates the zombie
    public void animate(){
        if(count % animateSpeed == 0){
            if(animateImage > 16){
                animateImage = 0;               
            }
            setImage("skeleton-move_"+ animateImage + ".png");
            animateImage++;
            getImage().scale(80,80);
        }
    }
    private void moveAround(){
        move(1);
        turnTowards(player.getX(), player.getY());
    }
    public void hitByBullet()
    {
        Bullet bullet = (Bullet)getOneIntersectingObject(Bullet.class);
        if (bullet != null && getNeighbours(getImage().getWidth()/2, false, Bullet.class).contains(bullet) && bullet.hitsZombie(this))
        {
            health--;
        }
        if(health == 0){
            getWorld().removeObject(this);
            counter.Score++;
        }
 
    }
    public boolean hitsPlayer(Player mainPlayer)
    {
        return getObjectsInRange(getImage().getWidth()/2, Player.class).contains(mainPlayer);
    }
}
import greenfoot.*;
public class Counter extends Actor
{
    int Score;
    int cash;
    public Counter(){
        setImage(new GreenfootImage( "Score:"+ Score+
        "cash:" + cash, 40, Color.BLACK, new Color(0,0,0,0)));
    }
    public void act()
    {
        setImage(new GreenfootImage( "Score:"+ Score+
        "cash:" + cash, 40, Color.BLACK, new Color(0,0,0,0)));
    }
}
import greenfoot.*;
public class SuperPower extends Actor
{
    final int SUPER_POWER_LIMIT = 100;
    int superCount;
    int count;
    int superTimer;
     
    
    public SuperPower()
    {
        createImage();
    }
    public void act ()
    {
        createImage();          
        World world = getWorld();
        MyWorld game = (MyWorld)world;
        game.getPlayer();
        setLocation(game.getPlayer().getX() + 10, game.getPlayer().getY() - 80);
        useSuper();
         
    }
    public void useSuper()
    {
        count++;
        if(count % 10 == 0)
        {
            superCount++;
        }
        if(superCount >= 100)
        {
            superCount = 0;
        }
         
    }
    public void createImage()
    {
        setImage(new GreenfootImage(SUPER_POWER_LIMIT + 2,12));
        getImage().drawRect(0,0,SUPER_POWER_LIMIT + 1,11);
        getImage().setColor(Color.BLUE);
        getImage().fillRect(1,1,superCount,10);
    }
     
}
import greenfoot.*;
public class WeaponButton extends Actor
{
    int cost = 150;
    int WeaponUpgrade = 1;
     
    Counter counter;
    public WeaponButton(Counter counter)
    {
        this.counter = counter;
        setImage(new GreenfootImage("Weapon \n upgrade \n cost: " + cost, 25, Color.BLACK, new Color(0,0,0,0)));
    }
    public void act(){
        upgrade();
    }
    public void upgrade()
    {
        if(Greenfoot.mousePressed(this) && counter.cash >149)
        {
            counter.cash -= 150;
            cost +=50;
            WeaponUpgrade++;
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class button extends Actor
{
    Color[] colors = { Color.BLACK, Color.RED };
    public static int colorValue = 0; // index of colors array
         
    public button() {
        setImage(new GreenfootImage(70, 50));
        changeColor();
        changeColor();
    }
    public Color getCurrentColor() {
        return colors[colorValue];
    }    
    public void changeColor() {
        colorValue = (colorValue+1)%2; // toggle index between 0 and 1
        getImage().setColor(getCurrentColor());
        getImage().fillOval(0, 0, 20 , 20);
    }    
    public void act()
    {
        if (Greenfoot.mouseClicked(this)) {
            changeColor();
        }
    }
     
}
danpost danpost

2023/3/31

#
envxity wrote...
so i ruined my game that i was making and now it barely works like it was meant to here is the source code its due today. so pls help << Codes Omitted >>
I did notice that in multiple places you have images being created continuously (every act frame). Image creation/manipulation can be a hog on CPU time and may cause some lagging. Also, I noticed that you have the health bar and the counter doing things they should not. GUI objects, as such, should be prompted to do things, not do them on their own -- and only do those things when required. In other words, they should not have an act method at all; and, the methods in the class should be called or values in the class should be changed only when necessary. You are also passing a lot of things around when creating objects. That should be minimized or eliminated altogether.
You need to login to post a reply.