Creating an Object Oriented Snake Game in LabVIEW

Поделиться
HTML-код
  • Опубликовано: 19 сен 2024

Комментарии • 47

  • @MortezaVafadar
    @MortezaVafadar 4 года назад +6

    Thank you Tom! I am learning to code like a professional! Realy appreciate it.

  • @panda192012
    @panda192012 4 года назад +1

    This has become my favorite channel. Thank you Tom!

  • @drwagner14
    @drwagner14 4 года назад +2

    Tom, this is a really great tutorial. I learned a lot! Next I would love to learn more about class method Access Scope. All methods in this example are public. When and why would you use private and protected scope?
    I made some notes while working through the example:
    1. I noticed that the key down event as written will always indicate up (the default case) for any other key pressed. I changed this to have a default case for all other key presses, and only send the queue element (or set the global direction value) for the appropriate keys.
    2. I appreciate the divide by two bit-shift trick for placing the snake on the board, but I feel like this obscures the basic math. To me this seems most appropriate for situations where performance must be optimized. I made the code a little cleaner by doing the divide (and other math steps) on the grid-size cluster before breaking out the x and y elements.
    3. In the tail strike code you can also search the tail array for the new head position using Search 1-D array. This may make the logic easier to follow.
    4. I didn't like the way "make tail longer" worked, as the length only increases once the tail passes all the way through where the fruit was. A more immediate approach is to reinstate the deleted portion of the tail when the fruit is captured. To do this I added a "Deleted Portion Coordinate" to the Snake class. When a fruit is caught, I append this to the tail array, and recolor the pixel. The other possibility is to detect the fruit capture before redrawing the snake, and then not removing the last element of the snake array.
    5. To answer your challenge question, I made an adjustment to the Move Forward VI to allow the snake to cross from one edge of the board to the other. When moving right or down, you can use the quotient and remainder function to get the offset of the head coordinate from (grid size - 1). But when moving left or up, I found the easiest way to handle the math is to allow the head coordinate to become negative (requires changing the coordinate type definition to I32), in which case the corresponding negative value is replaced by (grid size -1). You have to handle the X coordinate when moving up and down, and the y coordinate when moving left and right. Is there an easier way?
    The board failure routine remains unchanged.
    It would be nice to include this as an option in the game setup. You could also color the edge of the board to indicate if it can be crossed or not.
    6. I added a separate head color to the snake, which I think makes it easier to understand the snake's movements. The required a new head color class element and updating three pixels instead of two when moving the snake. Fun!
    7. For the snake direction detection, I replaced your single element queue with the native global variable, which makes the code much cleaner, and is functionally identical. It occurred to me that the way we read the direction from the UI should not be a part of the class method, so I moved this out of the Read Direction VI and renamed this "Set Direction".

    • @TomsLabVIEWAdventure
      @TomsLabVIEWAdventure  4 года назад +1

      Hey Russell,
      I'm glad you learned a lot and I'm also glad you've been able to challenge me! It shows you've been able to exercise your own knowledge and strengthen it :)
      Regarding access scope, it's very important to set appropriate scopes for your methods. Scope protects your code from developers (yourself included) from taking shortcuts that will later come to bite you in the butt!
      Generally speaking, I agree with all of your points:
      *Access scope: I thought about going through the access scope when recording. However, knowing that it was going to be a long video I decided not to set scopes. Perhaps I could make an Access Scopes tutorial and add it to the OOP playlist I have.
      *For points 1 and 4 - I realized this as I was doing it but because I didn't pre-plan this video; you're seeing the 'raw design choices' which I knew I should do differently [but I got a bit lazy in editing ;) ]. You're right to call me out on those!
      *Point 2: For this application, using the bitshift made no noticeable difference in performance. However, because dividing (including quotient and remainder) is incredibly processor-heavy, I've been trying to make sure I get into good habits - even when it's not necessary. I do a lot of embedded work where things like this make a big difference.
      *Point 3: I can't remember the exact way I did it, but someone else called me out on this in another video... you got me there! ;) Kudos.
      *Point 5: When I asked the question I was thinking about a dynamic dispatch override of Move Forward so you appending functionality instead of changing functionality - subtle but important difference. One thing I should have done in the video was to add the array size to the class private data - this would have made the code a lot cleaner!
      One thought that just popped into my head [this is not going to be a straightforward or easy way... (kinda interesting though)] Have you ever used fixed-point numbers? (where you can set word length and integer word length - so where we might have an i32, we could make a fixed-point number of an "i4") The point is... we could take advantage of number rollover (and with a custom number representation, we can control where that is)... sooooo if the head is at "pixel 15" and stepping into "pixel 16" it actually rolls over to "pixel 0". That could be one "thinking outside the box" approach - sorry, that was just me rambling (and not a good way at all...).
      I like your approach, it's straight forward and makes sense.
      *Point 6: Nice :)
      *Point 7: Errhrhhhhhh When I was editing the video I was thinking "WHY TOM WHY?!". SEQs solve a lot of problems and I wanted to show what an SEQ is, as I like to share concepts (much like bit-shifting instead of /2). However, this wasn't a good case-study for them. I mentioned in the edit how I made a complicated global :/
      Again, thanks for taking the time to give feedback, I want to do a similar video using LabVIEW NXG. When I do that, I'll make sure I plan it out and take this feedback into consideration :)
      Cheers!
      Tom 🤓👩‍💻😊

    • @TomsLabVIEWAdventure
      @TomsLabVIEWAdventure  4 года назад

      I just scanned through the video, are you referring to the code shown at 39:35 for point 3? You're absolutely right! I should have seen that!

    • @drwagner14
      @drwagner14 4 года назад

      @@TomsLabVIEWAdventure Yes, that's the spot! I do understand that we're watching you make decisions on the fly, and to me is fascinating to see how other people code. There's no wrong way! (Well, sortof).

    • @drwagner14
      @drwagner14 4 года назад

      @@TomsLabVIEWAdventure Thank you for the idea about fixed point numbers, very interesting. You'd be restricted to powers of two, but it makes the math very easy!
      Also, I am grateful for the idea to add a dynamic dispatch class for the snake wrap. It makes a lot of sense, once you have tested code, to extend it this way. I'm not in the habit of thinking this way yet.

  • @yvindmagnussen5479
    @yvindmagnussen5479 4 года назад +1

    Nice tutorial! I've implemented a snake game as an Easter egg in one of my programs. I will definitely replace it with this and expand the features. Thanks!

  • @santhoshkumar405
    @santhoshkumar405 4 года назад +1

    Tom you were incredible 💯💯
    Absolutely genius in LabVIEW❤️

    • @TomsLabVIEWAdventure
      @TomsLabVIEWAdventure  4 года назад +1

      Thanks! Although there are lots of things I would have done differently if I did it again ;)

  • @mlfbsbnmfirnbdml
    @mlfbsbnmfirnbdml 2 года назад

    Thank you for this Tom,it is the first time for me to see the use of lossy enqueue and queue preview, they are quite useful to learn.

  • @sankarmuthu4069
    @sankarmuthu4069 4 года назад +1

    Hi Tom, thanks for such an informative tutorial. As u mentioned I'm looking forward with unit test framework.

  • @aliasad573
    @aliasad573 4 года назад +3

    a video on unit testing would be awesome.

  • @MichaelStiffler-du5eb
    @MichaelStiffler-du5eb Год назад +3

    Hey Tom, I had a couple questions about editing the code you wrote to use a different form of input to move the snake. If you could reply with any help that would be much appreciated.

  • @osxdjr
    @osxdjr 4 года назад

    Such a nice video, keep going with the great work!

  • @TheWildCamper
    @TheWildCamper 3 года назад

    You could sell your courses on Udemy. I was recommended your course by our Technical Director. Very good, please make more!

  • @joe95joe-qy6hl
    @joe95joe-qy6hl 8 месяцев назад

    Really nice video. Do you have any videos of the same with manual creation of classes and methods(instead of using the oop tool)?

  • @hahuyetannhien4210
    @hahuyetannhien4210 Год назад +1

    how to control the snake? I can't

  • @shrinivasprabhurm
    @shrinivasprabhurm 4 года назад

    Recently stumbled across your channel . Great work :) . Wish I had the chance to make it to Munich this year to meet you.

    • @TomsLabVIEWAdventure
      @TomsLabVIEWAdventure  4 года назад +1

      In 2020, I plan on being at GDevCon#3, CLA Summit and NIDays Europe. Hope to see you at one of those :)

  • @21grams5v3n
    @21grams5v3n 4 года назад +1

    Thank you Tom for another great video! Could you do a tutorial on MAL in the context of AF for distributed systems?

    • @TomsLabVIEWAdventure
      @TomsLabVIEWAdventure  4 года назад

      So there are two things there, MALs and communicating between actors over a network. Both of which have nice solutions, I could make this into two videos. Is there a specific scenario you had in mind? If so, that would make a nice "real life" example for the video.

    • @21grams5v3n
      @21grams5v3n 4 года назад +1

      @@TomsLabVIEWAdventure A general real- life example would be running DAQ tasks on multiple cRIOs or PCs from single "controller" over the network

  • @AshishKumar-qe4hq
    @AshishKumar-qe4hq 2 года назад +1

    Unit testing video?

  • @tonyspies3648
    @tonyspies3648 4 года назад +1

    Thanks for the video! No matter what I try, I'm unable to get the UML modeller to generate the full code. The most luck I've had is when I remove the type def and the enum. Any suggestions you can give would be appreciated.

  • @yayilovesquares
    @yayilovesquares 4 года назад

    what are the chances that the day I search for oop examples in labview our boy makes a dope ass video?

  • @davidferster2263
    @davidferster2263 3 года назад

    Great video! Very helpful in my efforts to learn OO LabView.
    Why do you have two separate loops in Main? Why not put all the code of the top loop into the Timeout case of your Event Loop (with the appropriate check for elapsed time)? Then the current direction could go into a shift register and the Queue would not be necessary?

    • @TomsLabVIEWAdventure
      @TomsLabVIEWAdventure  3 года назад +2

      Hey David,
      Great question! To be honest, the real answer is: I should have thought about how I was going to write the code before recording it.
      In hindsight, a straightforward Event Driven State Machine would have been the correct choice - so I'll have to hold my hands up to that.
      I wouldn't put all the code in the timeout case, but I would have the timeout case trigger a 'progression' of the snake (in a separate state (of the case structure)). Like wise, a key down event, would progress the state machine into a 'change direction state'.
      Besides that, I'm glad you enjoyed the video!

  • @ramazancanbay268
    @ramazancanbay268 2 года назад

    Thats amazing! thank you :)

  • @theadvancceepic666
    @theadvancceepic666 2 года назад

    nice video bro

  • @mohamednadrani3585
    @mohamednadrani3585 2 года назад

    Thank you

  • @zlmsailor
    @zlmsailor 4 года назад

    Awesome!

  • @hpz4053
    @hpz4053 4 года назад

    我不怎么会英文,不过不影响我看你的视频,感谢分享这么多优秀的labview视频,谢谢

  • @vaibhavmistry5795
    @vaibhavmistry5795 3 года назад

    Cool man 😁

  • @MortezaVafadar
    @MortezaVafadar 2 года назад

    27:24 Shouldn't it be "Add Fruit Object" instead of "Add Fruit Class"?

    • @TomsLabVIEWAdventure
      @TomsLabVIEWAdventure  2 года назад

      Yep, you are correct. I misspoke.

    • @MortezaVafadar
      @MortezaVafadar 2 года назад

      @@TomsLabVIEWAdventure I mean the text on the icons of "+Fruit Class" and "+Snake Class" themselves. I saw similar naming in others' work also. It is confusing for those who learn oop concepts for the first time.

  • @MortezaVafadar
    @MortezaVafadar 4 года назад

    You deserve a professional microphone for recording, please!

    • @TomsLabVIEWAdventure
      @TomsLabVIEWAdventure  4 года назад +1

      Thank you for your feedback, I have actually purchased a good quality microphone, but at the moment I am just using the default settings and I need to learn how to set it up properly. This is really high on my priorities for this channel, so hopefully the next video will sound much better :)

    • @MortezaVafadar
      @MortezaVafadar 4 года назад

      @@TomsLabVIEWAdventureHope you set it up soon! The overall loudness is low in your videos which makes me increase the volume often. And with some noise cancellation, it would be perfect.

    • @TomsLabVIEWAdventure
      @TomsLabVIEWAdventure  4 года назад +1

      I'll try out some different software packages and hopefully you'll hear a difference soon!

  • @rorosyria146
    @rorosyria146 4 года назад

    يا ريت تعمل ترجمة بالعربي