diff --git a/programs/pong/src/main.cpp b/programs/pong/src/main.cpp index 40dc6d5..7806d04 100644 --- a/programs/pong/src/main.cpp +++ b/programs/pong/src/main.cpp @@ -1,5 +1,6 @@ #include "devicelib.h" +#include #include #include #include @@ -20,10 +21,14 @@ void draw_rectangle(float x, float y, float w, float h, uint8_t r, uint8_t g, int hi = h; display.set_update_area(x - w / 2, y - h / 2, wi, hi); + const uint8_t buf[24] = { + r, g, b, r, g, b, r, g, b, r, g, b, r, g, b, r, g, b, r, g, b, r, g, b, + }; + auto pixels = display.pixels(); - for (int i = 0; i < (wi + 1) * (hi + 1); i++) + for (int i = 0; i < (wi + 1) * (hi + 1); i += 8) { - pixels.write(r, g, b); + pixels.write(buf, sizeof(buf)); } } @@ -61,15 +66,21 @@ int main() absolute_time_t previous_time = get_absolute_time(); float time_since_last_display = 0; - constexpr float SPEED = 100; + constexpr float SPEED = 75; float paddle_x = 120; constexpr float paddle_y = 280; + constexpr float BALL_SPEED = 125; + float ball_x = 120; + float ball_y = 160; + + float INITIAL_BALL_ANGLE = -3.1415926f / 4.0f; + float ball_dx = cosf(INITIAL_BALL_ANGLE); + float ball_dy = sinf(INITIAL_BALL_ANGLE); + uint8_t left_history = 0; uint8_t right_history = 0; - bool moved = true; - while (true) { tud_task(); @@ -77,6 +88,34 @@ int main() float dt = absolute_time_diff_us(previous_time, current) / 1000000.0f; previous_time = current; + float old_ball_pos_x = ball_x; + float old_ball_pos_y = ball_y; + + ball_x += ball_dx * BALL_SPEED * dt; + ball_y += ball_dy * BALL_SPEED * dt; + + if (ball_x - 2 <= 0 || ball_x + 2 >= 240) + { + ball_dx = -ball_dx; + ball_x += ball_dx * 2; + } + if (ball_y - 2 <= 0 || ball_y + 2 >= 320) + { + ball_dy = -ball_dy; + ball_y += ball_dy * 2; + } + + if (ball_x + 2 >= paddle_x - 20 && ball_x - 2 <= paddle_x + 20 + && ball_y + 3 >= paddle_y - 2 && ball_y - 3 <= paddle_y - 2) + { + float ball_angle = atan2f(ball_dy, ball_dx); + float diff = paddle_x - ball_x; + ball_angle += diff / 50.0f; + ball_dx = cosf(ball_angle); + ball_dy = -sinf(ball_angle); + ball_y = paddle_y - 6; + } + float old_pos = paddle_x; left_history = (left_history << 1) | !gpio_get(BUTTON_LEFT); @@ -85,21 +124,24 @@ int main() if (left_history == 0xFF) { paddle_x -= SPEED * dt; - moved = true; } if (right_history == 0xFF) { paddle_x += SPEED * dt; - moved = true; } time_since_last_display += dt; - if (moved && time_since_last_display > 0.016f) + if (time_since_last_display > 0.02f) { - moved = false; - draw_rectangle(old_pos, paddle_y, 50, 7, 0, 0, 0); + // ball + draw_rectangle(old_ball_pos_x, old_ball_pos_y, 12, 12, 0, 0, 0); + draw_rectangle(ball_x, ball_y, 4, 4, 0xFF, 0xFF, 0xFF); + + // player paddle + draw_rectangle(old_pos - 20, paddle_y, 6, 7, 0, 0, 0); + draw_rectangle(old_pos + 20, paddle_y, 6, 7, 0, 0, 0); draw_rectangle(paddle_x, paddle_y, 40, 5, 0xFF, 0xFF, 0xFF); time_since_last_display = 0; } diff --git a/src/usb_descriptors.c b/src/usb_descriptors.c index a6da856..6eb2f11 100644 --- a/src/usb_descriptors.c +++ b/src/usb_descriptors.c @@ -46,7 +46,10 @@ enum }; const char* string_desc_arr[] - = { (const char[]){ 0x09, 0x04 }, "shylie.info", "MtG Token", NULL }; + = { [STRID_LANGID] = (const char[]){ 0x09, 0x04 }, + [STRID_MANUFACTURER] = "shylie.info", + [STRID_PRODUCT] = "card device", + [STRID_SERIAL] = NULL }; const uint16_t* tud_descriptor_string_cb(uint8_t index, uint16_t langid) {