feat: prevent reversing directly into snake neck and play denied sound effect

This commit is contained in:
phillip committed 2026-10-01 22:11:00 +02:00
1 parent 60740a53c2
commit fd87ad3091
7 files changed
+89 -19

No files matched your search

+13
View File
@@ -14,6 +14,7 @@ void audio_setup(AudioContext &audio) {
mmLoadEffect(SFX_167126__CRISSTANZA__UNPAUSE);
mmLoadEffect(SFX_273792__PACOMAV__PLOP);
mmLoadEffect(SFX_73750__TIMBRE__REMIX_OF_BENBONCAN_SAD_TROMBONE_MORE_WAH_BRIGHT_DE_CLICKED);
mmLoadEffect(SFX_DENIED);
audio.sfx_pickup = {
{ SFX_332629__TREASURESOUNDS__ITEM_PICKUP },
@@ -62,6 +63,14 @@ void audio_setup(AudioContext &audio) {
255,
128,
};
audio.sfx_denied = {
{ SFX_DENIED },
static_cast<int>(1.0F * (1 << 10)),
5,
160,
128,
};
}
void audio_start_music(const AudioContext &audio) {
@@ -111,6 +120,10 @@ void audio_play_fail(const AudioContext &audio) {
mmEffectEx(&audio.sfx_fail);
}
void audio_play_denied(const AudioContext &audio) {
mmEffectEx(&audio.sfx_denied);
}
bool audio_toggle_music(AudioContext &audio) {
audio.musicEnabled = !audio.musicEnabled;
+66 -13
View File
@@ -4,6 +4,8 @@
#include <cstdint>
#include <cstdio>
#include "consts.h"
bool handle_input_frame(InputContext &ctx) {
scanKeys();
const uint32_t keys = keysDown();
@@ -69,25 +71,76 @@ bool handle_input_frame(InputContext &ctx) {
if (!ctx.state->paused && !ctx.state->gameOver) {
const bool cursed = ctx.state->curseTicksRemaining > 0;
uint16_t requested_dir = 0;
bool has_dir_input = false;
if ((keys != 0U) && ((keys & KEY_UP) != 0U)) {
ctx.state->snakeDirection = cursed ? 2 : 0;
draw_game(*ctx.state, *ctx.renderAssets);
requested_dir = cursed ? 2 : 0;
has_dir_input = true;
} else if ((keys != 0U) && ((keys & KEY_RIGHT) != 0U)) {
requested_dir = cursed ? 3 : 1;
has_dir_input = true;
} else if ((keys != 0U) && ((keys & KEY_DOWN) != 0U)) {
requested_dir = cursed ? 0 : 2;
has_dir_input = true;
} else if ((keys != 0U) && ((keys & KEY_LEFT) != 0U)) {
requested_dir = cursed ? 1 : 3;
has_dir_input = true;
}
if ((keys != 0U) && ((keys & KEY_RIGHT) != 0U)) {
ctx.state->snakeDirection = cursed ? 3 : 1;
draw_game(*ctx.state, *ctx.renderAssets);
}
if (has_dir_input) {
bool is_reversing = false;
if ((keys != 0U) && ((keys & KEY_DOWN) != 0U)) {
ctx.state->snakeDirection = cursed ? 0 : 2;
draw_game(*ctx.state, *ctx.renderAssets);
}
if (ctx.state->snake.size() >= 2) {
const auto &head = ctx.state->snake.front();
const auto &neck = ctx.state->snake.at(1);
int next_x = head[0];
int next_y = head[1];
if ((keys != 0U) && ((keys & KEY_LEFT) != 0U)) {
ctx.state->snakeDirection = cursed ? 1 : 3;
draw_game(*ctx.state, *ctx.renderAssets);
switch (requested_dir) {
case 0:
next_y--;
break;
case 1:
next_x++;
break;
case 2:
next_y++;
break;
case 3:
next_x--;
break;
default:
break;
}
if (next_x > X_MAX) {
next_x = 0;
} else if (next_x < 0) {
next_x = X_MAX;
}
if (next_y > Y_MAX) {
next_y = 0;
} else if (next_y < 0) {
next_y = Y_MAX;
}
if (next_x == neck[0] && next_y == neck[1]) {
is_reversing = true;
}
} else {
if ((ctx.state->snakeDirection + 2) % 4 == requested_dir) {
is_reversing = true;
}
}
if (is_reversing) {
audio_play_denied(*ctx.audio);
} else if (ctx.state->snakeDirection != requested_dir) {
ctx.state->snakeDirection = requested_dir;
draw_game(*ctx.state, *ctx.renderAssets);
}
}
}
+2 -2
View File
@@ -175,7 +175,7 @@ void do_tick() {
void init_game() {
if (hardMode) {
timerStart(0, ClockDivider_1024, TIMER_FREQ_1024(9), do_tick);
timerStart(0, ClockDivider_1024, static_cast<uint16_t>(TIMER_FREQ_1024(9)), do_tick);
setBackdropColorSub(RGB15(10,5,5));
score = 0;
snakelength = START_LENGTH_HARD;
@@ -197,7 +197,7 @@ void init_game() {
}
else
{
timerStart(0, ClockDivider_1024, TIMER_FREQ_1024(5), do_tick);
timerStart(0, ClockDivider_1024, static_cast<uint16_t>(TIMER_FREQ_1024(5)), do_tick);
setBackdropColorSub(RGB15(5,5,5));
score = 0;
snakelength = START_LENGTH;