Basketball Game in MIT App Inventor | App Inventor Basketball Game | #appinventor #mitappinventor
Based on Obsidian Soft's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Create a visible-but-transparent basket sprite so collision events fire while the hoop area stays visually unobtrusive.
Briefing
A simple MIT App Inventor basketball game hinges on one trick: an “invisible” basket sprite that stays transparent enough to look like part of the court, yet remains detectable for collision events. The setup starts by creating a new project called “basketball,” centering the screen layout, and drawing a court background on a Canvas. Two ball sprites are added: a visible ball (radius 13, dark orange paint, 50 ms movement interval) and a second “basket” sprite (radius 10) placed over the hoop area. The basket sprite is kept visible for collision detection, but its paint color and opacity are adjusted so it’s effectively not seen.
Gameplay begins when the player flings on the screen. The ball’s speed is set to the fling speed multiplied by 7, and its direction (heading) is set to match the fling’s heading. A Clock component runs at a 200 ms interval, and it’s used to position the basket sprite after a short delay. That delay matters because initialization using screen properties (like screen height) can behave inconsistently on iPhone; moving the hoop positioning logic into the timer avoids misplacement.
When the timer fires, the basket’s X coordinate is calculated to center it horizontally on the Canvas (Canvas width divided by 2, adjusted by the basket radius). The Y coordinate is set to a fixed value (50 in the tutorial), with the note that it may need adjustment depending on the court image.
Scoring and ball resets are handled through a dedicated procedure called reset ball. On reset, the ball’s speed is set to zero so it stops drifting, then the ball is placed back near its starting position—centered horizontally and offset upward by subtracting 200 from the Canvas height. A global score variable is initialized to 0, and the score is redrawn on the Canvas whenever a successful collision occurs.
Collision logic ties everything together: when the basket sprite collides with the ball, the game resets the ball, clears the Canvas, increments the global score by 1, and draws updated text (“score: <value>”) centered on the Canvas. The ball is also managed at the edges: if it reaches the top or bottom edges, it resets using reset ball; if it hits the left or right edges, it bounces instead of resetting.
The result is a playable, physics-like flinging basketball mechanic built from App Inventor primitives—Canvas drawing, sprite collisions, edge detection, and a timer-based workaround for device-specific initialization quirks. It’s a compact blueprint for turning touch gestures into a scoring game without complex custom physics.
Cornell Notes
The game uses two sprites—one visible ball and one transparent “basket” sprite—to detect hoop hits while keeping the hoop area visually clean. A fling gesture sets the ball’s speed (fling speed × 7) and direction (fling heading). A Clock with a 200 ms delay positions the basket after startup to avoid iPhone issues with screen-property initialization. Scoring happens on basket–ball collision: the ball resets, the Canvas clears, the global score increments, and centered text is redrawn. Edge handling resets the ball on top/bottom hits and bounces it on left/right hits.
Why keep the basket sprite “visible” but transparent instead of fully invisible?
How does the fling gesture translate into ball movement?
What role does the Clock component play, and why not use Screen Initialize?
How is the basket positioned on the Canvas?
What does reset ball do, and when is it called?
How does scoring update on a successful shot?
Review Questions
- What specific properties are set from the fling event to control the ball’s speed and direction?
- Describe the difference in behavior when the ball hits the top/bottom edges versus the left/right edges.
- Why does the tutorial use a delayed timer to position the basket, and what problem does it aim to avoid on iPhone?
Key Points
- 1
Create a visible-but-transparent basket sprite so collision events fire while the hoop area stays visually unobtrusive.
- 2
Map touch flings to gameplay by setting ball speed to (fling speed × 7) and ball heading to the fling heading.
- 3
Use a Clock with a short delay (200 ms) to position sprites using screen properties, avoiding iPhone initialization quirks.
- 4
Center the basket horizontally using Canvas width ÷ 2 and adjust with the basket radius; set Y based on the court image layout.
- 5
Centralize ball reset logic in a reset ball procedure that stops motion (speed = 0) and repositions the ball to its starting point.
- 6
Increment a global score on basket–ball collision, clear the Canvas, and redraw centered score text each time.
- 7
Handle edges differently: reset on top/bottom hits, bounce on left/right hits to keep play continuous.