Skip to content

Commit 30c7189

Browse files
DeepMindcopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 809382380
1 parent 8270470 commit 30c7189

File tree

17 files changed

+1259
-0
lines changed

17 files changed

+1259
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2025 DeepMind Technologies Limited.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
module(
16+
name = "catch",
17+
version = "0.1.0",
18+
)
19+
20+
bazel_dep(name = "rules_kotlin", version = "2.1.8")
21+
bazel_dep(name = "rules_jvm_external", version = "6.8")
22+
23+
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
24+
maven.install(
25+
artifacts = [
26+
"joda-time:joda-time:2.12.7",
27+
],
28+
repositories = [
29+
"https://repo1.maven.org/maven2",
30+
],
31+
)
32+
use_repo(maven, "maven")
33+
34+
bazel_dep(name = "rules_android", version = "0.6.6")
35+
36+
remote_android_extensions = use_extension(
37+
"@rules_android//bzlmod_extensions:android_extensions.bzl",
38+
"remote_android_tools_extensions",
39+
)
40+
use_repo(remote_android_extensions, "android_tools")
41+
42+
android_sdk_repository_extension = use_extension("@rules_android//rules/android_sdk_repository:rule.bzl", "android_sdk_repository_extension")
43+
use_repo(android_sdk_repository_extension, "androidsdk")

android_env/apps/java/com/google/androidenv/MODULE.bazel.lock

