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

2020/5/26

How to choose 4 blocks ?

Nurnail Nurnail

2020/5/26

#
I'm creating a game like the Android/iOS game: FAiF, you click 4 blocks, the game randomly choose one of them, if sword was choosen, enemy‘s HP -1, if heart was choosen, your HP+1, if skull was choosen, your HP-1. Now I got the blocks randomly placed, and created two charactors, but I don't know how to choose 4 blocks to decrease or increase charactors' HP. Can anyone help me. Sorry for my cripple codes.
//gameworld
import greenfoot.*;  
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

public class SingleEasy1 extends World{
    private ArrayList<Pick> picks = new ArrayList<Pick>();  
    
    Hero hero = new Hero();
    Rogue rogue = new Rogue();
    
    public SingleEasy1(){    
        super(7, 10, 50);  
        showHer(); //player HP
        showRog(); //enemy HP
        addObject(new Pause1(), 3,0);
        addObject(new Hero(), 1,1);
        
        addObject(new Rogue(), 5,1);
        
        for (int i=0; i<getWidth(); i++) {   
            for (int j=3; j<10; j++) { 
                Pick pick = new Pick();        
                picks.add(pick);                
                addObject(pick, i, j);           
            }
        }
        Collections.shuffle(picks);    
        for (int i=0; i<21; i++) {      
            picks.get(i).setSword(); 
            picks.get(i).setImage("Sword.jpg");
        }
        for (int i=21; i<42; i++) {      
            picks.get(i).setHeart();
            picks.get(i).setImage("Heart.jpg");
        }
        for (int i=42; i<49; i++) {      
            picks.get(i).setSkull();
            picks.get(i).setImage("Skull.jpg");
        }
    }
public void act() 
    {
        showHer();
        showRog();
    }
    public void showHer()
    {
        showText(" " + hero.getHer() , 1, 2);
        
    }
    public void showRog(){
    showText(" " + rogue.getRog(), 5, 2);
    }
}
    //blocks
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;


public class Pick extends Actor{
    private boolean isSword=false;        
    private boolean isHeart=false;   
    private boolean isSkull=false;        
    public boolean chooseSword = false;
    public boolean chooseHeart = false;
    public boolean chooseSkull = false;
    
    public void setSword() {         
        isSword = true;
    }
    public void setHeart() {         
        isHeart = true;
    }
    public void setSkull() {         
        isSkull = true;
    }
    
    public boolean getSword(){
        return chooseSword;
    }
    public boolean getHeart(){
        return chooseHeart;
    }
    public boolean getSkull(){
        return chooseSkull;
    }
    
    public void act() {
        checkMouse();  
        
        
}


    
    public void checkMouse(){     
        
        if (Greenfoot.mouseClicked(this)) {       
            MouseInfo mouse = Greenfoot.getMouseInfo(); 
            
            if (mouse.getButton()==1 && isSword) { 
                if (!chooseSword) {                        
                    setImage("Sword_Clicked.jpg");      
                    chooseSword=true;
                    
                } else {                            
                    setImage("Sword.jpg");            
                    chooseSword=false; 
                    
                }
            }
            if (mouse.getButton()==1 && isHeart) { 
                if (!chooseHeart) {                        
                    setImage("Heart_Clicked.jpg");      
                    chooseHeart=true;    
                    
                } else {                            
                    setImage("Heart.jpg");            
                    chooseHeart=false;                  
                }
            }
            if (mouse.getButton()==1 && isSkull) { 
                if (!chooseSkull) {                        
                    setImage("Skull_Clicked.jpg");      
                    chooseSkull=true; 
                    
                } else {                            
                    setImage("Skull.jpg");            
                    chooseSkull=false;                  
                }
            }       
            
        }
    }

}


//player

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Hero extends Actor
{
    public int her;
    public Hero(){
        her = 20;
    }
    public int getHer(){
        return her;
    }
}

//enemy

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Rogue extends Actor
{
    private int rog;
    
    public Rogue(){
        rog = 5;
    }
    public int getRog(){
        return rog;
    }
}
danpost danpost

2020/5/26

#
I would go about it a totally different way. I would have mouse clicking on Pick objects toggle the existence of its reference being in a public static List in the Pick class. Then have the world act pick one when the list contains four elements (clearing the list afterwards). I am quite sure controlling the images would also be quite simple then (although I cannot see how the initial images are set). Other than the static List, the only other field required would be an int id to indicate which type item the Pick instance is. In all, it would simplify and shorten the code quite a bit.
You need to login to post a reply.