LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Pong template

Hi All,

 

I'm getting back into LabVIEW and decided to recreate the classic game of pong. Yes, I can see there are a few examples knocking around online but I wanted to give this a good go my self first.

 

I have attached my attempt so far which shows how I Intend to tackle this in LabVIEW. Before I continue and add the 'logic' (ball direction/ paddle interaction etc).. I was hoping to get some feedback on my general approach to this. It's a simple producer/ consumer setup to handle the user input (paddle left/right up and down) and a cluster to handle the 'game data'. But so far it seems a bit... lumpy ?

 

Any advice would be appreciated,

 

Thanks,

 

🙂

0 Kudos
Message 1 of 6
(1,727 Views)

Code looks pretty clean, but there are a few things that could be simplified.

 

  • You don't need the timeout event if you would place a timeout on the dequeue and execute the ball case when it times out.
  • It is sufficient to enqueue the key press only if it is one of the four useful values. You can put the enqueue into a case structure with an empty case as default and the four numbers in the other case.
  • In the consumer loop, you can combine all paddle events into one. They are all identical! Wire the "timed out?" to the case structure and make one case ball and the other paddle.
  • Depending on how you want the user to interact, also add a "key repeat" to the "key down" event.
  • "Main" is not a good name for a VI. Too generic.
  • Organize the project so you only have the toplevel VI visible. Create virtual folders for the subVIs and another one for the typedefs/controls. (at least!)
  • You probably need to work on implementing more stable update timing. Your loop rate increases when buttons are pressed because they break the timeout. Maybe the ball should be handled in an independent loop. Currently, your ball does not update during an iteration where the paddle is moved.
  • Your speed for x and y are independent, meaning it covers more distance per update moving diagonally. You are also only quantized to 6 directions. Maybe you want to carry the ball position as a complex number and increment by adding a complex value with fixed r and variable angle to have an unlimited number of directions.
  • You don't need to wire the error and queue to the left edge of the consumer loop.
  • You can eliminate the channel if you would stop the consumer on queue error (wire the error to the termination condition). This way the consumer loop will stop automatically when the producer has completed and the queue terminated. (note that in more complicated scenarios this might not be appropriate).
  • You probably want to keep all states (ball and paddle positions, etc) in an action engine and have a single subVI that creates the image based on it. You currently create different images depending on what got updated last.
  • It is sufficient to draw the new paddle and ball positions. Set the picture to "erase first".

I am sure there are more things to add. Maybe others want to chime in. 😉

Message 2 of 6
(1,716 Views)

Also remember the in place element structure:

 

altenbach_0-1616265795536.png

 

Message 3 of 6
(1,679 Views)

OK, I made a quick demo during Sunday breakfast demonstrating the action engine idea. Note that the board updates at regular intervals. No need for queues, channels and all that.

 

Just to give you some general ideas. Most of the ball logic is still missing.

 

 

altenbach_0-1616350520772.png

 

Message 4 of 6
(1,629 Views)

Thanks very much for the great information 😃

 

That looks brilliant.

 

I'll implement your suggestions and complete the game/ update this thread with the result...

 

Thanks again/

 

 

0 Kudos
Message 5 of 6
(1,622 Views)

Attached is my latest attempt.

 

Overall I'm pleased with the result. The ball logic isn't the best and the the general scalability of the program could be improved. But I'm happy with the outcome as a 'REV1' 😉

 

I want to try and shift this to an OOP approach. What would be the best way to implement this ?

 

Any tips welcome 🙂

 

Thanks again.

0 Kudos
Message 6 of 6
(1,497 Views)