Android Apps Development for Mobile Game Lesson Create a simple paint brush that allows user to draw something (Page 11 13)

Similar documents
package import import import import import import import public class extends public void super new this class extends public super public void new

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

public AnimLayer(Context context, AttributeSet attrs, int defstyle) { super(context, attrs, defstyle); initlayer(); }

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 4. Workshop

Workshop. 1. Create a simple Intent (Page 1 2) Launch a Camera for Photo Taking

Android Apps Development for Mobile Game Lesson 5

Custom Views in Android Tutorial

INTRODUCTION TO ANDROID

1. Defining Procedures and Reusing Blocks

Programming of Mobile Services, Spring 2012

Graphics. Android - graphics 1/9

GATAV 5. Animations Using a Game Loop

1. Simple List. 1.1 Simple List using simple_list_item_1

1. Location Services. 1.1 GPS Location. 1. Create the Android application with the following attributes. Application Name: MyLocation

MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

Q.1 Explain the dialog and also explain the Demonstrate working dialog in android.

Create Parent Activity and pass its information to Child Activity using Intents.

Android Application Development. By : Shibaji Debnath

Mobile Image Processing

1 카메라 1.1 제어절차 1.2 관련주요메서드 1.3 제작철차 서피스뷰를생성하고이를제어하는서피스홀더객체를참조해야함. 매니페스트에퍼미션을지정해야한다.

Mobile Application (Design and) Development

Android Programs Day 5

Intents. Your first app assignment


Mobile Image Processing

Android/Java Lightning Tutorial JULY 30, 2018

Contents at a Glance. iii.

SMAATSDK. NFC MODULE ON ANDROID REQUIREMENTS AND DOCUMENTATION RELEASE v1.0

Introduction to Android Development

Accelerating Information Technology Innovation

android:layout_margintop="5dp" > <EditText android:layout_width="210dp"

Group B: Assignment No 8. Title of Assignment: To verify the operating system name and version of Mobile devices.

Tabel mysql. Kode di PHP. Config.php. Service.php

Mobile Application Development Lab [] Simple Android Application for Native Calculator. To develop a Simple Android Application for Native Calculator.

Graphics. Lecture 16

<uses-permission android:name="android.permission.internet"/>

Android Development Tutorial

Coding Menggunakan software Eclipse: Mainactivity.java (coding untuk tampilan login): package com.bella.pengontrol_otomatis;

CSE 660 Lab 3 Khoi Pham Thanh Ho April 19 th, 2015

ANDROID (4) 2D Graphics and Animation, Handling Screen Rotation. Marek Piasecki

Press project on the left toolbar if it doesn t show an overview of the app yet.

CS 4518 Mobile and Ubiquitous Computing Lecture 3: Android UI Design in XML + Examples. Emmanuel Agu

Building MyFirstApp Android Application Step by Step. Sang Shin Learn with Passion!

Android Workshop: Model View Controller ( MVC):

Practical 1.ListView example

ITU- FAO- DOA- TRCSL. Training on. Innovation & Application Development for E- Agriculture. Shared Preferences

CS260 Intro to Java & Android 09.AndroidAdvUI (Part I)

Our First Android Application

MAD ASSIGNMENT NO 3. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

Database Development In Android Applications

Applied Cognitive Computing Fall 2016 Android Application + IBM Bluemix (Cloudant NoSQL DB)

Basic GUI elements - exercises

MyDatabaseHelper. public static final String TABLE_NAME = "tbl_bio";

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Topics Related. SensorManager & Sensor SensorEvent & SensorEventListener Filtering Sensor Values Example applications

Kotlin for wearables: use case. Andrey Mukamolov

App Development for Smart Devices. Lec #7: Audio, Video & Telephony Try It Out

Tip Calculator. xmlns:tools=" android:layout_width="match_parent"

An Android Studio SQLite Database Tutorial

CMSC434. Introduction to Human-Computer Interaction. Week 10 Lecture 17 Mar 31, 2016 Engineering Interfaces III. Jon

What have we learnt last week? Wireless Online Game Development for Mobile Device. Introduction to Thread. What is Thread?

Meniu. Create a project:

@Bind(R.id.input_ ) EditText EditText Button _loginbutton;

Time Picker trong Android

TextView Control. EditText Control. TextView Attributes. android:id - This is the ID which uniquely identifies the control.

Androidプログラミング 2 回目 迫紀徳

Android Tutorial: Part 3

Operator s Manual. minibht Audiometry Android App. Team 2. Samir Dahmani, Nihit Mody, Joseph Wolanski. Client: Dr. Oliver

Teaching Kids to Program. Lesson Plan: Catch the Ball

API Guide for Gesture Recognition Engine. Version 2.0

MotionEvents Touch Handling Gestures

CS 4518 Mobile and Ubiquitous Computing Lecture 4: WebView (Part 2) Emmanuel Agu

CSE 660 Lab 7. Submitted by: Arumugam Thendramil Pavai. 1)Simple Remote Calculator. Server is created using ServerSocket class of java. Server.

Arrays of Buttons. Inside Android

API Guide for Gesture Recognition Engine. Version 1.1

ANDROID PROGRAMS DAY 3

Introduction to Scratch Programming v1.4 (Second Ed) Lesson 6 Calculator

Android HelloWorld - Example. Tushar B. Kute,

Android Coding. Dr. J.P.E. Hodgson. August 23, Dr. J.P.E. Hodgson () Android Coding August 23, / 27

Programming Mobile Applications with Android Lab2

Create a local SQL database hosting a CUSTOMER table. Each customer includes [id, name, phone]. Do the work inside Threads and Asynctasks.

Kotlin for Android developers

Vienos veiklos būsena. Theory

Google SketchUp Design Exercise 1

Lesson 3 Creating and Using Graphics

OPTIMIZING ANDROID UI PRO TIPS FOR CREATING SMOOTH AND RESPONSIVE APPS

PENGEMBANGAN APLIKASI PERANGKAT BERGERAK (MOBILE)

Android writing files to the external storage device

B9: Việc cuối cùng cần làm là viết lại Activity. Tới Example.java và chỉnh sửa theo nội dung sau: Mã: package at.exam;

App Development for Smart Devices. Lec #18: Advanced Topics

Visual C# Program: Simple Game 3

Introduction to Mobile Application Development Using Android Week Three Video Lectures

Table of Content. CLOUDCHERRY Android SDK Manual

LifeStreet Media Android Publisher SDK Integration Guide

Mobile Programming Practice Background processing AsynTask Service Broadcast receiver Lab #5

Dynamically Create Admob Banner and Interstitial Ads

Android CardView Tutorial

App Development for Smart Devices. Lec #8: Android Sensors

Java Basics A Simple Hit the Zombies Game

StoppUhr. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start1"

Transcription:

Workshop 1. Create a simple Canvas view with simple drawing (Page 1 5) Hide Action Bar and Status Bar Create Canvas View Create Simple Drawing 2. Create a simple animation (Page 6 10) Load a simple image Create a simple animation 3. Create a simple paint brush that allows user to draw something (Page 11 13) 4. Exercise: Now it s time for you to develop a simple game. The hero needs to walk along a long path. The score will be increased once the hero gets a coin. However, one life will be deduced when it drops to the hole. The game will be over when all life are lost. User can press the screen to trigger the hero to jump over the hole. 4T026-1-A @ Peter Lo 2015

1. Using Canvas 1.1 Hide Action Bar and Status Bar 1. Create the Android application with the following attributes. Application Name: MyDrawing Project Name: Package Name: MyDrawing com.example.mydrawing 2. Modify the source code of "MainActivity.java" as follow. package com.example.mydrawing; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.window; import android.view.windowmanager; public class MainActivity extends Activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // Hide Action Bar requestwindowfeature(window.feature_no_title); // Hide status bar getwindow().setflags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setcontentview(r.layout.activity_main); public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 4T026-1-A @ Peter Lo 2015 1

3. Save and execute the apps, you action bar and status bar should be hide 1.2 Create Custom View 1. Open the previous project, Right click the package com.example.mydrawing, and select New Class. 2. Input MyView as the class name and press [Finish] button. 4T026-1-A @ Peter Lo 2015 2

3. Modify the source code of "MyView.java" as follow. package com.example.mydrawing; import android.content.context; import android.view.view; public class MyView extends View { public MyView(Context context) { super(context); 4. Modify the source code of "MainActivity.java" as follow. package com.example.mydrawing; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.window; import android.view.windowmanager; import android.view.view; public class MainActivity extends Activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // Hide Action Bar requestwindowfeature(window.feature_no_title); // Hide status bar getwindow().setflags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // setcontentview(r.layout.activity_main); View MyView = new MyView(this); setcontentview(myview); 4T026-1-A @ Peter Lo 2015 3

public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 4. Save and execute the apps, can you see a blank screen? 1.3 Simple Drawing 1. Modify the source code of "MyView.java" as follow. package com.example.mydrawing; import android.content.context; import android.view.view; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; public class MyView extends View { public Paint mpaint; // The paint used for drawing public MyView(Context context) { super(context); // Construct the paint (e.g. style, color) mpaint = new Paint(); 4T026-1-A @ Peter Lo 2015 4

public void ondraw(canvas canvas) { // Draw a red circle at (400, 100) with radius 50 mpaint.setcolor(color.red); canvas.drawcircle(400, 100, 50, mpaint); // Draw a yellow rectangle from (100, 400) to (400, 600) mpaint.setcolor(color.yellow); canvas.drawrect(100, 400, 400, 600, mpaint); // Draw a blue lines from (0, 600) to (480, 600) mpaint.setcolor(color.blue); canvas.drawline(0, 600, 480, 600, mpaint); 2. Save and execute the apps, can you see your beautiful drawing? 4T026-1-A @ Peter Lo 2015 5

2. Simple Animation 2.1 Load Simple Picture 1. Create the Android application with the following attributes. Application Name: MyAnimation Project Name: Package Name: MyAnimation com.example.myianimation 2. Name the pictures in a sequential order. img0.png img1.png img2.png img3.png 3. Put the images into the folder drawable-hdpi (drawable-xxhdpi for high resolution model). 4. Right click the package com.example.myanimation, and select New Class. Input MyView as the class name and press [Finish] button. 4T026-1-A @ Peter Lo 2015 6

5. Modify the source code of "MyView.java" as follow. package com.example.myanimation; import android.content.context; import android.view.view; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; public class MyView extends View { public int N = 0; public Bitmap[] mimage; public MyView(Context context) { super(context); // Construct the Bitmap array mimage = new Bitmap[10]; // Read the image from Drawable folder mimage[0] = BitmapFactory.decodeResource(getResources(), R.drawable.img0); public void ondraw(canvas canvas) { // Display the image at (100, 400) canvas.drawbitmap(mimage[n], 100, 400, null); 6. Modify the source code of "MainActivity.java" as follow. package com.example.myanimation; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.window; import android.view.windowmanager; import android.view.view; 4T026-1-A @ Peter Lo 2015 7

public class MainActivity extends Activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // Hide Action Bar requestwindowfeature(window.feature_no_title); // Hide status bar getwindow().setflags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // setcontentview(r.layout.activity_main); View MyView = new MyView(this); setcontentview(myview); public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 7. Save and execute the apps, can you see the lovely girl? 4T026-1-A @ Peter Lo 2015 8

2.2 Create Simple Animation 1. Modify the source code of "MyView.java" as follow. package com.example.myanimation; import android.content.context; import android.view.view; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; public class MyView extends View { public int N = 0; public Bitmap[] mimage; public MyView(Context context) { super(context); // Construct the Bitmap array mimage = new Bitmap[10]; // Read the image from Drawable folder mimage[0] = BitmapFactory.decodeResource(getResources(), R.drawable.img0); mimage[1] = BitmapFactory.decodeResource(getResources(), R.drawable.img1); mimage[2] = BitmapFactory.decodeResource(getResources(), R.drawable.img2); mimage[3] = BitmapFactory.decodeResource(getResources(), R.drawable.img3); public void ondraw(canvas canvas) { // Display the image at (100, 400) canvas.drawbitmap(mimage[n], 100, 400, null); // Sleep for 60ms try { Thread.sleep(60); catch (Exception e) { e.printstacktrace(); 4T026-1-A @ Peter Lo 2015 9

// Increase the counter if (N < 3) { N++; else { N = 0; // Redraw the screen invalidate(); 2. Save and execute the apps, can you see the animation? 4T026-1-A @ Peter Lo 2015 10

3. Paint Brush 3.1 Handle Touch Event 1. Create the Android application with the following attributes. Application Name: MyPaintBrush Project Name: Package Name: MyPaintBrush com.example.mypaintbrush 2. Right click the package com.example.mypaintbrush, and select New Class. Input MyView as the class name and press [Finish] button. 3. Modify the source code of "MyView.java" as follow. package com.example.mypaintbrush; import android.content.context; import android.view.view; import android.view.motionevent; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.point; import java.util.arraylist; import java.util.list; public class MyView extends View { List<Point> points = new ArrayList<Point>(); Paint paint = new Paint(); 4T026-1-A @ Peter Lo 2015 11

public MyView(Context context) { super(context); paint.setcolor(color.green); paint.setantialias(true); public boolean ontouchevent(motionevent event) { Point point = new Point(); // Obtain the coordinate for the touch point point.x = (int)event.getx(); point.y = (int)event.gety(); // Add the point to the list points.add(point); // Redraw the screen invalidate(); return true; public void ondraw(canvas canvas) { for (Point point : points) { // Draw the point on screen canvas.drawcircle(point.x, point.y, 5, paint); 4. Modify the source code of "MainActivity.java" as follow. package com.example.mypaintbrush; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.window; 4T026-1-A @ Peter Lo 2015 12

import android.view.windowmanager; import android.view.view; public class MainActivity extends Activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // Hide Action Bar requestwindowfeature(window.feature_no_title); // Hide status bar getwindow().setflags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // setcontentview(r.layout.activity_main); View MyView = new MyView(this); setcontentview(myview); public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 5. Save and execute the apps, now you can draw something on your screen. 4T026-1-A @ Peter Lo 2015 13

4. Game Development 4.1 Draw the Initial Game World 1. Create the Android application with the following attributes. Application Name: MyMotionGame Project Name: Package Name: MyMotionGame com.example.mymotiongame 2. Put the images into the folder res/drawable-xxhdpi. brick.png life.png hero0.png hero1.png dollar.png gameover.png 3. Right click the package com.example.mymotiongame, and select New Class. Input MyView as the class name and press [Finish] button. Modify the source code of "MyView.java" as follow. package com.example.mymotiongame; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.view.view; public class MyView extends View { // Define the constant for game status final int GAME_PLAY = 1, GAME_DROP = 2, GAME_OVER = 9; // Game Status: Playing // Game Status: Drop to Hole // Game Status: Game Over private Paint mpaint; private Bitmap[] imghero; private Bitmap imgbrick; private Bitmap imglife; private Bitmap imgdollar; // The paint used for drawing // Graphic for Hero moving // Graphic for Brick // Graphic for Life // Graphic for Dollar 4T026-1-A @ Peter Lo 2015 14

private Bitmap imggameover; private int HeroPicture = 0; private int Map[] = new int[10]; private int CurrentLife = 3; private int score = 0; private int HeroY = 535; private int MapX = 0; private int ScreenMode = 1; // Graphic for GameOver // Index for Hero picture // Map // Number of Life leave // Display the Score // Y-coordinate for Hero // X coordinate for Map // Game Screen Status public MyView(Context context) { super(context); // Set the font size and color mpaint = new Paint(); mpaint.setcolor(color.black); mpaint.settextsize(30); // Construct the Bitmap array imghero = new Bitmap[2]; // Read the image from Drawable folder imgbrick = BitmapFactory.decodeResource(getResources(), R.drawable.brick); imglife = BitmapFactory.decodeResource(getResources(), R.drawable.life); imgdollar = BitmapFactory.decodeResource(getResources(), R.drawable.dollar); imggameover = BitmapFactory.decodeResource(getResources(), R.drawable.gameover); imghero[0] = BitmapFactory.decodeResource(getResources(), R.drawable.hero0); imghero[1] = BitmapFactory.decodeResource(getResources(), R.drawable.hero1); // Initial the map (set it as ground first) for (int i=0; i<9; i++) { Map[i] = 2; 4T026-1-A @ Peter Lo 2015 15

public void ondraw(canvas canvas) { // Draw the game screen for (int i=0; i<9; i++) { switch (Map[i]) { case 0: // 20% to display a hole break; case 1: // 20% to display a coin canvas.drawbitmap(imgdollar, MapX + 60*i, 550, null); canvas.drawbitmap(imgbrick, MapX + 60*i, 600, null); break; default: // default to display a ground canvas.drawbitmap(imgbrick, MapX + 60*i, 600, null); break; // Display the number of life for (int i=1; i<=currentlife; i++) { canvas.drawbitmap(imglife, 280 + 45*i, 50, null); // Draw the score canvas.drawtext("score:" + score, 30, 80, mpaint); // Draw the Hero canvas.drawbitmap(imghero[heropicture], 70, HeroY, null); // Draw the Game Over screen if (ScreenMode == GAME_OVER ) { canvas.drawbitmap(imggameover, 50, 100, null); 4. Modify the source code of "MainActivity.java" as follow. package com.example.mymotiongame; import android.app.activity; import android.os.bundle; import android.view.menu; 4T026-1-A @ Peter Lo 2015 16

import android.view.window; import android.view.windowmanager; import android.view.view; public class MainActivity extends Activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // Hide Action Bar requestwindowfeature(window.feature_no_title); // Hide status bar getwindow().setflags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // setcontentview(r.layout.activity_main); View MyView = new MyView(this); setcontentview(myview); public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 5. Save and execute the app, you should able to see the initial game world. 4T026-1-A @ Peter Lo 2015 17

4.2 Handle the Character Movement 1. Open the previous project and modify the source code of "MyView.java" as follow. package com.example.mymotiongame; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.view.view; import java.util.random; public class MyView extends View { // Define the constant for game status final int GAME_PLAY = 1, GAME_DROP = 2, GAME_OVER = 9; // Game Status: Playing // Game Status: Drop to Hole // Game Status: Game Over private Paint mpaint; private Bitmap[] imghero; private Bitmap imgbrick; private Bitmap imglife; private Bitmap imgdollar; private Bitmap imggameover; private int HeroPicture = 0; private int Map[] = new int[10]; private int CurrentLife = 3; private int score = 0; private int HeroY = 535; private int MapX = 0; private int ScreenMode = 1; private int HeroSpeed = 10; // The paint used for drawing // Graphic for Hero moving // Graphic for Brick // Graphic for Life // Graphic for Dollar // Graphic for GameOver // Index for Hero picture // Map // Number of Life leave // Display the Score // Y-coordinate for Hero // X coordinate for Map // Game Screen Status // The Walking speed public MyView(Context context) { super(context); // Set the font size and color mpaint = new Paint(); 4T026-1-A @ Peter Lo 2015 18

mpaint.setcolor(color.black); mpaint.settextsize(30); // Construct the Bitmap array imghero = new Bitmap[2]; // Read the image from Drawable folder imgbrick = BitmapFactory.decodeResource(getResources(), R.drawable.brick); imglife = BitmapFactory.decodeResource(getResources(), R.drawable.life); imgdollar = BitmapFactory.decodeResource(getResources(), R.drawable.dollar); imggameover = BitmapFactory.decodeResource(getResources(), R.drawable.gameover); imghero[0] = BitmapFactory.decodeResource(getResources(), R.drawable.hero0); imghero[1] = BitmapFactory.decodeResource(getResources(), R.drawable.hero1); // Initial the map (set it as ground first) for (int i=0; i<9; i++) { Map[i] = 2; public void ondraw(canvas canvas) { // Generate the Random Number when program start Random randomize = new Random(); // Move the map to left according to Hero speed MapX = MapX - HeroSpeed; if (MapX < -59) { // Shift the Map to left MapX = 0; for (int i=0; i<9; i++) { Map[i] = Map[i+1]; // Generate the next map 4T026-1-A @ Peter Lo 2015 19

Map[9] = randomize.nextint(5); // Set the Hero to Walk if current stand if (HeroPicture == 1) { HeroPicture = 0; else { HeroPicture = 1; // Handle hero drop to hole if (ScreenMode == GAME_DROP) { try { Thread.sleep(2000); // Stop the screen for 2 second catch (Exception e) { e.printstacktrace(); // Reset the Screen for (int i=0; i<9; i++) { // Reset the map to ground Map[i] = 2; HeroY = 535; ScreenMode = GAME_PLAY; // Reset Hero location // Reset the Game Screen Flag // Check for Get Coin action if (Map[2] == 1 && HeroY > 500) { score = score + 10; Map[2] = 2; // Increase the score if get a coin // Reset the map to ground // Check for Drop to a hole if (Map[1] == 0 && HeroY > 500) { HeroY = 600; // Draw the hero in the hole CurrentLife = CurrentLife - 1; // Reduce one life if ( CurrentLife <= 0 ) { ScreenMode = GAME_OVER; // Change the flag to GAME_OVER else { 4T026-1-A @ Peter Lo 2015 20

ScreenMode = GAME_DROP; // Change the flag to GAME_DROP // Draw the game screen for (int i=0; i<9; i++) { switch (Map[i]) { case 0: // 20% to display a hole break; case 1: // 20% to display a coin canvas.drawbitmap(imgdollar, MapX + 60*i, 550, null); canvas.drawbitmap(imgbrick, MapX + 60*i, 600, null); break; default: // default to display a ground canvas.drawbitmap(imgbrick, MapX + 60*i, 600, null); break; // Display the number of life for (int i=1; i<=currentlife; i++) { canvas.drawbitmap(imglife, 280 + 45*i, 50, null); // Draw the score canvas.drawtext("score:" + score, 30, 80, mpaint); // Draw the Hero canvas.drawbitmap(imghero[heropicture], 70, HeroY, null); // Draw the Game Over screen if (ScreenMode == GAME_OVER ) { canvas.drawbitmap(imggameover, 50, 100, null); try { if ( ScreenMode!= GAME_OVER ) { Thread.sleep(60); invalidate(); // Sleep for 60ms // Redraw the screen 4T026-1-A @ Peter Lo 2015 21

catch (Exception e) { e.printstacktrace(); 2. Save and execute the app, the score can be increased when you get a coin. However, the hero dies easily because it always drops to the hole. 4.3 Handle the Touch Event (for Hero Jump) 1. Open the previous project and modify the source code of "MyView.java" as follow. package com.example.mymotiongame; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.view.view; import java.util.random; import android.view.motionevent; public class MyView extends View { // Define the constant for game status final int GAME_PLAY = 1, GAME_DROP = 2, GAME_OVER = 9; // Game Status: Playing // Game Status: Drop to Hole // Game Status: Game Over 4T026-1-A @ Peter Lo 2015 22

private Paint mpaint; private Bitmap[] imghero; private Bitmap imgbrick; private Bitmap imglife; private Bitmap imgdollar; private Bitmap imggameover; private int HeroPicture = 0; private int Map[] = new int[10]; private int CurrentLife = 3; private int score = 0; private int HeroY = 535; private int MapX = 0; private int ScreenMode = 1; private int HeroSpeed = 10; private int JumpAction = 0; // The paint used for drawing // Graphic for Hero moving // Graphic for Brick // Graphic for Life // Graphic for Dollar // Graphic for GameOver // Index for Hero picture // Map // Number of Life leave // Display the Score // Y-coordinate for Hero // X coordinate for Map // Game Screen Status // The Walking speed // Jump Action public MyView(Context context) { super(context); // Set the font size and color mpaint = new Paint(); mpaint.setcolor(color.black); mpaint.settextsize(30); // Construct the Bitmap array imghero = new Bitmap[2]; // Read the image from Drawable folder imgbrick = BitmapFactory.decodeResource(getResources(), R.drawable.brick); imglife = BitmapFactory.decodeResource(getResources(), R.drawable.life); imgdollar = BitmapFactory.decodeResource(getResources(), R.drawable.dollar); imggameover = BitmapFactory.decodeResource(getResources(), R.drawable.gameover); imghero[0] = BitmapFactory.decodeResource(getResources(), R.drawable.hero0); imghero[1] = BitmapFactory.decodeResource(getResources(), R.drawable.hero1); 4T026-1-A @ Peter Lo 2015 23

// Initial the map (set it as ground first) for (int i=0; i<9; i++) { Map[i] = 2; public boolean ontouchevent(motionevent event) { JumpAction = 1; return true; public void ondraw(canvas canvas) { // Generate the Random Number when program start Random randomize = new Random(); // Move the map to left according to Hero speed MapX = MapX - HeroSpeed; if (MapX < -59) { // Shift the Map to left MapX = 0; for (int i=0; i<9; i++) { Map[i] = Map[i+1]; // Generate the next map Map[9] = randomize.nextint(5); // Set the Hero to Walk if current stand if (HeroPicture == 1) { HeroPicture = 0; else { HeroPicture = 1; // Handle the Jump up action 4T026-1-A @ Peter Lo 2015 24

if (JumpAction == 1) { if (HeroY > 400){ HeroY = HeroY - 30; // Jump up until a defined high else { JumpAction = -1; // Reset the Jump Directions to Down // Handle the Jump Down action else if (JumpAction == -1) { if (HeroY < 500) { HeroY = HeroY + 30; // Free Fall to the ground else { HeroY = 535; JumpAction = 0; // Reset to walking mode // Handle hero drop to hole if (ScreenMode == GAME_DROP) { try { Thread.sleep(2000); // Stop the screen for 2 second catch (Exception e) { e.printstacktrace(); // Reset the Screen for (int i=0; i<9; i++) { // Reset the map to ground Map[i] = 2; HeroY = 535; JumpAction = 0; ScreenMode = GAME_PLAY; // Reset Hero location // Set the Hero to Walk (No Jump) // Reset the Game Screen Flag // Check for Get Coin action if (Map[2] == 1 && HeroY > 500) { score = score + 10; Map[2] = 2; // Increase the score if get a coin // Reset the map to ground 4T026-1-A @ Peter Lo 2015 25

// Check for Drop to a hole if (Map[1] == 0 && HeroY > 500) { HeroY = 600; CurrentLife = CurrentLife - 1; // Draw the hero in the hole // Reduce one life if ( CurrentLife <= 0 ) { ScreenMode = GAME_OVER; // Change the flag to GAME_OVER else { ScreenMode = GAME_DROP; // Change the flag to GAME_DROP // Draw the game screen for (int i=0; i<9; i++) { switch (Map[i]) { case 0: // 20% to display a hole break; case 1: // 20% to display a coin canvas.drawbitmap(imgdollar, MapX + 60*i, 550, null); canvas.drawbitmap(imgbrick, MapX + 60*i, 600, null); break; default: // default to display a ground canvas.drawbitmap(imgbrick, MapX + 60*i, 600, null); break; // Display the number of life for (int i=1; i<=currentlife; i++) { canvas.drawbitmap(imglife, 280 + 45*i, 50, null); // Draw the score canvas.drawtext("score:" + score, 30, 80, mpaint); // Draw the Hero canvas.drawbitmap(imghero[heropicture], 70, HeroY, null); // Draw the Game Over screen if (ScreenMode == GAME_OVER ) { 4T026-1-A @ Peter Lo 2015 26

canvas.drawbitmap(imggameover, 50, 100, null); try { if ( ScreenMode!= GAME_OVER ) { Thread.sleep(60); invalidate(); // Sleep for 60ms // Redraw the screen catch (Exception e) { e.printstacktrace(); 2. Save and execute the app, touch the screen to dump across the hole. How many marks can you get? 4T026-1-A @ Peter Lo 2015 27