=====Pong=====
====scoring====
Scoring in pong is as simple as checking if the ball has left the screen; if it crosses the left side the right player gets a point, and vice versa.
For our purposes, we can check if the ball's x position has exceeded 640 or subceeded 0
====score storage====
You can store the score and other variables you may use such as x and y positions to locations in memory by defining it, for example:
%define LScore 0x00000000
You can then load each score into a register before use so you can use the same score tracking function for both the right and left players scores by loading different scores before calling the function.
====score display====
Displaying the player's scores is where things start to get more complex, as we no longer have built in 'itoa' or 'print' functions, so we will have to build them manually.
First off, and debatably more complex portion, is creating an itoa function\\
To do this we can break down what we need to get printable characters from any arbitrary number\\
That process requires getting each digit from our number, ie to print 104, we need to have print a 1, a 0, and a 4, as they are all separate regions is the BIOS texture\\
One possible way of doing this is to use math to pick apart a number from right to left, converting each digit to its ASCII value, and store them, in a process that may look like:
MOV R0, 104
MOV R1, R0 ; Store a copy of the original into R1
_itoa:
MOV R2, R1 ; Store a copy of the number into R2
IMOD R2, 10 ; Retrieve the last digit from the copy
IADD R2, 0x30 ; Convert the number to its ASCII value
PUSH R2 ; Store the digit onto the stack
IDIV R1, 10 ; Remove the last digit from the original
MOV R3, R1 ; ;
IEQ R3, 0 ; Repeat until zero ;
JF R3, _itoa ; ;
Once we have each character we want to print, printing them is a simple matter\\
The process may look like:
%define SELREG GPU_SelectedRegion
%define SELTEX GPU_SelectedTexture
%define DRAWX GPU_DrawingPointX
%define DRAWY GPU_DrawingPointY
%define GPUCOM GPU_Command
%define DRAWREG GPUCommand_DrawRegion
...
OUT SELTEX, -1 ; Select the BIOS texture
MOV R0, 0 ; Place a dummy value onto the stack ;
PUSH R0 ; ;
CALL _itoa ; (assuming itoa has a RET instruction)
_innerloop:
POP R0 ; Retrieve the first character
MOV R1, R0
IEQ R1, 0
JF R1, _return
OUT SELREG, R0 ; Set the region
OUT DRAWX, RX ; Set the x position (whatever register contains the xpos)
OUT DRAWY, # ; Set the y position
OUT GPUCOM, DRAWREG ; Draw the character
IADD RX, 10 ; Move xpos to next open space
JMP _innerloop
_return:
RET
====computer operated player====
====determining computer paddle movement====
The simplest version of a computer operated player has the bot moving the paddle to the same vertical position as the ball.
First we need to get the difference in position between the paddle and the ball.
*NOTE* we want the center of the paddle to hit the center of the ball, so we must make sure to compensate for the distance between the hotpots and centers of the paddle
This will tell us the direction and magnitude the paddle should move, but we will want to cap the movement speed of the paddle so the bot isn't too good.
We can also add a small multiplier to further limit the movement of the bot.
_npcMovement:
;RY is the register containing the ball's y pos
;RP is the register containing the paddles y pos
;RT is a temporary register
mov RT, RY ;Store a copy of the ball's ypos
isub RT, RP ;Get the difference in position
iadd RT, 20 ;Compensate for any differences in sizes
imul RT, 2 ;Optional slow down on bot movement
idiv RT, 3 ;This will make the bot 2/3 the responsive
imax RT, -10 ;Limit the speed to -5 pixels and 5 pixels
imin RT, 10
iadd RP, RT ;Move the bot's paddle acordingly
RET
====difficulty of computer player====
The difficulty of the computer can be tuned by changing the max speed and responsiveness during their movement step
Another way to increase difficulty is to have the ball move faster or have a smaller paddle. You could also give the computer player a larger paddle.