domingo, novembro 20, 2016

Android App: Whack the Minion!


(NB: Sorry. I haven't been able to upload this to the Google Play Store. This is what I got in reply: "After review, Whack the Minion, has been suspended and removed from Google Play as a policy strike because it violates the impersonation policy." Damn the minion. I should have chosen "google, the minion moron" instead...)




import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class Game extends JFrame{
private JPanel panel;
private JLabel[] holes = new JLabel[16];
private int[] board = new int[16];
private int score = 0;
private void pressedButton(int id){
int val = board[id];
//if val is 1 = mole
//if val is 0 = empty hole
if(val==1){
score++;
}else{ //val==0
score--;
}
lblScore.setText("Score: " + score); //update the score
clearBoard();
Whack A Minion

genRandMole();
}
private void initEvents(){
for(int i = 0; i < holes.length; i++){
holes[i].addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
JLabel lbl = (JLabel)e.getSource();
int id = Integer.parseInt(lbl.getName());
pressedButton(id);
}
});
}
}
private void initGUI(){
setTitle("Whack A Minion");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 608, 720);
JPanel contentPane = new JPanel();
contentPane = new JPanel();
contentPane.setBackground(new Color(0, 51, 0));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
JLabel lblTitle = new JLabel("Whack A Minion");
lblTitle.setForeground(new Color(153, 204, 0));
lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
lblTitle.setFont(new Font("Century Gothic", Font.BOLD, 20));
lblTitle.setBounds(0, 0, 602, 47);
contentPane.add(lblTitle);
panel = new JPanel();
panel.setBackground(new Color(0, 102, 0));
panel.setBounds(32, 105, 535, 546);
panel.setLayout(null);
contentPane.add(panel);
Whack A Minion

holes[0] = new JLabel("0");
holes[0].setName("0");
holes[0].setBounds(0, 396, 132, 132);
panel.add(holes[0]);
holes[1] = new JLabel("1");
holes[1].setName("1");
holes[1].setBounds(132, 396, 132, 132);
panel.add(holes[1]);
holes[2] = new JLabel("2");
holes[2].setName("2");
holes[2].setBounds(264, 396, 132, 132);
panel.add(holes[2]);
holes[3] = new JLabel("3");
holes[3].setName("3");
holes[3].setBounds(396, 396, 132, 132);
panel.add(holes[3]);
holes[4] = new JLabel("4");
holes[4].setName("4");
holes[4].setBounds(0, 264, 132, 132);
panel.add(holes[4]);
holes[5] = new JLabel("5");
holes[5].setName("5");
holes[5].setBounds(132, 264, 132, 132);
panel.add(holes[5]);
holes[6] = new JLabel("6");
holes[6].setName("6");
holes[6].setBounds(264, 264, 132, 132);
panel.add(holes[6]);
holes[7] = new JLabel("7");
holes[7].setName("7");
holes[7].setBounds(396, 264, 132, 132);
panel.add(holes[7]);
Whack A Minion

holes[8] = new JLabel("8");
holes[8].setName("8");
holes[8].setBounds(0, 132, 132, 132);
panel.add(holes[8]);
holes[9] = new JLabel("9");
holes[9].setName("9");
holes[9].setBounds(132, 132, 132, 132);
panel.add(holes[9]);
holes[10] = new JLabel("10");
holes[10].setName("10");
holes[10].setBounds(264, 132, 132, 132);
panel.add(holes[10]);
holes[11] = new JLabel("11");
holes[11].setName("11");
holes[11].setBounds(396, 132, 132, 132);
panel.add(holes[11]);
holes[12] = new JLabel("12");
holes[12].setName("12");
holes[12].setBounds(0, 0, 132, 132);
panel.add(holes[12]);
holes[13] = new JLabel("13");
holes[13].setName("13");
holes[13].setBounds(132, 0, 132, 132);
panel.add(holes[13]);
holes[14] = new JLabel("14");
holes[14].setName("14");
holes[14].setBounds(264, 0, 132, 132);
panel.add(holes[14]);
holes[15] = new JLabel("15");
holes[15].setName("15");
holes[15].setBounds(396, 0, 132, 132);
panel.add(holes[15]);
Whack A Minion

setContentPane(contentPane);
}
private void clearBoard(){
for(int i = 0; i < 16; i++){
holes[i].setIcon(loadImage("/moleIn.png"));
board[i] = 0;
}
}
private void genRandMole(){
Random rnd = new Random(System.currentTimeMillis()); //seeding random int moleID = rnd.nextInt(16);
board[moleID] = 1;
holes[moleID].setIcon(loadImage("/moleOut.png"));
}
private ImageIcon loadImage(String path){
Image image = new ImageIcon(this.getClass().getResource(path)).getImage();
Image scaledImage = image.getScaledInstance(132, 132, java.awt.Image.return new ImageIcon(scaledImage);
}
public Game() {
initGUI();
clearBoard();
initEvents();
}
public static void main(String[] args) {
Game frame = new Game();
frame.setVisible(true);
}
}

NB: I didn't bother correcting the code indentations. Beware of attempting an implementation without that. On top of that, the above code implementation does not follow to the letter the app on the Google Play Store, meaning, the app on the google play store is not the latest version. I still have to update the app with the above-mentioned source code. One of these days...Or probably never...So much to do, so little time to do it...

NB2: App developed with Matilde's help, for our baby boy Manuel Maria. May he spend many happy hours whacking the minion...

Sem comentários: