Ever wanted to be LeBron James for 30 seconds? Well, now you can! For my interactive media midterm, I created a LeBron James simulator, a humorous experience designed to help you unleash yout inner LeBron. Using computer vision and ml5.js, I made a virtual basketball track the player’s index finger to control the game instead of the usual keyboard and mouse. The aim of the game is for the player to shoot as many hoops as they can in 30 seconds while LeBron-inspired encouragements flash across the screen. I gave the game a gold and purple color scheme to match the Los Angeles Lakers. For background music i used the instrumental of Pat Benatar’s Hit Me with Your Best Shot which adds that classic arcade feel.
The game was built using p5.js and the ml5.js HandPose model. When the player starts the game, their webcam is activated to begin tracking their hand. The game tracks the player’s index finger, which updates the basketball’s coordinates on the canvas. The player scores points by moving the ball into a hoop on the right side of the screen. The scoring system uses the dist() function to measure the distance between the ball and the hoop, while a Boolean variable called wasInsideHoop prevents the player from continuously scoring points by keeping the ball inside the hoop. I also used a basketball class to store the ball’s properties, such as its position and size, while methods update its location and draw it on the canvas.
The game uses three different states: the main menu, gameplay, and game over. Pressing Space starts the game, giving the player 30 seconds to score as many points as possible. Their final score determines which LeBron-inspired message they receive on the ending screen. Pressing R after the game ends takes the player back to the main menu without restarting the entire sketch. I also included a fullscreen mode that can be activated by pressing F.
One part that I am particularly proud of is the different ending quotes displayed depending on the player’s score. I used conditional statements to determine which message appears based on how well the player performed. I wanted these messages to add humor to the experience and make the game feel more entertaining, even when the player doesn’t score many points.
hat I struggled with most during development was getting the hand tracking to work. p5.js had recently received an update that removed the preload() function, which I had initially tried to use to load the HandPose model. This caused some issues with my original code, and I eventually had to use an asynchronous setup() function to initialize the model instead.
I also ran into some problems with sound, although these were unrelated to p5.js itself. I only wanted to use around 30 seconds of the song, but when I tried trimming the audio in QuickTime Player, it exported the file in .mov format instead of MP3. I ended up using GarageBand to trim and export the song, although its interface was more confusing to navigate. After some trial and error, I managed to get the music working in the game.
For improvements, i’d say that i want to add more challenges in the future. A defense mode would be fun. i would also maybe like to make it a full body tracking gamw instead of just hand tracking. The game could serve as an exercise outlet by needing the player to get up and move their whole body.
References:
P5.Js
ML5.JS handpose library
Music; Hit Me with Your Best Shot instrumental. Originally by Pat Benatar https://youtu.be/Dxb3HZPb5-c?si=3cGfPgRrMRDmHqPB
Image: Lebron James
https://www.pinterest.com/pin/lebron-james-standing-png-image–552957660533717293/
Developmental assistance: AI-assisted troubleshooting and code development, with testing and modifications during implementation




