mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 05:47:35 +00:00
Snake: Move geometry types into Geometry.h and add Direction enum
This commit is contained in:
parent
3ce87ea5f9
commit
da7c883dfa
3 changed files with 54 additions and 16 deletions
|
@ -290,7 +290,7 @@ void Game::queue_velocity(int v, int h)
|
||||||
m_velocity_queue.enqueue({ v, h });
|
m_velocity_queue.enqueue({ v, h });
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::Velocity const& Game::last_velocity() const
|
Velocity const& Game::last_velocity() const
|
||||||
{
|
{
|
||||||
if (!m_velocity_queue.is_empty())
|
if (!m_velocity_queue.is_empty())
|
||||||
return m_velocity_queue.last();
|
return m_velocity_queue.last();
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
* Copyright (c) 2022, the SerenityOS developers.
|
* Copyright (c) 2022, the SerenityOS developers.
|
||||||
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "Geometry.h"
|
||||||
#include <AK/CircularQueue.h>
|
#include <AK/CircularQueue.h>
|
||||||
#include <LibGUI/Frame.h>
|
#include <LibGUI/Frame.h>
|
||||||
|
|
||||||
|
@ -36,21 +38,6 @@ private:
|
||||||
virtual void keydown_event(GUI::KeyEvent&) override;
|
virtual void keydown_event(GUI::KeyEvent&) override;
|
||||||
virtual void timer_event(Core::TimerEvent&) override;
|
virtual void timer_event(Core::TimerEvent&) override;
|
||||||
|
|
||||||
struct Coordinate {
|
|
||||||
int row { 0 };
|
|
||||||
int column { 0 };
|
|
||||||
|
|
||||||
bool operator==(Coordinate const& other) const
|
|
||||||
{
|
|
||||||
return row == other.row && column == other.column;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Velocity {
|
|
||||||
int vertical { 0 };
|
|
||||||
int horizontal { 0 };
|
|
||||||
};
|
|
||||||
|
|
||||||
void game_over();
|
void game_over();
|
||||||
void spawn_fruit();
|
void spawn_fruit();
|
||||||
bool is_available(Coordinate const&);
|
bool is_available(Coordinate const&);
|
||||||
|
|
51
Userland/Games/Snake/Geometry.h
Normal file
51
Userland/Games/Snake/Geometry.h
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2022, the SerenityOS developers.
|
||||||
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Format.h>
|
||||||
|
|
||||||
|
namespace Snake {
|
||||||
|
|
||||||
|
enum class Direction {
|
||||||
|
Up,
|
||||||
|
Right,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Coordinate {
|
||||||
|
int row { 0 };
|
||||||
|
int column { 0 };
|
||||||
|
|
||||||
|
bool operator==(Coordinate const& other) const
|
||||||
|
{
|
||||||
|
return row == other.row && column == other.column;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Velocity {
|
||||||
|
int vertical { 0 };
|
||||||
|
int horizontal { 0 };
|
||||||
|
|
||||||
|
Direction as_direction() const
|
||||||
|
{
|
||||||
|
if (vertical > 0)
|
||||||
|
return Direction::Down;
|
||||||
|
if (vertical < 0)
|
||||||
|
return Direction::Up;
|
||||||
|
if (horizontal > 0)
|
||||||
|
return Direction::Right;
|
||||||
|
if (horizontal < 0)
|
||||||
|
return Direction::Left;
|
||||||
|
|
||||||
|
return Direction::Up;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue