Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
rust-card10
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Puzzlewolf
rust-card10
Commits
b2def381
Commit
b2def381
authored
5 years ago
by
Nora
Browse files
Options
Downloads
Patches
Plain Diff
control the paddle by accelerometer x direction
parent
37955426
No related branches found
No related tags found
No related merge requests found
Pipeline
#3522
passed
5 years ago
Stage: build
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
rkanoid/src/main.rs
+60
-3
60 additions, 3 deletions
rkanoid/src/main.rs
with
60 additions
and
3 deletions
rkanoid/src/main.rs
+
60
−
3
View file @
b2def381
...
...
@@ -162,6 +162,62 @@ enum GameResult {
LevelFinish
(
u32
),
}
struct
Input
{
buttons
:
Buttons
,
accelerometer
:
BHI160
<
Accelerometer
>
,
accel_data_x
:
f32
,
}
impl
Input
{
pub
fn
update
(
&
mut
self
)
{
self
.buttons
=
Buttons
::
read
();
for
data
in
&
self
.accelerometer
.read
()
{
self
.accel_data_x
=
data
.x
;
}
}
pub
fn
left_bottom
(
&
self
)
->
bool
{
self
.buttons
.left_bottom
()
||
self
.accel_is_left
()
}
pub
fn
right_bottom
(
&
self
)
->
bool
{
self
.buttons
.right_bottom
()
||
self
.accel_is_right
()
}
pub
fn
left_top
(
&
self
)
->
bool
{
self
.buttons
.left_top
()
}
pub
fn
right_top
(
&
self
)
->
bool
{
self
.buttons
.right_top
()
}
pub
fn
reset
(
&
self
)
->
bool
{
self
.buttons
.reset
()
}
pub
fn
new
()
->
Self
{
Input
{
buttons
:
Buttons
::
read
(),
accelerometer
:
BHI160
::
<
Accelerometer
>
::
start
(),
accel_data_x
:
Default
::
default
(),
}
}
fn
accel_is_right
(
&
self
)
->
bool
{
// technically, true if < 0.0
// false if >= 0.0
// but need static middle space
self
.accel_data_x
>
0.1
}
fn
accel_is_left
(
&
self
)
->
bool
{
self
.accel_data_x
<
-
0.1
}
}
fn
game
(
level
:
u16
,
mut
score
:
u32
)
->
GameResult
{
let
start_time
=
Seconds
::
time
();
let
display
=
Display
::
open
();
...
...
@@ -172,8 +228,9 @@ fn game(level: u16, mut score: u32) -> GameResult {
let
mut
ball_y
=
Display
::
H
-
PADDLE_HEIGHT
-
BALL_RADIUS
;
let
mut
ball_direction
=
Direction
::
UR
;
let
mut
blocks
=
Blocks
::
generate
((
0x3F
+
0x10
*
level
)
.min
(
0xff
)
as
u8
);
let
mut
input
=
Input
::
new
();
for
tick
in
0
..
{
let
input
=
Buttons
::
read
();
input
.update
();
let
old_paddle
=
paddle
;
if
input
.left_bottom
()
{
paddle
-=
PADDLE_SPEED
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment