User Tools

Site Tools


notes:comporg:spring2024:projects:cta0

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
notes:comporg:spring2024:projects:cta0 [2024/02/28 23:30] – [Registers] cmazzaranotes:comporg:spring2024:projects:cta0 [2024/03/04 16:12] (current) – [Collision With Food] wgates1
Line 52: Line 52:
 </code> </code>
  
-Looking the above, we are getting a random value between 0 and 620 and storing that in R0. we then feed that into R14. This is our X value for the food. We then repeat the same steps for the y value for the food. +Looking at the above, we are getting a random value between 0 and 620 and storing that in R0. we then feed that into R14. This is our X value for the food. We then repeat the same steps for the y value for the food. 
  
 +Make sure your object moves at a predictable speed (example: 1 pixel at a time), for if you don't. Then you run the risk of the objects not touching again, as the random value will have a random number assigned to it. Imagine your speed is 5 pixels, then your object will only be able to land on the food in the 2/10 chance it lands in a position ending in 0 or 5.
 +
 +Remember to add one to the result if your region bounds happen to be at 0, as although unlikely, the fruit can still spawn at 0,0 causing an unfortunate game over. You can add to the random number with iadd R0, 1. This snippet of code will add one to R0.
 +
 +====Collision With Food====
 +
 +Collision is extremely simple in Vircon assembly and can be done in just 20-ish lines. To start, we need to store our X and Y values of our snake head into temp registers so they do not get destroyed. We will first start with finding the position difference between the snake head and food (for me, an apple). Here is what that looks like:
 +
 +<code asm>
 +    ; Collision Detection
 +    mov R10, R2 ; Snake head X
 +    isub R10, R14 ; Subtract apple X
 +    iabs R10 ; Absolute value of X difference
 +
 +    mov R11, R3 ; Snake head Y
 +    isub R11, R15 ; Subtract apple Y
 +    iabs R11 ; Absolute value of Y difference
 +</code>
 +
 +From the code snip above, we first store our X and Y values in temp registers that will get destroyed later on. Then we calculated the difference. It does not matter if the result is negative or positive because we take the abs value. We take the absolute value because the difference in position is a difference. It's hard to explain but it just needs to be the abs value (Someone please step in and make this clearer. I cannot figure out how to say it). 
 +
 +Next, we want to check if the snake head comes in contact with the food. Here is what that looks like:
 +<code asm>
 +    ; Check if within 30 pixels
 +    ile R10, 30
 +    jf R10, _no_collision ; If false then go to _no_collision
 +
 +    ; Check if within 48 pixels
 +    ile R11, 48
 +    jf R11, _no_collision ; If false then go to _no_collision
 +</code>
 +
 +Here, we are simply just checking the difference we got for the X and Y values and then either jumping out of this check because they are not in contact or falling through to what will happen if we are in contact with the food. 
 ====Text Area==== ====Text Area====
  
Line 62: Line 95:
   * Registers are used to store values/data in.   * Registers are used to store values/data in.
   * For example in R0 we are storing a value of 1.   * For example in R0 we are storing a value of 1.
-<code bash>+<code assembly>
   mov R0, 1   mov R0, 1
 </code> </code>
   * We can also modify the values in R0.   * We can also modify the values in R0.
-<code bash>+<code assembly>
   iadd R0, 1   iadd R0, 1
 </code> </code>
   * This will make R0 = 2.   * This will make R0 = 2.
  
-A register will also store a "yes" or "no" value from certain operation like "ige" is greater than or equal+A register will also store a "yes" or "no" value from certain operation like IGE, which is "Integer Greater Than or Equal To"
-<code bash>+<code assembly>
   ige R0, 1   ige R0, 1
 </code> </code>
   * This checks if R0 is greater than or equal to 1. If so it returns a 1 for true which will then be stored in R0 and the 2 will be lost forever.   * This checks if R0 is greater than or equal to 1. If so it returns a 1 for true which will then be stored in R0 and the 2 will be lost forever.
-  * But if you wanted to save the 2 in R0, before the "ige" you could+  * But if you wanted to save the 2 in R0, before the "ige" you could copy R0 into another register: 
-  * ile is similar but checks if R0 is less than or equal to 1. +<code assembly>
-<code bash>+
   mov R1, R0   mov R1, R0
 </code> </code>
   * This will copy the 2 in R0 to R1.   * This will copy the 2 in R0 to R1.
  
 +  * ILE is similar but checks if R0 is less than or equal to 1.
 +<code assembly>
 +  ile R0, 1
 +</code>
 ====Instructions==== ====Instructions====
 Instructions are things that can be done to effect registers, memory addresses, and where you are in your code. There are many different types of instruction sets, for example: Instructions are things that can be done to effect registers, memory addresses, and where you are in your code. There are many different types of instruction sets, for example:
notes/comporg/spring2024/projects/cta0.1709163032.txt.gz · Last modified: 2024/02/28 23:30 by cmazzara