This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:comporg:spring2024:projects:cta0 [2024/02/28 13:41] – [CTA0] jmerri10 | notes:comporg:spring2024:projects:cta0 [2024/03/04 16:12] (current) – [Collision With Food] wgates1 | ||
---|---|---|---|
Line 52: | Line 52: | ||
</ | </ | ||
- | Looking | + | Looking |
+ | 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 | ||
+ | </ | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | 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 59: | Line 92: | ||
====Registers==== | ====Registers==== | ||
- | Vircon has 16 registers (0-15) with 14 and 15 being reserved for stack purposes | + | Vircon has 16 registers (0-15) with 14 and 15 being reserved for stack purposes. |
- | * 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. |
- | < | + | < |
mov R0, 1 | mov R0, 1 | ||
</ | </ | ||
- | * We can also modify the values in R0 | + | * We can also modify the values in R0. |
- | < | + | < |
iadd R0, 1 | iadd R0, 1 | ||
</ | </ | ||
- | * This will make R0 = 2 | + | * This will make R0 = 2. |
- | A register will also store a " | + | A register will also store a " |
- | < | + | < |
ige R0, 1 | ige R0, 1 | ||
</ | </ | ||
- | * 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 " | + | * But if you wanted to save the 2 in R0, before the " |
- | * ile is similar but checks if R0 is less than or equal to 1 | + | < |
- | < | + | |
mov R1, R0 | mov R1, R0 | ||
</ | </ | ||
- | * 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 | ||
+ | </ | ||
====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: | ||
+ | * The previously demonstrated " | ||
+ | * The also previously mentioned " | ||
+ | * The instruction " | ||
+ | * Most importantly there are the " |