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

4 days ago

How to fix to dont log in for saving the score?

1
2
Daav3r Daav3r

4 days ago

#
import greenfoot.*;
import java.util.List;

/**
 * An actor class that can display a scoreboard, using Greenfoot's
 * PlayerData class.  
 * 
 * You typically use this by including some code into the world for when your game ends:
 * 
 * <pre>
 *   addObject(new ScoreBoard(800, 600), getWidth() / 2, getHeight() / 2); 
 * </pre>
 * 
 * Where 800 by 600 should be replaced by the desired size of the score board. 
 * 

public class ScoreBoard extends Actor
{
    // The vertical gap between user images in the scoreboard:
    private static final int GAP = 10;
    // The height of the "All Players"/"Near Me" text at the top:
    private static final int HEADER_TEXT_HEIGHT = 30;
    // The main text color:
    private static final Color MAIN_COLOR = new Color(0x10, 0x10, 0x10); // Svart
    // The score color:
    private static final Color SCORE_COLOR = new Color(0x40, 0x40, 0x40); // orange-y

    /**
     * Constructor for objects of class ScoreBoard.
     * 
     * You can specify the width and height that the score board should be, but
     * a minimum width of 600 will be enforced.
     */
    public ScoreBoard(int width, int height)
    {    
        setImage(new GreenfootImage(Math.max(600, width),  height));
        drawScores();
    }
    
    private void drawString(String text, int x, int y, Color color, int height)
    {
        getImage().drawImage(new GreenfootImage(text, height, color, null), x, y);
    }
    
    private void drawScores()
    { 
        // 50 pixels is the max height of the user image
        final int pixelsPerUser = 50 + 7*GAP;
        // Calculate how many users we have room for:
        final int numUsers = ((getImage().getHeight() - (HEADER_TEXT_HEIGHT + 10)) / pixelsPerUser);
        final int topSpace = getImage().getHeight() - (numUsers * pixelsPerUser) - GAP;

        drawString("Top Scores", 40, topSpace - HEADER_TEXT_HEIGHT - 10, MAIN_COLOR, HEADER_TEXT_HEIGHT);
        drawString("Your Score", 230, topSpace - HEADER_TEXT_HEIGHT - 1, MAIN_COLOR, HEADER_TEXT_HEIGHT);
        
        drawUserPanel(GAP, topSpace, (getImage().getWidth() / 2) - GAP, topSpace + numUsers * pixelsPerUser, UserInfo.getTop(numUsers));
        drawUserPanel(GAP + getImage().getWidth() / 2, topSpace, getImage().getWidth() - GAP, topSpace + numUsers * pixelsPerUser, UserInfo.getNearby(numUsers));
    }
    
    private void drawUserPanel(int left, int top, int right, int bottom, List users)
    {
        getImage().setColor(MAIN_COLOR);
        getImage().drawRect(left, top, right - left, bottom - top);
        
        int y = top + GAP;

        if (users == null) {
            getImage().setColor(Color.WHITE);
            getImage().fillRect(left + 5, y - GAP + 1, right - left - 10, 50 + 2*GAP - 1);
            int x = left + 10;
            drawString("You must be logged in to save your score", x, y+18, MAIN_COLOR, 14);
            return;
        }
        
        if (users.isEmpty()) {
            getImage().setColor(Color.WHITE);
            getImage().fillRect(left + 5, y - GAP + 1, right - left - 10, 50 + 2*GAP - 1);
            int x = left + 10;
            drawString("Please log in to save your score", x, y+18, MAIN_COLOR, 14);
            return;
        }
        
        for (Object obj : users)
        {
            UserInfo playerData = (UserInfo)obj;            
            Color c;
            if (playerData.getUserName().equals(UserInfo.getMyInfo().getUserName())) {
                // Highlight our row in a sky blue colour:
                c = new Color(180, 230, 255);
            }
            else {
                c = Color.WHITE;
            }
            getImage().setColor(c);
            getImage().fillRect(left + 5, y - GAP + 1, right - left - 10, 50 + 2*GAP - 1);

            int x = left + 10;
            drawString("#" + Integer.toString(playerData.getRank()), x, y+18, MAIN_COLOR, 14);
            x += 50;
            drawString(Integer.toString(playerData.getScore()), x, y+18, SCORE_COLOR, 14);
            x += 80;
            getImage().drawImage(playerData.getUserImage(), x, y);
            x += 55;
            drawString(playerData.getUserName(), x, y + 18, MAIN_COLOR, 14);
            y += 50 + 2*GAP;
        }
    }
}
Daav3r Daav3r

4 days ago

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Highscore here.
 * 
 * @author 
 * @version 
 */
