Add bounce sound; update submodule

This commit is contained in:
shylie 2025-06-08 09:32:56 -04:00
parent f26f2163e9
commit c34f09dce1
4 changed files with 51 additions and 7 deletions

View File

@ -8,7 +8,9 @@ pico_sdk_init()
add_library(common INTERFACE) add_library(common INTERFACE)
target_compile_definitions(common INTERFACE PICO_XOSC_STARTUP_DELAY_MULTIPLIER=128) target_compile_definitions(common INTERFACE PICO_XOSC_STARTUP_DELAY_MULTIPLIER=128)
target_link_libraries(common INTERFACE pico_stdlib)
add_subdirectory(picoled) add_subdirectory(picoled)
add_subdirectory(pong) add_subdirectory(pong)
add_subdirectory(gfx2d)

@ -1 +1 @@
Subproject commit 8115ae141217b05512ff59833360bbd5070a1ec2 Subproject commit 10e9176d13b249e2705745a441aa333d8a2c1823

View File

@ -4,7 +4,7 @@ add_executable(pong
src/main.cpp src/main.cpp
) )
target_link_libraries(pong common picoled) target_link_libraries(pong common picoled hardware_pwm)
pico_enable_stdio_uart(pong 0) pico_enable_stdio_uart(pong 0)
pico_enable_stdio_usb(pong 1) pico_enable_stdio_usb(pong 1)

View File

@ -3,6 +3,8 @@
#include <pico/stdio_usb.h> #include <pico/stdio_usb.h>
#include <pico/stdlib.h> #include <pico/stdlib.h>
#include <pico/time.h>
#include <hardware/pwm.h>
#include <picoled/SSD1306.h> #include <picoled/SSD1306.h>
@ -19,7 +21,7 @@ struct rect
{ {
for (int j = 0; j < h; j++) for (int j = 0; j < h; j++)
{ {
oled(x + i, y + j) = 0xFF; oled.write(x + i, y + j, 0xFF);
} }
} }
} }
@ -68,6 +70,21 @@ constexpr uint8_t OLED_ADDRESS = 0x3C;
constexpr float PADDLE_SPEED = 20; constexpr float PADDLE_SPEED = 20;
constexpr float INITIAL_BALL_SPEED = 32; constexpr float INITIAL_BALL_SPEED = 32;
constexpr uint8_t BUZZER_PIN = 2;
int64_t unbeep(alarm_id_t, void*)
{
pwm_set_enabled(pwm_gpio_to_slice_num(BUZZER_PIN), false);
return 0;
}
void beep()
{
pwm_set_enabled(pwm_gpio_to_slice_num(BUZZER_PIN), true);
add_alarm_in_ms(40, unbeep, nullptr, false);
}
} }
int main() int main()
@ -94,20 +111,28 @@ int main()
int64_t us = time_us_32(); int64_t us = time_us_32();
pwm_config config = pwm_get_default_config();
pwm_config_set_wrap(&config, 0x6000);
pwm_config_set_clkdiv(&config, 2.6f);
pwm_init(pwm_gpio_to_slice_num(BUZZER_PIN), &config, false);
pwm_set_gpio_level(BUZZER_PIN, 0x3000);
gpio_set_function(BUZZER_PIN, GPIO_FUNC_PWM);
while (true) while (true)
{ {
oled.clear(); oled.clear();
for (int i = 0; i < oled.get_width(); i++) for (int i = 0; i < oled.get_width(); i++)
{ {
oled(i, 0) = 0xFF; oled.write(i, 0, 0xFF);
oled(i, oled.get_height() - 1) = 0xFF; oled.write(i, oled.get_height() - 1, 0xFF);
} }
for (int i = 0; i < oled.get_height(); i++) for (int i = 0; i < oled.get_height(); i++)
{ {
oled(0, i) = 0xFF; oled.write(0, i, 0xFF);
oled(oled.get_width() - 1, i) = 0xFF; oled.write(oled.get_width() - 1, i, 0xFF);
} }
int64_t current = time_us_32(); int64_t current = time_us_32();
@ -130,6 +155,8 @@ int main()
if (ball.overlaps(player_top) || ball.overlaps(player_bottom)) if (ball.overlaps(player_top) || ball.overlaps(player_bottom))
{ {
ball_dy = -ball_dy; ball_dy = -ball_dy;
beep();
} }
else if (ball.overlaps(player)) else if (ball.overlaps(player))
{ {
@ -142,6 +169,8 @@ int main()
ball_dx = cosf(angle + angle_delta); ball_dx = cosf(angle + angle_delta);
ball_dy = sinf(angle + angle_delta); ball_dy = sinf(angle + angle_delta);
beep();
} }
const rect ai_top = { ai.left() + 2, ai.top(), ai.w - 2, 1 }; const rect ai_top = { ai.left() + 2, ai.top(), ai.w - 2, 1 };
@ -149,6 +178,8 @@ int main()
if (ball.overlaps(ai_top) || ball.overlaps(ai_bottom)) if (ball.overlaps(ai_top) || ball.overlaps(ai_bottom))
{ {
ball_dy = -ball_dy; ball_dy = -ball_dy;
beep();
} }
else if (ball.overlaps(ai)) else if (ball.overlaps(ai))
{ {
@ -162,28 +193,38 @@ int main()
ball_dx = cosf(angle + angle_delta); ball_dx = cosf(angle + angle_delta);
ball_dy = sinf(angle + angle_delta); ball_dy = sinf(angle + angle_delta);
beep();
} }
if (ball.left() < 0) if (ball.left() < 0)
{ {
ball_dx = -ball_dx; ball_dx = -ball_dx;
ball.x = 0; ball.x = 0;
beep();
} }
if (ball.right() >= oled.get_width()) if (ball.right() >= oled.get_width())
{ {
ball_dx = -ball_dx; ball_dx = -ball_dx;
ball.x = oled.get_width() - ball.w; ball.x = oled.get_width() - ball.w;
beep();
} }
if (ball.top() < 0) if (ball.top() < 0)
{ {
ball_dy = -ball_dy; ball_dy = -ball_dy;
ball.y = 0; ball.y = 0;
beep();
} }
if (ball.bottom() >= oled.get_height()) if (ball.bottom() >= oled.get_height())
{ {
ball_dy = -ball_dy; ball_dy = -ball_dy;
ball.y = oled.get_height() - ball.h; ball.y = oled.get_height() - ball.h;
beep();
} }
player.draw(oled); player.draw(oled);
@ -191,5 +232,6 @@ int main()
ball.draw(oled); ball.draw(oled);
oled.update(); oled.update();
oled.swap_buffers();
} }
} }