Skip to content

Commit 2a8c391

Browse files
committed
add completed game
1 parent b0d91e4 commit 2a8c391

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

24-findme/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
A simple game to learn how to create effects with shaders.
44

5-
<!-- <p align="center">
5+
<p align="center">
66
<img src="screenshot.png" style="width:528px;max-width: 100%;">
7-
</p> -->
7+
</p>
88

99
## Features
1010

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
package com.hfad.findme
22

3+
import android.app.Dialog
4+
import androidx.appcompat.app.AlertDialog
35
import androidx.appcompat.app.AppCompatActivity
46
import android.os.Bundle
7+
import androidx.core.content.ContextCompat
58

69
class MainActivity : AppCompatActivity() {
710
override fun onCreate(savedInstanceState: Bundle?) {
811
super.onCreate(savedInstanceState)
912
setContentView(R.layout.activity_main)
13+
val dialog = createInstructionsDialog()
14+
dialog.show()
15+
}
16+
17+
private fun createInstructionsDialog(): Dialog {
18+
val builder = AlertDialog.Builder(this)
19+
builder.setIcon(R.drawable.android)
20+
.setTitle(R.string.instructions_title)
21+
.setMessage(R.string.instructions)
22+
.setPositiveButtonIcon(ContextCompat.getDrawable(this, android.R.drawable.ic_media_play))
23+
return builder.create()
1024
}
1125
}

24-findme/app/src/main/java/com/hfad/findme/SpotlightImageView.kt

+57-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package com.hfad.findme
33
import android.content.Context
44
import android.graphics.*
55
import android.util.AttributeSet
6+
import android.view.MotionEvent
67
import androidx.appcompat.widget.AppCompatImageView
8+
import kotlin.math.floor
9+
import kotlin.random.Random
710

811
class SpotlightImageView @JvmOverloads constructor(
912
context: Context, attrs: AttributeSet? = null
@@ -23,7 +26,7 @@ class SpotlightImageView @JvmOverloads constructor(
2326
private val spotlight = BitmapFactory.decodeResource(resources, R.drawable.mask)
2427

2528
private var shader: Shader
26-
// private val shaderMatrix = Matrix()
29+
private val shaderMatrix = Matrix()
2730

2831
init {
2932
val bitmap = Bitmap.createBitmap(spotlight.width, spotlight.height, Bitmap.Config.ARGB_8888)
@@ -43,6 +46,11 @@ class SpotlightImageView @JvmOverloads constructor(
4346
paint.shader = shader
4447
}
4548

49+
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
50+
super.onSizeChanged(w, h, oldw, oldh)
51+
setupWinnerRect()
52+
}
53+
4654
override fun onDraw(canvas: Canvas) {
4755
super.onDraw(canvas)
4856
// Draw the BitmapShader texture
@@ -54,5 +62,53 @@ class SpotlightImageView @JvmOverloads constructor(
5462
)
5563
shader.setLocalMatrix(shaderMatrix)
5664
canvas.drawRect(0.0f, 0.0f, width.toFloat(), height.toFloat() / 2, paint)*/
65+
66+
// Draw the Android image
67+
canvas.drawColor(Color.WHITE)
68+
canvas.drawBitmap(bitmapAndroid, androidBitmapX, androidBitmapY, paint)
69+
70+
if (!gameOver) {
71+
if (shouldDrawSpotlight) {
72+
canvas.drawRect(0.0f, 0.0f, width.toFloat(), height.toFloat(), paint)
73+
} else {
74+
canvas.drawColor(Color.BLACK)
75+
}
76+
}
77+
}
78+
79+
override fun onTouchEvent(motionEvent: MotionEvent): Boolean {
80+
val motionEventX = motionEvent.x
81+
val motionEventY = motionEvent.y
82+
when (motionEvent.action) {
83+
MotionEvent.ACTION_DOWN -> {
84+
shouldDrawSpotlight = true
85+
if (gameOver) {
86+
gameOver = false
87+
setupWinnerRect()
88+
}
89+
}
90+
MotionEvent.ACTION_UP -> {
91+
shouldDrawSpotlight = false
92+
gameOver = winnerRect.contains(motionEventX, motionEventY)
93+
}
94+
}
95+
shaderMatrix.setTranslate(
96+
motionEventX - spotlight.width / 2.0f,
97+
motionEventY - spotlight.height / 2.0f
98+
)
99+
shader.setLocalMatrix(shaderMatrix)
100+
invalidate() // redraw
101+
return true
102+
}
103+
104+
private fun setupWinnerRect() {
105+
androidBitmapX = floor(Random.nextFloat() * (width - bitmapAndroid.width))
106+
androidBitmapY = floor(Random.nextFloat() * (height - bitmapAndroid.height))
107+
winnerRect = RectF(
108+
(androidBitmapX),
109+
(androidBitmapY),
110+
(androidBitmapX + bitmapAndroid.width),
111+
(androidBitmapY + bitmapAndroid.height)
112+
)
57113
}
58114
}

24-findme/screenshot.png

150 KB
Loading

0 commit comments

Comments
 (0)