public class Highscore extends Actor
{
    UserInfo userInfo;
    boolean loggedIn;
    int highscore;
    boolean storageAvailable = true;
    public Highscore()
    {
        drawText();
         
        if (UserInfo.isStorageAvailable())
        {
            storageAvailable = false;
        } else {
            userInfo = null;
        }
    }
    public void act() 
    {
       getUserInfo();
       if (storageAvailable) setHighscore();
    }  
    private void drawText()
    {
       GreenfootImage img = new GreenfootImage(220, 100);
       img.setColor(new Color(0, 0, 0, 0));
       img.fill();
       img.setColor(Color.LIGHT_GRAY);
       img.setFont(new Font("Pixeled", false, false , 10));
       img.drawString("Highscore: "+highscore, 80, 20);
       setImage(img);
    }
    private void getUserInfo() //not called
    {
        if (UserInfo.isStorageAvailable()) {
            userInfo = UserInfo.getMyInfo();
            loggedIn = true;
        } else {
            loggedIn = false;
            userInfo = null;
        }
    }
    public void setHighscore()
    {
        if (HitCounter.hitscore > highscore)
        {
            highscore = HitCounter.hitscore;
        }
        UserInfo myInfo = UserInfo.getMyInfo();
        if (HitCounter.hitscore > myInfo.getScore()) {
            myInfo.setScore(highscore);
            drawText();
            myInfo.store();  // write back to server
        }
    }
}
danpost danpost

4 days ago

#
See logic at line 19 of the Highscore class. Although, this is something that should probably not be saved in a field as you can never tell when the availability of storage may change (not being logged in is not the only reason why it may not be available). Better is to use the method check when needed.
Daav3r Daav3r

4 days ago

#
Should I just delete line 19 and down 24 then?
danpost danpost

4 days ago

#
Daav3r wrote...
Should I just delete line 19 and down 24 then?
... and lines 12 and 14 (plus make any other necessary changes)
Daav3r Daav3r

4 days ago

#
public void act() { getUserInfo(); if (storageAvailable) setHighscore(); } The (StorageAvailable) is wrong, how should i change it? Because I can't just remove it?
danpost danpost

4 days ago

#
Use "UserInfo.isStorageAvailable()".
danpost wrote...
Better is to use the method check when needed.
Daav3r Daav3r

3 days ago

#
Still dont work, or I'm doing something wrong
Super_Hippo Super_Hippo

3 days ago

#
What do you currently have? Do you get any error messages? If yes, what error messages and pointing to which lines? If not, what happens and what should happen instead? I don’t understand the title to be honest. You can’t write anything to the Greenfoot server in your scenario while you are not logged in.
Daav3r Daav3r

3 days ago

#
There is no error messages, hard to explain, but the this game i made is excatly like flappybird. When the games ends, I want to save the best scorebut instead of saving the score there is, log in to save the score. Can I somewhere here send the game so you self can open the scenario and see the problem?
Daav3r Daav3r

3 days ago

#
DanPost I found your asteroid game, (Asteroids w/improved QActor SuperClass) I want my end to look more like your ending, so the best score saves at the very end when you lose.
danpost danpost

3 days ago

#
Daav3r wrote...
DanPost I found your asteroid game, (Asteroids w/improved QActor SuperClass) I want my end to look more like your ending, so the best score saves at the very end when you lose.
I would think the basics are quite similar to how the imported ScoreBoard class works.
Daav3r Daav3r

3 days ago

#
I worked with it 4h yesterday, On the flappybird game i dont came anywhere, but i tried to do it on asteroids from the book and it worked. There must be something with the score system, something with the another class i think in flappy class, can you take a look?
Daav3r Daav3r

3 days ago

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

public class Flappy extends Actor{
    private final double FLAP = 1.8;
    private boolean oldSpace = false;
    private double yVel = 0;
    private double y = 300;
    private boolean oldTouchingPipe = false;
    private static boolean dead;
    public Flappy(double yVel){
        GreenfootImage image = getImage();
        image.scale(34, 25);
        dead = false;
        this.yVel = yVel;
    }
    public Flappy(){
        this(0);
    }
    
    public void act() 
    {
        //Graviteten
        yVel += 0.06;
        if(spacePressed()){
            yVel = -FLAP;
        }
        //Hur "flappyn" snurrar
        y += yVel;
        setLocation(getX(), y);
        setRotation((int)(yVel * 100));
        if(isAtEdge()){
            dead = true;
        }
        //Mellanrummet mellan rören
        boolean touchingPipe = false;
        for(Pipe pipe : getWorld().getObjects(Pipe.class)){
            if(Math.abs(pipe.getX() - getX()) < 40){
                if(Math.abs(pipe.getY() + 30 - getY()) > 37){
                    dead = true;
                }
                touchingPipe = true;
            }
        }
        //Poäng räkning vid man går förbi rören
        if(!oldTouchingPipe && touchingPipe && !dead){
            Score.add(1);
        }
        oldTouchingPipe = touchingPipe;
        if(dead){
            Background myWorld = (Background) getWorld();
            myWorld.gameOver();
            getWorld().removeObject(this);
        }
     
    }
    //Hela funktionen för att kunna hoppa 
    public boolean spacePressed(){
        boolean pressed = false;
        if(Greenfoot.isKeyDown("space")){
            if(!oldSpace){
                pressed = true;
            }
            //Hoppet, högt vid false
            oldSpace = true;
        }
        else{
            //Vid true funkar inte hopp systemet
            oldSpace = false;
        }
        return pressed;
    }
    public static boolean isAlive(){
        return !dead;
    }//Konstruktion av hur den rör sig framåt
    public void setLocation(double x, double y){
        super.setLocation((int)x, (int)y);
    }
}
danpost danpost

3 days ago

#
I think I know what is wrong. You are not logged in. Press Control-Shift-P and enter a name to log in. Alternatively, you can get there by way of the menu bar with: Edit>>Preferences and selecting the Miscellaneous tab to add a name in the Player Name section.
There are more replies on the next page.
1
2