Pong for Mindstorms EV3 is a one player game code using LabView EV3 language that will help you understand the usage of variables, ranges, conditions and multithreading. You just need one LEGO Ev3 Brick, a L-Motor and a big gear to have hours of enjoyment ( but I would better go and buy an Xbox One )
In my way to improve my EV3 coding skills I am doing a series of tests of old classics, like the bouncing ball or this little Pong game.
Pong was a game for two players were each of them has a racket that can make a ball bounce on the playfield. The player that was able to pass a ball behind the player racket scores a goal.
I have coded this 70s classic in EV3 but for a single player. As usual, source code included ( and released as GPL Open Source ).
Here is the video of the game and then I explain you how it was done and the tricky parts on code.
So let’s see the design before talking about code.
I will design the code to be running on three different threads
Thread Score
forever {
score = score + 1
wait 1 second
}
This thread keeps counting the time, that will be our score.
Thread Player
rotations = 0
forever {
new = read motor rotations
if new > rotations then
playery = playery + 1
if new < rotation then
playery = playery - 1
}
The Player thread will read the motor rotations and transform it on vertical movements of the player.
And now to understand the next pseudocode I would like to explain a few things. We have two entities in our game:
A ball that has and x,y position and a speed that it is represented by a vector dx,dy. Our ball has also a radius of 5.
A player that has a vertical position y. Our player don't move on the horizontal axis so we don't need to store that value. I have made it fixed at 170 pixels so it is on the end of the screen. The player's racket has a length of 20 pixels.
Thread Game loop
bx, by = 0
dx, dy = 1
playery = 0
forever {
bx = bx + dx
if bx is not in range [0,160] then
dx = dx * -1
if bx >= 160 and bx <= 165 then
if by >= playery and by <=playery+20 then
dx = dx * -1
if bx > 175
end loop
by = by + dy
if by is not in range [0,118] then
dy = dy * -1
draw score
draw player
draw ball
}
print game score
play Game over sound
wait for key
Ok, still there? The only tricky part here is the part about colliding with the player. So what means that the ball collide with the player? It means that the ball area intersect the player area. And we want to make sure that we hit the ball with only the front side of the racket ... so if the ball has a size of 10 and player is on x=170 then we check if the position of the ball is between 160 and 165.
Why? 170 - 10 = 160... but if our ball speed isn't 1 ( and it isn't ) then we may have the ball somewhere between 160 and 160 + dx. So we want to make sure that we don't miss the collision by checking between 160 and 165.
Then if the ball y is on the same range that the player y to player y + size, that was 20, then we conclude that the ball did in fact collided with the racket and make it bounce.
Well that's pretty much the idea. Just one last tip. Unless you want to kill your EV3 Brick batteries in half and hour you better let the program sleep a bit on the end of each turn... just 70 to 100 milliseconds is enough.
So here is the EV3 code.
This graphical language is nice because it let you hack something for your robot quite fast but it is a pain when you have to use variables.
Download EV3 Pong Source Code
Here you have the file ready for your EV3 brick. Make sure you run the whole program and not just a thread.
[sociallocker]
EV3 Source Code file
[/sociallocker]
Stand up to the Challenge!
Can you turn this to a two players game? You just need to add a few things, an extra L-motor, the code to read the motor, just another thread working on a different port and maybe the hardest part ( but not hard at all ) the code to check the bounces on the left player's racket.
Now I just need to learn about arrays, files and blue-tooth communication. Keep your eyes peeled for next articles.
I’ve programmed a bluetooth version, you can check it out here,
http://www.youtube.com/watch?v=h2I85ZchVGo
could you set up a download link for your program please?
Great, had the same idea a few days ago as my son got an ev3 for xmas. As a professional software developer I had a few problems with the graphical programming (normally using python and c/c++), but still finished a pong game for two players.
Hahaha, yeah, the software is great for kids to prototype anything… but developing anything more serious than Pong is a pain on the ass… go and have a look to leJOS if you can write Java… it really worth the effort.
The EV3 code is toooooooooooooooooooooooooooooooooooooooooooooooo small! Can you send me a bigger one?
Great! Softwares are very advanced.
Can you get a bigger program? I love the idea and figured out how to build it. It was a great idea except the program wasn’t big enough to read. I tried to put in the program but it didn’t work, I need a bigger one!!!!!!!
I can’t download it for some reason. :(
I can’t open the ev3 code it’s not .ev3 it’s .ev3p can you help me please?
The ev3 code written is very blurry when zoomed in. Please allow a ev3 download program link.
can you send me the file of the code I don’t have a twitter or faceebook
I’m not able to get the source code. Can you post it somewhere?
Great Job! My kid looooooooooooooove it so much