First commit

This commit is contained in:
Administrator
2014-02-15 09:38:34 +01:00
commit 60e341a363
58 changed files with 1308 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
+33
View File
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Pong</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
+4
View File
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.source=1.6
+29
View File
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mangavalk.canvasfirsttry"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<activity
android:name="mangavalk.canvasfirsttry.MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
+29
View File
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mangavalk.canvasfirsttry"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<activity
android:name="mangavalk.canvasfirsttry.MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+3
View File
@@ -0,0 +1,3 @@
# cache for current jar dependency. DO NOT EDIT.
# format is <lastModified> <length> <SHA-1> <path>
# Encoding is UTF-8
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
View File
Binary file not shown.
+218
View File
@@ -0,0 +1,218 @@
package mangavalk.canvasfirsttry;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
public class CanvasView extends View {
Resources r ;
float dpHeight;
float dpWidth;
float Height;
float Width;
float moveX1,moveX2;
float moveY1,moveY2;
long oldTime;
public CanvasView(Context context) {
super(context);
super.setBackgroundColor(Color.WHITE);
oldTime = System.currentTimeMillis();
this.postInvalidate();
}
public void SetActivity(Activity activity)
{
Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
r = getResources();
float density = getResources().getDisplayMetrics().density;
dpHeight = outMetrics.heightPixels / density;
dpWidth = outMetrics.widthPixels / density;
Height = outMetrics.heightPixels;
Width = outMetrics.widthPixels;
moveY1 = 50;
moveY2 = 50;
}
int count = 0;
@Override
protected void onDraw(Canvas canvas) {
count++;
long newTime = System.currentTimeMillis();
//drawText(canvas, count);
DrawFps(canvas, newTime);
drawPeddleRight(canvas, moveY1);
drawPeddleLeft(canvas, moveY2);
oldTime = newTime;
//this.postInvalidateDelayed( 100 );
this.postInvalidate();
}
private void drawRoundedRectangle(Canvas canvas) {
// TODO Auto-generated method stub
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.MAGENTA);
paint.setStyle(Style.FILL);
int xPosition = 10;
int yPosition = 350;
int rectangleWidth = 100;
int rectangleHeight = 50;
RectF rect=new RectF(xPosition, yPosition, xPosition + rectangleWidth,
yPosition + rectangleHeight);
canvas.drawRoundRect(rect, 10,10, paint);
paint.setStyle(Style.STROKE);
xPosition = 150;
rect=new RectF(xPosition, yPosition, xPosition + rectangleWidth,
yPosition + rectangleHeight);
canvas.drawRoundRect(rect, 10,10, paint);
}
private void drawRotatedImage(Canvas canvas) {
// TODO Auto-generated method stub
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
Matrix matrix = new Matrix();
matrix.postRotate(60);// angle to rotate in clockwise direction
matrix.postTranslate(200, 180);
canvas.drawBitmap(bitmap, matrix, null);
}
private void DrawFps(Canvas canvas, long newTime)
{
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(30);
String text = "FPS: " + (1000/(newTime-oldTime));
canvas.drawText(text, GetRealPixelSize(dpWidth, 5), GetRealPixelSize(dpHeight, 5), paint);
}
float map(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
private void drawPeddleRight(Canvas canvas, float i) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setStyle(Style.FILL);
float rectangleWidth = 2;
float rectangleHeight = 20;
float xPosition = 97;
float yPosition = i;
yPosition = yPosition <= (100-(rectangleHeight / 2))?yPosition:(100-(rectangleHeight / 2));
yPosition = yPosition >= (rectangleHeight / 2)?yPosition:(rectangleHeight / 2);
canvas.drawRect(
GetRealPixelSize(dpWidth, xPosition),
GetRealPixelSize(dpHeight, yPosition - (rectangleHeight / 2)),
GetRealPixelSize(dpWidth, xPosition + (rectangleWidth)),
GetRealPixelSize(dpHeight, yPosition + (rectangleHeight / 2)), paint);
}
private void drawPeddleLeft(Canvas canvas, float i) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setStyle(Style.FILL);
float rectangleWidth = 2;
float rectangleHeight = 20;
float xPosition = 3;
float yPosition = i;
yPosition = yPosition <= (100-(rectangleHeight / 2))?yPosition:(100-(rectangleHeight / 2));
yPosition = yPosition >= (rectangleHeight / 2)?yPosition:(rectangleHeight / 2);
canvas.drawRect(
GetRealPixelSize(dpWidth, xPosition - (rectangleWidth)),
GetRealPixelSize(dpHeight, yPosition - (rectangleHeight / 2)),
GetRealPixelSize(dpWidth, xPosition),
GetRealPixelSize(dpHeight, yPosition + (rectangleHeight / 2)), paint);
}
private float GetRealPixelSize(float dpWH, float DIP)
{
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (dpWH * ( DIP / 100 )), r.getDisplayMetrics());
}
private void drawImage(Canvas canvas) {
// TODO Auto-generated method stub
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
canvas.drawBitmap(bitmap, 10, 200, null);
}
private void drawRectangle(Canvas canvas) {
// TODO Auto-generated method stub
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
int xPosition = 10;
int yPosition = 150;
int rectangleWidth = 100;
int rectangleHeight = 50;
canvas.drawRect(xPosition, yPosition, xPosition + rectangleWidth,
yPosition + rectangleHeight, paint);
paint.setStyle(Style.STROKE);
xPosition = 150;
canvas.drawRect(xPosition, yPosition, xPosition + rectangleWidth,
yPosition + rectangleHeight, paint);
}
private void drawCircle(Canvas canvas) {
// TODO Auto-generated method stub
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
paint.setStyle(Style.FILL);
canvas.drawCircle(50, 60, 50, paint);
paint.setStyle(Style.STROKE);
canvas.drawCircle(200, 60, 50, paint);
}
public void sendMove(float x, float y) {
if (x / Width >= 0.6f)
{
moveX1 = (x / Width) * 100;
moveY1 = (y / Height) * 100;
}
else if (x / Width <= 0.4f)
{
moveX2 = (x / Width) * 100;
moveY2 = (y / Height) * 100;
}
}
}
@@ -0,0 +1,6 @@
/** Automatically generated file. DO NOT MODIFY */
package mangavalk.canvasfirsttry;
public final class BuildConfig {
public final static boolean DEBUG = true;
}
+76
View File
@@ -0,0 +1,76 @@
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package mangavalk.canvasfirsttry;
public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
*/
public static final int activity_horizontal_margin=0x7f050000;
public static final int activity_vertical_margin=0x7f050001;
}
public static final class drawable {
public static final int gameover=0x7f020000;
public static final int ic_launcher=0x7f020001;
public static final int player1=0x7f020002;
public static final int player2=0x7f020003;
}
public static final class id {
public static final int action_settings=0x7f090000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int main=0x7f080000;
}
public static final class raw {
public static final int bat_cat=0x7f040000;
public static final int gameover=0x7f040001;
public static final int pling=0x7f040002;
}
public static final class string {
public static final int action_settings=0x7f060001;
public static final int app_name=0x7f060000;
public static final int hello_world=0x7f060002;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f070000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f070001;
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.
+20
View File
@@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
+14
View File
@@ -0,0 +1,14 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-19
Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

+16
View File
@@ -0,0 +1,16 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
+9
View File
@@ -0,0 +1,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
+8
View File
@@ -0,0 +1,8 @@
<resources>
<!--
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw600dp devices (e.g. 7" tablets) here.
-->
</resources>
+9
View File
@@ -0,0 +1,9 @@
<resources>
<!--
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
-->
<dimen name="activity_horizontal_margin">128dp</dimen>
</resources>
+11
View File
@@ -0,0 +1,11 @@
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>
</resources>
+12
View File
@@ -0,0 +1,12 @@
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>
+7
View File
@@ -0,0 +1,7 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">CanvasFirstTry</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
+20
View File
@@ -0,0 +1,20 @@
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
@@ -0,0 +1,675 @@
package mangavalk.canvasfirsttry;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
public class CanvasView extends View
{
//region Variabelen
Resources r;
// Sizes
float dpHeight;
float dpWidth;
float Height;
float Width;
// Screen Boundary's
float boundaryTop = 10;
float boundaryBottom = 95;
// Pointers
float moveX1,moveX2;
float moveY1,moveY2;
// Ball
boolean bounceXdirection, bounceYdirection;
float bounceX, bounceY;
float speedX, speedY;
// Pedles
float rectangleHeight1 = 20;
float rectangleHeight2 = 20;
// Score
int score;
int Player1Life = 3;
int Player2Life = 3;
// Fps
long oldTime;
// Sound
boolean musicisplaying = false;
private SoundPool soundPool,soundPool2;
private int Sound_Pling,Sound_GameOver,Sound_Music;
// Game Status
boolean gamePlaying = false;
boolean gameover = false;
// Difficulty
int BotDif = 0;
// Style
int selectedStyle = 0;
boolean StyleRound = true;
int Color_PedleLeft = Color.WHITE;
int Color_PedleRight = Color.WHITE;
int Color_Ball = Color.RED;
int Color_ScoreBar = Color.GRAY;
int Color_Footer = Color.GRAY;
int Color_Background = Color.BLACK;
int Color_Score = Color.WHITE;
//endregion Variabelen
public CanvasView(Context context) {
super(context);
super.setBackgroundColor(Color_Background);
oldTime = System.currentTimeMillis();
this.postInvalidate();
}
public void SetActivity(Activity activity)
{
Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
r = getResources();
float density = getResources().getDisplayMetrics().density;
dpHeight = outMetrics.heightPixels / density;
dpWidth = outMetrics.widthPixels / density;
Height = outMetrics.heightPixels;
Width = outMetrics.widthPixels;
// Set Basic Score And Pedle Position
moveY1 = 50;
moveY2 = 50;
bounceX = bounceY = 40 + (int)(Math.random() * ((60 - 40) + 1));
bounceY = bounceY = 0 + (int)(Math.random() * ((100 - 0) + 1));
speedX = 1;
speedY = 1.1f;
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool2 = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
Sound_Pling = soundPool.load(activity, R.raw.pling, 1);
Sound_GameOver = soundPool.load(activity, R.raw.gameover, 1);
Sound_Music = soundPool2.load(activity, R.raw.bat_cat, 1);
}
@Override
protected void onDraw(Canvas canvas)
{
if (musicisplaying == false)
{
soundPool2.play(Sound_Music, 1, 1, 0, -1, 1);
musicisplaying = true;
}
if (gamePlaying)
{
Bot();
drawPeddleRight(canvas);
drawPeddleLeft(canvas);
Bal();
drawCircle(canvas, bounceX, bounceY);
// Draw Score Bar
ScoreBar(canvas, boundaryTop);
Footer(canvas, boundaryBottom);
// Fps
long newTime = System.currentTimeMillis();
DrawFps(canvas, newTime);
// Score
DrawText(canvas, "Life: " + Player1Life, 15);
DrawText(canvas, "Score: " + score, 45);
DrawText(canvas, "Life: " + Player2Life, 85);
oldTime = newTime;
if (gameover)
{
gameover = false;
// Sound
soundPool.play(Sound_GameOver, .5f, .5f, 0, 0, 1);
// Show GameOver message
if ((Player1Life < 0) || (Player2Life < 0))
{
DrawGameOver(canvas);
gamePlaying = false;
// New Life
Player1Life = 3;
Player2Life = 3;
}
// New Ball
// Position
bounceX = 40 + (int)(Math.random() * ((60 - 40) + 1));
bounceY = 0 + (int)(Math.random() * ((100 - 0) + 1));
// Direction
bounceXdirection = Math.random() >= 0.5f ? true : false;
// Speed
speedX = 1f;
speedY = 1f;
// Respawn time
this.postInvalidateDelayed( 5000 );
}
else
this.postInvalidate();
}
else // Show Start Menu
{
// Uhem
SetStyle(0);
Rect(canvas, 15, 25, 10, 25, Color.GRAY);
DrawText(canvas, "Start", 15, 20, 50);
Rect(canvas, 15+2, 25+2, 65, 80, Color.GRAY);
DrawText(canvas, "Style:", 70, 20+2, 50);
Rect(canvas, 25+4, 35+4, 65, 80, selectedStyle == 0 ? Color.RED : Color.GRAY);
DrawText(canvas, "Retro", 70, 30+4, 50);
if ((moveX1 >= 65 && moveX1 <= 80) && (moveY1 >= 25+4 && moveY1 <= 35+4))
selectedStyle = 0;
Rect(canvas, 35+6, 45+6, 65, 80, selectedStyle == 1 ? Color.RED : Color.GRAY);
DrawText(canvas, "Neon", 70, 40+6, 50);
if ((moveX1 >= 65 && moveX1 <= 80) && (moveY1 >= 35+6 && moveY1 <= 45+6))
selectedStyle = 1;
Rect(canvas, 45+8, 55+8, 65, 80, selectedStyle == 2 ? Color.RED : Color.GRAY);
DrawText(canvas, "Red", 70, 50+8, 50);
if ((moveX1 >= 65 && moveX1 <= 80) && (moveY1 >= 45+8 && moveY1 <= 55+8))
selectedStyle = 2;
Rect(canvas, 15+2, 25+2, 45, 60, Color.GRAY);
DrawText(canvas, "Bot:", 50, 20+2, 50);
Rect(canvas, 25+4, 35+4, 45, 60, BotDif == 0 ? Color.RED : Color.GRAY);
DrawText(canvas, "Hard", 50, 30+4, 50);
if ((moveX1 >= 45 && moveX1 <= 60) && (moveY1 >= 25+4 && moveY1 <= 35+4))
BotDif = 0;
Rect(canvas, 35+6, 45+6, 45, 60, BotDif == 1 ? Color.RED : Color.GRAY);
DrawText(canvas, "Normal", 50, 40+6, 50);
if ((moveX1 >= 45 && moveX1 <= 60) && (moveY1 >= 35+6 && moveY1 <= 45+6))
BotDif = 1;
Rect(canvas, 45+8, 55+8, 45, 60, BotDif == 2 ? Color.RED : Color.GRAY);
DrawText(canvas, "Easy", 50, 50+8, 50);
if ((moveX1 >= 45 && moveX1 <= 60) && (moveY1 >= 45+8 && moveY1 <= 55+8))
BotDif = 2;
Rect(canvas, 55+10, 65+10, 45, 60, BotDif == 3 ? Color.RED : Color.GRAY);
DrawText(canvas, "Novice", 50, 60+10, 50);
if ((moveX1 >= 45 && moveX1 <= 60) && (moveY1 >= 55+10 && moveY1 <= 65+10))
BotDif = 3;
Rect(canvas, 65+12, 75+12, 45, 60, BotDif == 4 ? Color.RED : Color.GRAY);
DrawText(canvas, "Off", 50, 70+12, 50);
if ((moveX1 >= 45 && moveX1 <= 60) && (moveY1 >= 65+12 && moveY1 <= 75+12))
BotDif = 4;
if ((moveX1 >= 15 && moveX1 <= 25) && (moveY1 >= 10 && moveY1 <= 25))// Start Pressed
{
// Set Style
SetStyle(selectedStyle);
// Set Basic Score And Padle Position
moveY1 = 50;
moveY2 = 50;
bounceX = bounceY = 40 + (int)(Math.random() * ((60 - 40) + 1));
bounceY = bounceY = 0 + (int)(Math.random() * ((100 - 0) + 1));
speedX = 1;
speedY = 1.1f;
Player1Life = 3;
Player2Life = 3;
gamePlaying = true;
}
this.postInvalidate();
}
}
private void SetStyle(int i)
{
// Set Style
switch (i)
{
case 0:
Color_PedleLeft = Color.WHITE;
Color_PedleRight = Color.WHITE;
Color_Ball = Color.RED;
Color_ScoreBar = Color.GRAY;
Color_Footer = Color.GRAY;
Color_Background = Color.BLACK;
Color_Score = Color.WHITE;
break;
case 1:
Color_PedleLeft = Color.MAGENTA;
Color_PedleRight = Color.MAGENTA;
Color_Ball = Color.BLUE;
Color_ScoreBar = Color.YELLOW;
Color_Footer = Color.YELLOW;
Color_Background = Color.CYAN;
Color_Score = Color.RED;
break;
case 2:
Color_PedleLeft = Color.RED;
Color_PedleRight = Color.RED;
Color_Ball = Color.RED;
Color_ScoreBar = Color.RED;
Color_Footer = Color.RED;
Color_Background = Color.LTGRAY;
Color_Score = Color.BLACK;
break;
}
super.setBackgroundColor(Color_Background);
}
private void Bal()
{
if (bounceXdirection)
if (bounceX >= 96 & bounceY >= (moveY1 - (rectangleHeight1/2)) & bounceY <= (moveY1 + (rectangleHeight1/2)))
{
bounceXdirection = !bounceXdirection;
score++;
// check angle;
//change speed y;
speedY = map( (bounceY - moveY1), -(rectangleHeight2/2), (rectangleHeight2/2), -2f, 2f);
if (speedY <= 0)
{
bounceYdirection = false;
speedY = Math.abs(speedY);
}
else
bounceYdirection = true;
speedX = 3f - speedY;
soundPool.play(Sound_Pling, .5f, .5f, 0, 0, 1);
}
else if (bounceX <= 96)
bounceX += speedX;
else
{
//dead
Player2Life--;
gameover = true;
}
else if (!bounceXdirection)
if (bounceX <= 4 & bounceY >= (moveY2 - (rectangleHeight2/2)) & bounceY <= (moveY2 + (rectangleHeight2/2)))
{
bounceXdirection = !bounceXdirection;
score++;
// check angle;
//change speed y;
speedY = map( (bounceY - moveY2), -(rectangleHeight2/2), (rectangleHeight2/2), -2f, 2f);
if (speedY <= 0)
{
bounceYdirection = false;
speedY = Math.abs(speedY);
}
else
bounceYdirection = true;
speedX = 3f - speedY;
soundPool.play(Sound_Pling, .5f, .5f, 0, 0, 1);
}
else if (bounceX >= 4)
bounceX -= speedX;
else
{
//dead
Player1Life--;
gameover = true;
}
// Y movement
if (bounceYdirection)
if (bounceY < boundaryBottom - 1.5f)
bounceY += speedY;
else
bounceYdirection = !bounceYdirection;
else if (!bounceYdirection)
{
if (bounceY > boundaryTop + 1.5f)
bounceY -= speedY;
else
bounceYdirection = !bounceYdirection;
}
}
private void Bot()
{
// Bot - hard
switch (BotDif)
{
case 0:
if (bounceX <= 40) // Left
if (bounceY > moveY2 + .1f + (int)(Math.random() * ((7 - .1f) + 1)))
moveY2 += 1f + (int)(Math.random() * ((5 - 1f) + 1));
else if (bounceY < moveY2 - .1f + (int)(Math.random() * ((7 - .1f) + 1)))
moveY2 -= 1f + (int)(Math.random() * ((5 - 1f) + 1));
if (bounceX >= 60) // Right
if (bounceY > moveY1 + .1f + (int)(Math.random() * ((7 - .1f) + 1)))
moveY1 += 1f + (int)(Math.random() * ((5 - 1f) + 1));
else if (bounceY < moveY1 - .1f + (int)(Math.random() * ((7 - .1f) + 1)))
moveY1 -= 1f + (int)(Math.random() * ((5 - 1f) + 1));
break;
// Bot - normal
case 1:
if (bounceX <= 40) // Left
if (bounceY > moveY2 + .1f + (int)(Math.random() * ((10 - .1f) + 1)))
moveY2 += 1f + (int)(Math.random() * ((3 - 1f) + 1));
else if (bounceY < moveY2 - .1f + (int)(Math.random() * ((10 - .1f) + 1)))
moveY2 -= 1f + (int)(Math.random() * ((3 - 1f) + 1));
if (bounceX >= 60) // Right
if (bounceY > moveY1 + .1f + (int)(Math.random() * ((10 - .1f) + 1)))
moveY1 += 1f + (int)(Math.random() * ((3 - 1f) + 1));
else if (bounceY < moveY1 - .1f + (int)(Math.random() * ((10 - .1f) + 1)))
moveY1 -= 1f + (int)(Math.random() * ((3 - 1f) + 1));
break;
// Bot - easy
case 2:
if (bounceX <= 40) // Left
if (bounceY > moveY2 + .1f + (int)(Math.random() * ((10 - .1f) + 1)))
moveY2 += .1f + (int)(Math.random() * ((3 - .1f) + 1));
else if (bounceY < moveY2 - .1f + (int)(Math.random() * ((10 - .1f) + 1)))
moveY2 -= .1f + (int)(Math.random() * ((3 - .1f) + 1));
if (bounceX >= 60) // Right
if (bounceY > moveY1 + .1f + (int)(Math.random() * ((10 - .1f) + 1)))
moveY1 += .1f + (int)(Math.random() * ((3 - .1f) + 1));
else if (bounceY < moveY1 - .1f + (int)(Math.random() * ((10 - .1f) + 1)))
moveY1 -= .1f + (int)(Math.random() * ((3 - .1f) + 1));
break;
// Bot - novice
case 3:
if (bounceX <= 40) // Left
if (bounceY > moveY2 + .1f + (int)(Math.random() * ((10 - .1f) + 1)))
moveY2++;
else if (bounceY < moveY2 - .1f + (int)(Math.random() * ((10 - .1f) + 1)))
moveY2--;
if (bounceX >= 60) // Right
if (bounceY > moveY1 + .1f + (int)(Math.random() * ((10 - .1f) + 1)))
moveY1++;
else if (bounceY < moveY1 - .1f + (int)(Math.random() * ((10 - .1f) + 1)))
moveY1--;
break;
}
}
private void DrawFps(Canvas canvas, long newTime)
{
Paint paint = new Paint();
paint.setColor(Color_Score);
paint.setTextSize(30);
String text = "FPS: " + (1000/(newTime-oldTime));
canvas.drawText(text, GetRealPixelSize(dpWidth, 5), GetRealPixelSize(dpHeight, 5), paint);
}
private void DrawText(Canvas canvas, String text, int x)
{
Paint paint = new Paint();
paint.setColor(Color_Score);
paint.setTextSize(30);
canvas.drawText(text, GetRealPixelSize(dpWidth, x), GetRealPixelSize(dpHeight, 5), paint);
}
private void DrawText(Canvas canvas, String text, int x, int y, int size)
{
Paint paint = new Paint();
paint.setColor(Color_Score);
paint.setTextSize(size);
canvas.drawText(text, GetRealPixelSize(dpWidth, x), GetRealPixelSize(dpHeight, y), paint);
}
private void DrawGameOver(Canvas canvas)
{
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.gameover);
bitmap = Bitmap.createScaledBitmap(bitmap, (int)GetRealPixelSize(dpWidth, 40), (int)GetRealPixelSize(dpHeight, 20), false);
canvas.drawBitmap(bitmap, (int)GetRealPixelSize(dpWidth, 30), (int)GetRealPixelSize(dpHeight, 40), null);
// Which Player Won? Player1Life
if (Player1Life < 0)
{
bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.player2);
bitmap = Bitmap.createScaledBitmap(bitmap, (int)GetRealPixelSize(dpWidth, 40), (int)GetRealPixelSize(dpHeight, 20), false);
canvas.drawBitmap(bitmap, (int)GetRealPixelSize(dpWidth, 30), (int)GetRealPixelSize(dpHeight, 55), null);
}
else
{
bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.player1);
bitmap = Bitmap.createScaledBitmap(bitmap, (int)GetRealPixelSize(dpWidth, 40), (int)GetRealPixelSize(dpHeight, 20), false);
canvas.drawBitmap(bitmap, (int)GetRealPixelSize(dpWidth, 30), (int)GetRealPixelSize(dpHeight, 55), null);
}
}
float map(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
private void ScoreBar(Canvas canvas, float Bottom)
{
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color_ScoreBar);
paint.setStyle(Style.FILL);
// Style Square
canvas.drawRect(
GetRealPixelSize(dpWidth, 0),
GetRealPixelSize(dpHeight, 0),
GetRealPixelSize(dpWidth, 100),
GetRealPixelSize(dpHeight, Bottom), paint);
}
private void Footer(Canvas canvas, float Top)
{
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color_Footer);
paint.setStyle(Style.FILL);
// Style Square
canvas.drawRect(
GetRealPixelSize(dpWidth, 0),
GetRealPixelSize(dpHeight, Top),
GetRealPixelSize(dpWidth, 100),
GetRealPixelSize(dpHeight, 100), paint);
}
private void drawPeddleRight(Canvas canvas) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color_PedleRight);
paint.setStyle(Style.FILL);
float rectangleWidth = 2;
rectangleHeight1 = 20;
moveY1 = moveY1 <= (boundaryBottom - (rectangleHeight1 / 2)) ? moveY1 : (boundaryBottom - (rectangleHeight1 / 2));
moveY1 = moveY1 >= (rectangleHeight1 / 2) + boundaryTop ? moveY1 : (rectangleHeight1 / 2) + boundaryTop;
float xPosition = 97;
float yPosition = moveY1;
// Style Square/Round
if (!StyleRound)
canvas.drawRect(
GetRealPixelSize(dpWidth, xPosition),
GetRealPixelSize(dpHeight, yPosition - (rectangleHeight1 / 2)),
GetRealPixelSize(dpWidth, xPosition + (rectangleWidth)),
GetRealPixelSize(dpHeight, yPosition + (rectangleHeight1 / 2)), paint);
else
{
RectF rect=new RectF(
GetRealPixelSize(dpWidth, xPosition),
GetRealPixelSize(dpHeight, yPosition - (rectangleHeight1 / 2)),
GetRealPixelSize(dpWidth, xPosition + (rectangleWidth)),
GetRealPixelSize(dpHeight, yPosition + (rectangleHeight1 / 2)));
canvas.drawRoundRect(rect, 20,20, paint);
}
}
private void Rect(Canvas canvas, float Top, float Bottom, float Left, float Right, int Color)
{
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color);
paint.setStyle(Style.FILL);
// Style Square
canvas.drawRect(
GetRealPixelSize(dpWidth, Left),
GetRealPixelSize(dpHeight, Top),
GetRealPixelSize(dpWidth, Right),
GetRealPixelSize(dpHeight, Bottom), paint);
}
private void drawPeddleLeft(Canvas canvas) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color_PedleLeft);
paint.setStyle(Style.FILL);
float rectangleWidth = 2;
rectangleHeight2 = 20;
moveY2 = moveY2 <= (boundaryBottom - (rectangleHeight2 / 2)) ? moveY2 : (boundaryBottom - (rectangleHeight2 / 2));
moveY2 = moveY2 >= (rectangleHeight2 / 2) + boundaryTop ? moveY2 : (rectangleHeight2 / 2) + boundaryTop;
float xPosition = 3;
float yPosition = moveY2;
// Style Square/Round
if (!StyleRound)
canvas.drawRect(
GetRealPixelSize(dpWidth, xPosition - (rectangleWidth)),
GetRealPixelSize(dpHeight, yPosition - (rectangleHeight2 / 2)),
GetRealPixelSize(dpWidth, xPosition),
GetRealPixelSize(dpHeight, yPosition + (rectangleHeight2 / 2)), paint);
else
{
RectF rect=new RectF(
GetRealPixelSize(dpWidth, xPosition - (rectangleWidth)),
GetRealPixelSize(dpHeight, yPosition - (rectangleHeight2 / 2)),
GetRealPixelSize(dpWidth, xPosition),
GetRealPixelSize(dpHeight, yPosition + (rectangleHeight2 / 2)));
canvas.drawRoundRect(rect, 20,20, paint);
}
}
private void drawCircle(Canvas canvas, float xPosition, float yPosition) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color_Ball);
paint.setStyle(Style.FILL);
canvas.drawCircle(GetRealPixelSize(dpWidth, xPosition), GetRealPixelSize(dpHeight, yPosition), GetRealPixelSize(dpWidth, 1), paint);
}
private float GetRealPixelSize(float dpWH, float DIP)
{
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (dpWH * ( DIP / 100 )), r.getDisplayMetrics());
}
public void sendMove(float x, float y)
{
if (!gamePlaying)
{
moveX1 = (x / Width) * 100;
moveY1 = (y / Height) * 100;
}
else if (x / Width >= 0.6f)
{
moveX1 = (x / Width) * 100;
moveY1 = (y / Height) * 100;
}
else if (x / Width <= 0.4f)
{
moveX2 = (x / Width) * 100;
moveY2 = (y / Height) * 100;
}
}
public void SetGameState(boolean b)
{
gamePlaying = false;
}
}
@@ -0,0 +1,92 @@
package mangavalk.canvasfirsttry;
import android.app.Activity;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.MotionEvent;
public class MainActivity extends Activity
{
CanvasView canvasClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
canvasClass = new CanvasView(this);
setContentView(canvasClass);
mActivePointers = new SparseArray<PointF>();
canvasClass.SetActivity(this);
}
private SparseArray<PointF> mActivePointers;
@Override
public boolean onTouchEvent(MotionEvent e) {
// MotionEvent reports input details from the touch screen
// and other input controls. In this case, you are only
// interested in events where the touch position changed.
// get pointer index from the event object
int pointerIndex = e.getActionIndex();
// get pointer ID
int pointerId = e.getPointerId(pointerIndex);
// get masked (not specific to a pointer) action
int maskedAction = e.getActionMasked();
switch (maskedAction) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN: {
// We have a new pointer. Lets add it to the list of pointers
PointF f = new PointF();
f.x = e.getX(pointerIndex);
f.y = e.getY(pointerIndex);
mActivePointers.put(pointerId, f);
for (int size = e.getPointerCount(), i = 0; i < size; i++) {
PointF point = mActivePointers.get(e.getPointerId(i));
if (point != null) {
point.x = e.getX(i);
point.y = e.getY(i);
canvasClass.sendMove(point.x, point.y);
}
}
break;
}
case MotionEvent.ACTION_MOVE: { // a pointer was moved
for (int size = e.getPointerCount(), i = 0; i < size; i++) {
PointF point = mActivePointers.get(e.getPointerId(i));
if (point != null) {
point.x = e.getX(i);
point.y = e.getY(i);
canvasClass.sendMove(point.x, point.y);
}
}
break;
}
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_CANCEL: {
// TODO use data
break;
}
}
return true;
}
@Override
public void onBackPressed()
{
canvasClass.SetGameState(false);
}
}