Changes

Jump to: navigation, search

Programming:Display and update Scores

3,493 bytes added, 04:18, 21 September 2007
Finished the page
== Methods for Storing , Displaying and Updating Scores in Memory ==
There are a number of methods for storing a Score in memory, here are a few examples:<br>
 
=== A Single Word ===
This is the simplest and smallest method for storing the score in memory, but it has a couple of disadvantages over some other methods. It can only hold a score up to 65535 (although you can add an extra '0' or two on the display to make that 655350 or 6553500), converting it to digits for display requires a relatively slow algorithm, and adding extra lives every 10000 points for example can take a complex routine. The advantage of using a straight Word is that the maths required to add points to the score is very simple and fast.<br>
</pre>
=== One Byte per Digit ===This is one of the simplest methods of storing and displaying scores. Its main disadvantages are that it uses slightly more memory and can't usually be held in registers, so must be copied around (eg. for high score tables). Its main advantages are that the scores are easy to display, update and can be easily read in a memory editor.<br><br>To define the score (6 digits):<pre>.score ds 6</pre>To add a value to the score, you usually decide which digit you wish to add to, then start at that digit, in this routine, B holds the value to add, C is the digit number.<pre>.add_score_b_cld a,bld hl,score - 2560ld b,10add hl,bcadd (hl)ld (hl),asub bret cld (hl),ainc c.add_loopdec cret zdec hl ;; You can test the value in C here to determine when to increment lives (eg. ld a,c:cp 2:call z,inc_lives)inc (hl)ld a,(hl)sub bret nzld (hl),ajr add_loop</pre>Displaying the score is really easy. All you have to do is add the value of the '0' character to each digit:<pre>.show_scoreld hl,scoreld b,6.show_loopld a,(hl)inc hladd '0'call show_chardjnz show_loopret</pre> === One character per digit ===This method is identical to the one above, but you actually store the character representation of each digit, meaning you don't have to add the '0' value to each digit for display. In this case, you could use a generic string display routine to show the score. In this example I'm using a zero terminated string.<br><br>To define the score:<pre>.score db '000000',0</pre>This add routine can only increment a particular digit, simplifying it somewhat (eg. Add 1, 10, 100, 1000 to the score). L holds the offset. I've also used another trick here, by assuming the score is page-aligned.<pre>.inc_scoreld h,score / 256.next_digitinc (hl)ld a,(hl)cp '9' + 1ret nzld (hl),'0'dec l ; You can test the value in L here to determine when to increment lives (eg. ld a,l:cp 2:call z,inc_lives)jp p,next_digitret</pre>And the routine to display the score is a simple zero terminated string printing routine, just call it with HL pointing to <b>score</b>:<pre>.print_hlld a,(hl)inc hlor aret zcall show_charjr print_hl</pre> == Some More Information ==Each of these routines has its advantages and disadvantages, and it usually depends on the type of game you're writing or your own preferences which one you pick to comeuse.A few questions you should consider before deciding which routines to use are:<br><br><li>What's the maximum score in my game, or how big would you like the score before it clocks over?<li>How many times in a cycle (eg. frame) does the score get incremented?<li>Will the score be redisplayed each time it's updated, once every cycle, or only rarely (eg. in between turns)?<li>How fast is the character printing routine?<br><li>Is there going to be a high score shown or a high score table in the game?<br><br>Depending on the answers to these questions you should be able to pick the best routine. For example, it's almost impossible in Frogger to get past 20,000 points, and the score only updates in multiples of 10 points so for this the Single BCD Word method was used with an extra '0' appended to the end. The score would clock after 99,990 points. It's also easy with this method to compare it with the high score each time points are added.Frogger displays both the current player's score and the high score every frame using a very fast character printing routine.
[[User:Executioner|Executioner]] 0106:2718, 21 September 2007 (CEST)
151
edits