Changeset 32

Show
Ignore:
Timestamp:
12/26/07 17:21:41 (1 year ago)
Author:
charlie137
Message:

Added high score

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/guisterax/src/display.d

    r31 r32  
    206206 
    207207    ~this() { 
    208         printf("SDL_Quit\n"); 
    209208        SDL_Quit(); 
    210209    } 
  • trunk/guisterax/src/font.d

    r17 r32  
    4747public: 
    4848    static void s_init() { 
    49         font = new Font("data/font/font.png", "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .:/$ ", 16, 25); 
    50         font_grey = new Font("data/font/font_grey.png", "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .:/$ ", 16, 25); 
     49        font = new Font("data/font/font.png", "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .:/$_=", 16, 25); 
     50        font_grey = new Font("data/font/font_grey.png", "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .:/$_=", 16, 25); 
    5151    } 
    5252    this(in char[] file, in char[] letters, uint w, uint h) { 
     
    109109 
    110110}; 
     111 
     112/** 
     113    Used to get the user input 
     114*/ 
     115class TextInput : Text 
     116 
     117    this(Font font, in Vect pos, bool centered = false) { 
     118        super(font, "", pos, centered); 
     119    } 
     120     
     121    this(in Vect pos, bool centered = false) { 
     122        this(Font.font, pos, centered); 
     123    } 
     124     
     125    override void iter(float dt) { 
     126        for(int key = 'a'; key <= 'z'; ++key) 
     127        if (universe.pressed_key(key)) { 
     128            text ~= key; 
     129            text = toupper(text); 
     130        } 
     131        if (universe.pressed_key(SDLK_BACKSPACE) && text.length > 0) { 
     132            text.length = text.length - 1; 
     133        } 
     134    } 
     135     
     136    override void draw(Display disp, in Vect pos = Vect()) { 
     137          font.draw(disp, text ~ "_", pos + this.pos, centered); 
     138    } 
     139}; 
  • trunk/guisterax/src/game.d

    r30 r32  
    3434import bonus_levels; 
    3535import derelict.sdl.sdl; 
     36import scores; 
     37import std.string : format; 
    3638static import menu; 
    3739 
     
    7173    this(Vect pos = Vect()) { 
    7274        super(pos); 
    73         append(new Entry("NEW GAME", &new_game, Vect(0,0))); 
    74         append(new Entry("HELP", &help, Vect(0, -32))); 
     75        append(new Entry("NEW GAME", &new_game, Vect(0,32))); 
     76        append(new Entry("HELP", &help, Vect(0, 0))); 
     77        append(new Entry("HIGH SCORE", &high_score, Vect(0, -32))); 
    7578        append(new Entry("QUIT", &quit, Vect(0,-64))); 
    7679    } 
     
    8487        Game g = new Game(); 
    8588        g.help(); 
     89    } 
     90     
     91    void high_score() { 
     92        Game g = new Game(); 
     93        g.high_score(); 
    8694    } 
    8795     
     
    118126}; 
    119127 
     128class InputName : Universe 
     129{ 
     130    TextInput text_input; 
     131    char[] name; 
     132     
     133    this() { 
     134        super(); 
     135        append(new Text("TYPE YOUR NAME", Vect(0, 32), true)); 
     136        append(text_input = new TextInput(Vect(0,0), true)); 
     137    } 
     138 
     139     override void iter(float dt) { 
     140        super.iter(dt); 
     141        if (pressed_key(SDLK_RETURN)) { 
     142            name = text_input.text; 
     143            kill(); 
     144        } 
     145    } 
     146}; 
     147 
     148class HighScore : Universe { 
     149    this() { 
     150        super(); 
     151        append(new Text("HIGH SCORE", Vect(0, 200), true)); 
     152        foreach(i, score; Scores().scores) { 
     153            char[] text = format("%d %s : %d", i+1, score.name, score.score); 
     154            append(new Text(text, Vect(0, 138. - 32. * i), true)); 
     155        } 
     156    } 
     157     
     158    override void iter(float dt) { 
     159        super.iter(dt); 
     160        if (pressed_key(SDLK_RETURN)) { 
     161            kill(); 
     162        } 
     163    } 
     164}; 
     165 
    120166class Quit : Exception 
    121167{ 
     
    167213        ship = new Ship(); 
    168214        uint level = 1; 
    169         while(true) { 
     215        while(ship.alive) { 
    170216            // auto bonus_level = new MinesLevel(); 
    171217            // run(bonus_level); 
     
    173219            shop(); 
    174220            wave(level); 
    175             if (!ship.alive) { 
    176                 return; 
    177             } 
    178221            level++; 
    179222        } 
     223        if (Scores().is_high(score)) { 
     224            char[] name = input_name(); 
     225            Scores().add_score(score, name); 
     226            high_score(); 
     227        } 
     228 
    180229    } 
    181230     
     
    184233        run(s); 
    185234    } 
     235     
     236    /** 
     237        Get the name of the player 
     238    */ 
     239    char[] input_name() { 
     240        InputName u = new InputName(); 
     241        run(u); 
     242        return u.name; 
     243    } 
     244     
     245    void high_score() { 
     246        HighScore u = new HighScore(); 
     247        run(u); 
     248    } 
    186249 
    187250    void wave(uint level) { 
    188         writefln("level ", level); 
    189251        Wave u = new Wave(level, ship); 
    190252        u.update(); 
     
    230292    } 
    231293} 
     294 
  • trunk/guisterax/src/main.d

    r30 r32  
    3434import display; 
    3535import game; 
     36import scores; 
    3637import init : init; 
    3738 
     
    5051    new MainDisplay(); 
    5152    Clock(new Clock()); 
     53    // The scores 
     54    new Scores(); 
    5255 
    5356    init(); 
  • trunk/guisterax/src/ship.d

    r31 r32  
    3737import metal; 
    3838import mine; 
     39import sound; 
    3940 
    4041import std.math : abs; 
     
    5051    static Surface[] power_surfs; 
    5152    Sprite power_sprite; 
     53    static Sound explosion_sound; 
    5254     
    5355    float angle_speed = 0.1; 
     
    6668        normal_surfs = Sprite.loadSurfs("data/ship/normal/ship.png", 8, 8); 
    6769        power_surfs = Sprite.loadSurfs("data/ship/power/power.png", 8, 8); 
     70        explosion_sound = new Sound("data/ship/explosion.wav"); 
    6871         
    6972        Debris.s_init(); 
     
    133136             
    134137            if (energy <= 0) { 
     138                explosion_sound.play(); 
    135139                for (int type=0; type < 2; ++type) { 
    136140                    Vect speed = rand(0, 1) * Vect.from_angle(rand(0, 2 * PI)) + this.speed;