0% found this document useful (0 votes)
314 views2 pages

Whack-a-Mole App

The document provides instructions for creating a Whack-a-Mole app using timers, animations, and click listeners in Android Studio. It includes directions to add image files of moles or other objects to click, design the user interface with an image view and text view, code a timer to randomly reposition the image every second, and make it so clicking the image increases the user's score tracked in the text view. It also provides a cheat sheet on basic animations and using a timer handler.

Uploaded by

Kelly Lougheed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
314 views2 pages

Whack-a-Mole App

The document provides instructions for creating a Whack-a-Mole app using timers, animations, and click listeners in Android Studio. It includes directions to add image files of moles or other objects to click, design the user interface with an image view and text view, code a timer to randomly reposition the image every second, and make it so clicking the image increases the user's score tracked in the text view. It also provides a cheat sheet on basic animations and using a timer handler.

Uploaded by

Kelly Lougheed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Whack-a-Mole App 

Directions​: Use timers and animation to make a ​Whack-a-Mole​ app! 


 
Mole​. Find a picture of a mole, another animal, or something else whack-able. 
● Save the pictures to your desktop with file names consisting of the characters a-z 
(lowercase) and/or 0-9 
● Put them in the drawable folder located at the following path:  
app → src → main → res → drawable 
 
Design​. In activity_main.xml… 
● Create an ImageView element with the mole picture and give it an ID 
● Create a TextView element to show the user’s score and give it an ID 
 
Code​. In MainActivity.java: 
● Store both UI elements as variables, e.g. 
○ Outside onCreate:  ImageView mole;
○ Inside onCreate:  mole ​=​ (ImageView) findViewById(R​.​id​.​mole);
● Use the timer and animations (see cheat sheet on next page) to randomly reposition 
the mole every second (or however often you see fit) 
● Make a click event listener that gives the user a point whenever they click on the 
mole (thus whacking it) 
mole​.​setOnClickListener(​new​ View.OnClickListener() {
​@Override
​public​ ​void​ ​onClick​(View ​view​) {
// Your code here!
}
});

 
 
Animation Cheat Sheet 
Basic formula for an animation​: 
elementName.animate().animation(arg).setDuration(milliseconds);

Animations​: 

alpha(int a) Sets the transparency of an image. 


0=transparent, 1=opaque 

translationX(int pt), Moves the image from its original location to a 


translationY(int pt) new x or y point 

translationXBy(int dps), Moves the image from its current location on 
translationYBy(int dps) the x- or y-axis by a certain amount 
expressed in dps (​dp = 1 pixel​) 

Timer Guide 
The timer code goes inside onCreate.
Handler handler = new Handler();
Runnable run = new Runnable() {
@Override
public void run() {
// This code will run every x milliseconds
handler.postDelayed(this, x);
}
};
handler.post(run);

Additional Features​: 
● Make the mole disappear when the user clicks it and then reappear in a new place 
● Make the mole start out moving slowly and then have its speed increase when the 
user earns certain scores (e.g. 10 points, 20 points, etc). 
● Give the user a 60-second timer limit and shut them out of the game after that. 
● What else can you think of? 

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy