Skip to content

Instantly share code, notes, and snippets.

@clamytoe
Last active August 29, 2015 14:27
Show Gist options
  • Save clamytoe/51343bfafc7eb2580acd to your computer and use it in GitHub Desktop.
Save clamytoe/51343bfafc7eb2580acd to your computer and use it in GitHub Desktop.
HKUSTx: COMP107x Introduction to Mobile Application Development using Android
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise: Shooting Game\n",
"---\n",
"## Objectives\n",
"\n",
"In this exercise you will continue to learn more about 2D graphics support in Android using a View, and the use of the onDraw() method and canvas. At the end of this exercise you will be able to:\n",
"\n",
"* Define custom views in Android by subclassing the View class and use them in your UI\n",
"* Make use of the 2D graphics support in Android to draw on the view canvas\n",
"* Use the invalidate() method to make Android framework redraw the UI. We use this as a way of generating motion on the screen\n",
"\n",
"## Introduction\n",
"\n",
"This project further demonstrates the use of a view canvas to create 2D graphics in Android for implementing a simple 2D shooting game.\n",
"\n",
"---\n",
"### Step 1: Examining the Project Files\n",
"\n",
"1. Download the [ShootingGame.zip](http://w02.hkvu.hk/edX/COMP107x/w3/ShootingGame.zip) file, unzip it and import the project into Android Studio.\n",
"2. Open the **ShootingGame.java** file.\n",
"3. Open the **activity_shooting_game.xml** file. Note on the use of the custom view in the UI layout.\n",
"4. Then, open the remaining java files and examine the contents.\n",
"5. You may refer back to the video instructions for detailed explanation.\n",
"\n",
"---\n",
"### Step 2: Updating the DrawView\n",
"\n",
"1. Update the drawGameBoard() method in the DrawView class as shown below."
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
" canvas.drawColor(Color.WHITE); //if you want another background color\n",
" // Draw the cannon\n",
" cannon.draw(canvas);\n",
"\n",
" // Draw all the bullets\n",
" for (int i = 0; i < bullets.size(); i++ ) {\n",
" if (bullets.get(i) != null) {\n",
" bullets.get(i).draw(canvas);\n",
"\n",
" if (bullets.get(i).move() == false) {\n",
" bullets.remove(i);\n",
" }\n",
" }\n",
" }\n",
"\n",
" // Draw all the explosions, at those locations where the bullet\n",
" // hits the Android Guy\n",
" for (int i = 0; i < explosions.size(); i++ ) {\n",
" if (explosions.get(i) != null) {\n",
" if (explosions.get(i).draw(canvas) == false) {\n",
" explosions.remove(i);\n",
" }\n",
" }\n",
" }\n",
"\n",
" // If the Android Guy is falling, check to see if any of the bullets\n",
" // hit the Guy. Draw the Android Guy\n",
" if (androidGuy != null) {\n",
" androidGuy.draw(canvas);\n",
"\n",
" RectF guyRect = androidGuy.getRect();\n",
"\n",
" for (int i = 0; i < bullets.size(); i++ ) {\n",
"\n",
" // The rectangle surrounding the Guy and Bullet intersect, then it's a collision\n",
" // Generate an explosion at that location and delete the Guy and bullet. Generate\n",
" // a new Android Guy to fall from the top.\n",
" if (RectF.intersects(guyRect, bullets.get(i).getRect())) {\n",
" explosions.add(new Explosion(Color.RED,mContext, androidGuy.getX(), androidGuy.getY()));\n",
" androidGuy.reset();\n",
" bullets.remove(i);\n",
" break;\n",
" }\n",
"\n",
" }\n",
"\n",
" if (androidGuy.move() == false) {\n",
" androidGuy = null;\n",
" }\n",
" }\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment