LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

What do you think about this snake game code?

Solved!
Go to solution

Hello everyone,

 

In this quarintine I´ve developed a snake game code in Labview and I would like you to tell me what do you think about it?  How can I improve it?

 

Thank you

Wait for your reply

0 Kudos
Message 1 of 13
(3,755 Views)

How do you play it?

 

I felt the game was very slow to respond to my button presses.  Sometimes not responding at all.

0 Kudos
Message 2 of 13
(3,732 Views)

Hi alhebu,

 


@alhebu wrote:

How can I improve it? Wait for your reply


Why is there a 1000ms wait function in parallel to your event structure with a 100ms timeout event?

Your "tablero 2" arrays shows the result of the last iteration instead of showing the result of the current iteration…

Why do all your controls have a " 2" in their label?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 3 of 13
(3,688 Views)
Solution
Accepted by topic author alhebu

Great first effort! Congratulations! 😄

 

Now try to redo it with cleaner and less code. Some ideas:

 

  1. You don't need an event structure because the loop is spinning anyway and you can just poll the controls.
  2. Your inner loop sounds very dangerous, for example if all fields are green, it would never stop. (unlikely, but still something to keep in mind)
  3. Your random init coordinates are not fair because not all values occur a the same probability
  4. Try to operate on the board "in place". There is no need to delete and insert.
  5. Instead of buttons, try to read e.g. the cursor keys.
  6. Create some subVIs for simple operations. The big code on the right could be a subVI with cluster in, cluster out, and boolean out (stop?). Same for the init code.
  7. Make your cluster a typedef to make changes easier later, especially once you create subVIs.
  8. Don't stop the code at the end. Just wait for a reset to start over.
  9. ...

See ow far you get!

Message 4 of 13
(3,669 Views)

Thank you for the second line 😄

0 Kudos
Message 5 of 13
(3,657 Views)

Thank you for your reply! 😄

 

1.But if I would like to use the cursor keys I would use an event structure right?

2.Okay so I would have to write some code in order to get to a winning message if there´s no enough space for a red field right? Or why is it risky? Should I change this part?

3. I don´t understand... could you explain why not ocurr at the same probability?

4. Okay, I understand, LabView is slower if I insert and delete it many times right? Or just for do a cleaner code?

5. ...

6, 7, 8. I´ll do it thank you!

Thank you for taking some time to check my code and I would be grateful if you help me with my questions 😄

0 Kudos
Message 6 of 13
(3,652 Views)

Hi alhebu,

 


@alhebu wrote:

Thank you for your reply! 😄

 

1.But if I would like to use the cursor keys I would use an event structure right?

2.Okay so I would have to write some code in order to get to a winning message if there´s no enough space for a red field right? Or why is it risky? Should I change this part?

3. I don´t understand... could you explain why not ocurr at the same probability?

4. Okay, I understand, LabView is slower if I insert and delete it many times right? Or just for do a cleaner code?

5. ...

6, 7, 8. I´ll do it thank you!

Thank you for taking some time to check my code and I would be grateful if you help me with my questions 😄


1. Yes.

2. It's risky because your code may end up in a loop running forever: bad UX for the players…

3. Because the RandomNumber function outputs the numbers within a certain range. And there are certain rounding rules applied when converting from DBL to I32…

4. Cleaner code (most often) results in less buggy code: why do you even need to delete/insert when you can replace?

 

Ideas on "simpler code":

Why do you need 2 comparisons and an OR function to check the postion when there is a CoerceAndInRange function doing the same operation?

Why do you need to duplicate that check for the other coordinate? (To reuse code you either use subVIs or a loop…)

 

This is how my Snake game looks like:

It uses an event structure to catch key down events, like cursor keys…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 7 of 13
(3,647 Views)

@alhebu wrote:

 

3. I don´t understand... could you explain why not ocurr at the same probability?


Because you are rounding to the nearest index, so e.g. 0 occurs for values between 0 and 0.5, while 1 occurs for values between 0.5 and 1.5, a range twice as wide! Correct is to multiply by 11 and round down towards the next lowest integer. Now 0 occurs for values 0..1, 1 occurs for values 1..2, etc. All ranges have the same width! Note that the random number is never exactly 0 or 1, so you don't need to worry about rare edge effects.

 

Here's a quick demo. Note that since we are in 2D, edges get half and corners only a quarter with your code. You'll never get a job working for a Vegas Casino if your random numbers are biased. 😄

 

altenbach_0-1589218848586.png

 

Message 8 of 13
(3,605 Views)

Gerd already answered most points. Here's some additional info:

 

  • You don't need to use an event structure to read keys. You can also just read the keyboard. 😄
  • An event structure is actually problematic because events break the timeout wait and if you use external timing instead, events can accumulate in the event queue unless you take precautions.
  • Of course you should not call it a snake game, because it is not a snake, just a flea that tries to avoid corners and the red square. 😄 (A real snake game is one of the very early video games and forms a snake that grows in length)
  • I answered the probability issue in detail above.

 

Message 9 of 13
(3,597 Views)

Heeeeeyyyy,

 

I don´t know if have to open a new to topic because I already chose one answer as a solution and my question is litle bit different about what we were talking about...

 

I´ve added the things you told me to my code and now I have subvi´s and a type defeinition for my data cluster and my question is... How do I send my code to other person... I mean Is there anyway in wich I just send, I don´t know... just the Labview project and all the subvis goes with it? Do I have to send always my code and all the subvi´s?

0 Kudos
Message 10 of 13
(3,484 Views)