Lines changed: 401 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025 DeepMind Technologies Limited.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
<?xml version="1.0" encoding="utf-8"?>
16+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17+
xmlns:tools="http://schemas.android.com/tools"
18+
package="com.google.androidenv.catch">
19+
<uses-sdk android:minSdkVersion="26"
20+
android:targetSdkVersion="35"/>
21+
<application
22+
android:allowBackup="false"
23+
android:label="@string/app_name"
24+
android:supportsRtl="false"
25+
android:taskAffinity=""
26+
tools:ignore="AllowBackup">
27+
<activity
28+
android:name=".MainActivity"
29+
android:configChanges="orientation|keyboardHidden|screenSize"
30+
android:exported="true"
31+
android:hardwareAccelerated="true">
32+
<intent-filter>
33+
<action android:name="android.intent.action.MAIN" />
34+
<category android:name="android.intent.category.LAUNCHER" />
35+
</intent-filter>
36+
</activity>
37+
</application>
38+
</manifest>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2025 DeepMind Technologies Limited.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
# Classic RL task implemented as an Android app.
16+
17+
load("@rules_android//rules:rules.bzl", "android_binary")
18+
load("@rules_kotlin//kotlin:android.bzl", "kt_android_library")
19+
20+
package(
21+
default_applicable_licenses = ["//third_party/py/android_env:license"],
22+
default_visibility = ["//catch:__subpackages__"],
23+
)
24+
25+
licenses(["notice"])
26+
27+
# Build targets will slowly be added to this project and ultimately we'll have
28+
# an android_binary() target that'll contain the app.
29+
kt_android_library(
30+
name = "GameLogic",
31+
srcs = ["GameLogic.kt"],
32+
deps = [
33+
"//catch/sprite:Background",
34+
"//catch/sprite:Ball",
35+
"//catch/sprite:LineSegment",
36+
"//catch/sprite:Paddle",
37+
"@maven//:joda_time_joda_time",
38+
],
39+
)
40+
41+
kt_android_library(
42+
name = "RenderThread",
43+
srcs = ["RenderThread.kt"],
44+
deps = [
45+
":GameLogic",
46+
"@maven//:joda_time_joda_time",
47+
],
48+
)
49+
50+
kt_android_library(
51+
name = "MainActivity",
52+
srcs = ["MainActivity.kt"],
53+
manifest = "AndroidManifest.xml",
54+
resource_files = glob(["res/**"]),
55+
deps = [
56+
":GameLogic",
57+
":GameLogicThread",
58+
":RenderThread",
59+
"//catch/sprite:Background",
60+
"//catch/sprite:Ball",
61+
"//catch/sprite:Paddle",
62+
],
63+
)
64+
65+
android_binary(
66+
name = "app",
67+
manifest = "AndroidManifest.xml",
68+
multidex = "native",
69+
deps = [":MainActivity"],
70+
)
71+
72+
kt_android_library(
73+
name = "GameLogicThread",
74+
srcs = ["GameLogicThread.kt"],
75+
deps = [
76+
":GameLogic",
77+
],
78+
)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2025 DeepMind Technologies Limited.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.androidenv.catch
16+
17+
import android.graphics.Canvas
18+
import android.view.MotionEvent
19+
import com.google.androidenv.catch.sprite.Background
20+
import com.google.androidenv.catch.sprite.Ball
21+
import com.google.androidenv.catch.sprite.LineSegment
22+
import com.google.androidenv.catch.sprite.Paddle
23+
import kotlin.random.Random
24+
import org.joda.time.Duration
25+
import org.joda.time.Instant
26+
27+
/** The class that contains the game logic. */
28+
open class GameLogic(
29+
// Expected number of frames per second.
30+
fps: Int = 60,
31+
// Pseudo random number generator.
32+
private val rand: Random = Random.Default,
33+
// Width and height of the game in pixels.
34+
private val width: Int,
35+
private val height: Int,
36+
// UI objects in the game.
37+
private var background: Background = Background(),
38+
private var ball: Ball = Ball(maxX = width, maxY = height, rand = rand),
39+
private var paddle: Paddle = Paddle(maxX = width, y = height),
40+
) {
41+
42+
private val sleepTime: Duration = Duration.millis((1000.0 / fps).toLong())
43+
44+
/** Reinitializes the state of the game. */
45+
// Need to make this open to allow for testing.
46+
open fun reset() {
47+
this.ball.reset()
48+
}
49+
50+
/** Runs one "throw" of a [ball] that needs to be caught by the [paddle]. */
51+
// Need to make this open to allow for testing.
52+
open fun run(): Boolean {
53+
var lastTimestamp = Instant.now()
54+
do {
55+
Thread.sleep(sleepTime.millis)
56+
val now = Instant.now()
57+
val interval = Duration(lastTimestamp, now)
58+
lastTimestamp = now
59+
ball.update(interval)
60+
} while (!ball.isOutOfBounds())
61+
62+
return ball.intersects(LineSegment(paddle.topLeft(), paddle.topRight()))
63+
}
64+
65+
/** Processes a user event (e.g. a touchscreen event) and updates the [paddle] accordingly. */
66+
fun handleTouch(event: MotionEvent) {
67+
paddle.x = event.x.toInt()
68+
}
69+
70+
/** Renders the game on [c]. */
71+
open fun render(c: Canvas) {
72+
background.draw(c)
73+
ball.draw(c)
74+
paddle.draw(c)
75+
}
76+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2025 DeepMind Technologies Limited.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.androidenv.catch
16+
17+
import android.util.Log
18+
19+
/** A thread that continuously runs the game logic, resetting after each internal [run()]. */
20+
class GameLogicThread(private val game: GameLogic, private val loggingTag: String) : Thread() {
21+
22+
/** Whether this thread should continuously run. */
23+
private var shouldRun: Boolean = true
24+
/** A counter of game runs. */
25+
private var counter: Int = 0
26+
27+
/**
28+
* Lets the current [run()] iteration complete then break exit this [Thread].
29+
*
30+
* Notice that [shouldRun] cannot have a private getter with a public setter (please see
31+
* https://youtrack.jetbrains.com/issue/KT-3110 for details), hence this public function. Also
32+
* notice that we cannot call this function [stop()] since it would shadow [Thread.stop()].
33+
*/
34+
public fun finish() {
35+
shouldRun = false
36+
}
37+
38+
/** Continuously runs the [game] until [finish()] is called. */
39+
public override fun run() {
40+
while (shouldRun) {
41+
game.reset()
42+
Log.i(loggingTag, "${counter++} - ${game.run()}")
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)