/
paddle.cpp
104 lines (85 loc) · 2.57 KB
/
paddle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//=============================================================================
// *** Arduino Pong ***
// Copyright (c) Robert Grasböck, All rights reserved.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3.0 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library.
//=============================================================================
/// @file paddle.cpp
/// @author Robert Grasböck (robert.grasboeck@gmail.com)
/// @date November, 2015
/// @brief Paddle class
//=============================================================================
#include <Arduino.h>
#include "tv.h"
#include "pong.h"
#include "paddle.h"
Paddle::Paddle() :
autoplay(false),
xpos(0), ypos(0) {
}
void Paddle::init(uint8_t x, uint8_t y, uint8_t p, bool autoplay) {
xpos = x;
ypos = y;
paddle = p;
this->autoplay = autoplay;
}
void Paddle::update() {
if (autoplay == false) {
uint16_t val = pong.PaddlePos(paddle);
// sub offset of resistor network and divide to a rage of 6
uint16_t calc = (val - 460) / 6;
// sanity check
if (calc > TV.vres() - HEIGHT)
calc = TV.vres() - HEIGHT;
ypos = calc + HEIGHT / 2;
// debug
// TV.print(20 + 80*paddle, 80, (int)ypos);
// TV.print(20 + 80 * paddle, 85, val);
// TV.print(20 + 80*paddle, 70, (int)left());
// TV.print(20 + 80*paddle, 75, (int)left());
// TV.print(20 + 80*paddle, 80, (int)top());
// TV.print(20 + 80*paddle, 85, (int)bottom());
} else {
ypos = ballpos;
if (ypos > TV.vres() - HEIGHT / 2)
ypos = TV.vres() - HEIGHT / 2;
else if (ypos < HEIGHT / 2)
ypos = HEIGHT / 2;
}
TV.draw_rect(xpos - WIDTH / 2,
ypos - HEIGHT / 2,
WIDTH - 1,
HEIGHT - 1, 1);
}
void Paddle::setBallPos(uint8_t y) {
ballpos = y;
}
int16_t Paddle::x() {
return xpos;
}
int16_t Paddle::y() {
return ypos;
}
int16_t Paddle::left() {
return x() - WIDTH / 2;
}
int16_t Paddle::right() {
return x() + WIDTH / 2;
}
int16_t Paddle::top() {
return y() - HEIGHT / 2;
}
int16_t Paddle::bottom() {
return y() + HEIGHT / 2;
}