1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 00:57:44 +00:00

Games: Move to Userland/Games/

This commit is contained in:
Andreas Kling 2021-01-12 12:03:28 +01:00
parent b8d6a56fa3
commit aa939c4b4b
49 changed files with 1 additions and 1 deletions

View file

@ -0,0 +1,8 @@
set(SOURCES
main.cpp
Game.cpp
LevelSelectDialog.cpp
)
serenity_app(Breakout ICON app-breakout)
target_link_libraries(Breakout LibGUI)

View file

@ -0,0 +1,328 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Game.h"
#include "LevelSelectDialog.h"
#include <LibGUI/Application.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Font.h>
#include <LibGfx/StandardCursor.h>
namespace Breakout {
Game::Game()
{
set_override_cursor(Gfx::StandardCursor::Hidden);
auto level_dialog = LevelSelectDialog::show(m_board, window());
if (level_dialog != GUI::Dialog::ExecOK)
m_board = -1;
set_paused(false);
start_timer(16);
reset();
}
Game::~Game()
{
}
void Game::reset_paddle()
{
m_paddle.moving_left = false;
m_paddle.moving_right = false;
m_paddle.rect = { game_width / 2 - 40, game_height - 20, 80, 16 };
}
void Game::reset()
{
m_lives = 3;
m_pause_count = 0;
m_cheater = false;
reset_ball();
reset_paddle();
generate_bricks();
}
void Game::generate_bricks()
{
m_bricks = {};
Gfx::Color colors[] = {
Gfx::Color::Red,
Gfx::Color::Green,
Gfx::Color::Blue,
Gfx::Color::Yellow,
Gfx::Color::Magenta,
Gfx::Color::Cyan,
Gfx::Color::LightGray,
};
Vector<Brick> boards[] = {
// :^)
Vector({
Brick(0, 0, colors[3], 40, 12, 100),
Brick(0, 4, colors[3], 40, 12, 100),
Brick(1, 2, colors[3], 40, 12, 100),
Brick(1, 5, colors[3], 40, 12, 100),
Brick(2, 1, colors[3], 40, 12, 100),
Brick(2, 3, colors[3], 40, 12, 100),
Brick(2, 6, colors[3], 40, 12, 100),
Brick(3, 6, colors[3], 40, 12, 100),
Brick(4, 0, colors[3], 40, 12, 100),
Brick(4, 6, colors[3], 40, 12, 100),
Brick(5, 6, colors[3], 40, 12, 100),
Brick(6, 5, colors[3], 40, 12, 100),
Brick(7, 4, colors[3], 40, 12, 100),
})
};
if (m_board != -1) {
m_bricks = boards[m_board];
} else {
// Rainbow
for (int row = 0; row < 7; ++row) {
for (int column = 0; column < 10; ++column) {
Brick brick(row, column, colors[row]);
m_bricks.append(brick);
}
}
}
}
void Game::set_paused(bool paused)
{
m_paused = paused;
if (m_paused) {
set_override_cursor(Gfx::StandardCursor::None);
m_pause_count++;
} else {
set_override_cursor(Gfx::StandardCursor::Hidden);
}
update();
}
void Game::timer_event(Core::TimerEvent&)
{
if (m_paused)
return;
tick();
}
void Game::paint_event(GUI::PaintEvent& event)
{
GUI::Painter painter(*this);
painter.add_clip_rect(event.rect());
painter.fill_rect(rect(), Color::Black);
painter.fill_ellipse(enclosing_int_rect(m_ball.rect()), Color::Red);
painter.fill_rect(enclosing_int_rect(m_paddle.rect), Color::White);
for (auto& brick : m_bricks) {
if (!brick.dead)
painter.fill_rect(enclosing_int_rect(brick.rect), brick.color);
}
int msg_width = font().width(String::formatted("Lives: {}", m_lives));
int msg_height = font().glyph_height();
painter.draw_text({ (game_width - msg_width - 2), 2, msg_width, msg_height }, String::formatted("Lives: {}", m_lives), Gfx::TextAlignment::Center, Color::White);
if (m_paused) {
const char* msg = m_cheater ? "C H E A T E R" : "P A U S E D";
int msg_width = font().width(msg);
int msg_height = font().glyph_height();
painter.draw_text({ (game_width / 2) - (msg_width / 2), (game_height / 2) - (msg_height / 2), msg_width, msg_height }, msg, Gfx::TextAlignment::Center, Color::White);
}
}
void Game::keyup_event(GUI::KeyEvent& event)
{
if (m_paused)
return;
switch (event.key()) {
case Key_Left:
m_paddle.moving_left = false;
break;
case Key_Right:
m_paddle.moving_right = false;
break;
default:
break;
}
}
void Game::keydown_event(GUI::KeyEvent& event)
{
if (m_paused)
return;
switch (event.key()) {
case Key_Escape:
GUI::Application::the()->quit();
break;
case Key_Left:
m_paddle.moving_left = true;
break;
case Key_Right:
m_paddle.moving_right = true;
break;
default:
break;
}
}
void Game::mousemove_event(GUI::MouseEvent& event)
{
if (m_paused)
return;
float new_paddle_x = event.x() - m_paddle.rect.width() / 2;
new_paddle_x = max(0.0f, new_paddle_x);
new_paddle_x = min(game_width - m_paddle.rect.width(), new_paddle_x);
m_paddle.rect.set_x(new_paddle_x);
}
void Game::reset_ball()
{
int position_x_min = (game_width / 2) - 50;
int position_x_max = (game_width / 2) + 50;
int position_x = arc4random() % (position_x_max - position_x_min + 1) + position_x_min;
int position_y = 200;
int velocity_x = arc4random() % 3 + 1;
int velocity_y = 3 + (3 - velocity_x);
if (arc4random() % 2)
velocity_x = velocity_x * -1;
m_ball = {};
m_ball.position = { position_x, position_y };
m_ball.velocity = { velocity_x, velocity_y };
}
void Game::hurt()
{
stop_timer();
m_lives--;
if (m_lives <= 0) {
update();
GUI::MessageBox::show(window(), "You lose!", "Breakout", GUI::MessageBox::Type::Information, GUI::MessageBox::InputType::OK);
reset();
}
sleep(1);
reset_ball();
reset_paddle();
start_timer(16);
}
void Game::win()
{
stop_timer();
update();
if (m_cheater) {
GUI::MessageBox::show(window(), "You cheated not only the game, but yourself.", "Breakout", GUI::MessageBox::Type::Information, GUI::MessageBox::InputType::OK);
} else {
GUI::MessageBox::show(window(), "You win!", "Breakout", GUI::MessageBox::Type::Information, GUI::MessageBox::InputType::OK);
}
reset();
start_timer(16);
}
void Game::tick()
{
auto new_ball = m_ball;
new_ball.position += new_ball.velocity;
if (new_ball.x() < new_ball.radius || new_ball.x() > game_width - new_ball.radius) {
new_ball.position.set_x(m_ball.x());
new_ball.velocity.set_x(new_ball.velocity.x() * -1);
}
if (new_ball.y() < new_ball.radius) {
new_ball.position.set_y(m_ball.y());
new_ball.velocity.set_y(new_ball.velocity.y() * -1);
}
if (new_ball.y() > game_height - new_ball.radius) {
hurt();
return;
}
if (new_ball.rect().intersects(m_paddle.rect)) {
new_ball.position.set_y(m_ball.y());
new_ball.velocity.set_y(new_ball.velocity.y() * -1);
float distance_to_middle_of_paddle = new_ball.x() - m_paddle.rect.center().x();
float relative_impact_point = distance_to_middle_of_paddle / m_paddle.rect.width();
new_ball.velocity.set_x(relative_impact_point * 7);
}
for (auto& brick : m_bricks) {
if (brick.dead)
continue;
if (new_ball.rect().intersects(brick.rect)) {
brick.dead = true;
auto overlap = new_ball.rect().intersected(brick.rect);
if (overlap.width() < overlap.height()) {
new_ball.position.set_x(m_ball.x());
new_ball.velocity.set_x(new_ball.velocity.x() * -1);
} else {
new_ball.position.set_y(m_ball.y());
new_ball.velocity.set_y(new_ball.velocity.y() * -1);
}
break;
}
}
bool has_live_bricks = false;
for (auto& brick : m_bricks) {
if (!brick.dead) {
has_live_bricks = true;
break;
}
}
if (!has_live_bricks) {
win();
return;
}
if (m_paddle.moving_left) {
m_paddle.rect.set_x(max(0.0f, m_paddle.rect.x() - m_paddle.speed));
}
if (m_paddle.moving_right) {
m_paddle.rect.set_x(min(game_width - m_paddle.rect.width(), m_paddle.rect.x() + m_paddle.speed));
}
m_ball = new_ball;
if (m_pause_count > 50)
m_cheater = true;
update();
}
}

View file

@ -0,0 +1,109 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibGUI/Widget.h>
namespace Breakout {
class Game final : public GUI::Widget {
C_OBJECT(Game);
public:
static const int game_width = 480;
static const int game_height = 500;
virtual ~Game() override;
void set_paused(bool paused);
private:
Game();
virtual void paint_event(GUI::PaintEvent&) override;
virtual void keyup_event(GUI::KeyEvent&) override;
virtual void keydown_event(GUI::KeyEvent&) override;
virtual void mousemove_event(GUI::MouseEvent&) override;
virtual void timer_event(Core::TimerEvent&) override;
void reset();
void reset_ball();
void reset_paddle();
void generate_bricks();
void tick();
void hurt();
void win();
struct Ball {
Gfx::FloatPoint position;
Gfx::FloatPoint velocity;
float radius { 8 };
float x() const { return position.x(); }
float y() const { return position.y(); }
Gfx::FloatRect rect() const
{
return { x() - radius, y() - radius, radius * 2, radius * 2 };
}
};
struct Paddle {
Gfx::FloatRect rect;
float speed { 5 };
bool moving_left { false };
bool moving_right { false };
};
struct Brick {
Gfx::FloatRect rect;
Gfx::Color color;
bool dead { false };
Brick(int row, int column, Gfx::Color c, int brick_width = 40, int brick_height = 12, int field_left_offset = 30, int field_top_offset = 30, int brick_spacing = 3)
{
rect = {
field_left_offset + (column * brick_width) + (column * brick_spacing),
field_top_offset + (row * brick_height) + (row * brick_spacing),
brick_width,
brick_height
};
color = c;
}
};
bool m_paused;
int m_lives;
int m_board;
long m_pause_count;
bool m_cheater;
Ball m_ball;
Paddle m_paddle;
Vector<Brick> m_bricks;
};
}

View file

@ -0,0 +1,82 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "LevelSelectDialog.h"
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/Label.h>
#include <LibGUI/ListView.h>
namespace Breakout {
LevelSelectDialog::LevelSelectDialog(Window* parent_window)
: Dialog(parent_window)
{
set_rect(0, 0, 300, 250);
set_title("Level Select");
build();
}
LevelSelectDialog::~LevelSelectDialog()
{
}
int LevelSelectDialog::show(int& board_number, Window* parent_window)
{
auto box = LevelSelectDialog::construct(parent_window);
box->set_resizable(false);
if (parent_window)
box->set_icon(parent_window->icon());
auto result = box->exec();
board_number = box->level();
return result;
}
void LevelSelectDialog::build()
{
auto& main_widget = set_main_widget<GUI::Widget>();
main_widget.set_fill_with_background_color(true);
auto& layout = main_widget.set_layout<GUI::VerticalBoxLayout>();
layout.set_margins({ 4, 4, 4, 4 });
main_widget.add<GUI::Label>("Choose a level").set_text_alignment(Gfx::TextAlignment::Center);
auto& level_list = main_widget.add<GUI::Widget>();
auto& scroll_layout = level_list.set_layout<GUI::VerticalBoxLayout>();
scroll_layout.set_spacing(4);
level_list.add<GUI::Button>("Rainbow").on_click = [this](auto) {
m_level = -1;
done(Dialog::ExecOK);
};
level_list.add<GUI::Button>(":^)").on_click = [this](auto) {
m_level = 0;
done(Dialog::ExecOK);
};
}
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibGUI/Dialog.h>
namespace Breakout {
class LevelSelectDialog : public GUI::Dialog {
C_OBJECT(LevelSelectDialog)
public:
virtual ~LevelSelectDialog() override;
static int show(int& board_number, Window* parent_window);
int level() const { return m_level; }
private:
explicit LevelSelectDialog(Window* parent_window);
void build();
int m_level;
};
}

View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Game.h"
#include <LibGUI/Application.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Menu.h>
#include <LibGUI/MenuBar.h>
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
int main(int argc, char** argv)
{
if (pledge("stdio rpath wpath cpath shared_buffer accept unix fattr", nullptr) < 0) {
perror("pledge");
return 1;
}
auto app = GUI::Application::construct(argc, argv);
if (pledge("stdio rpath shared_buffer", nullptr) < 0) {
perror("pledge");
return 1;
}
if (unveil("/res", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil(nullptr, nullptr) < 0) {
perror("unveil");
return 1;
}
auto window = GUI::Window::construct();
window->resize(Breakout::Game::game_width, Breakout::Game::game_height);
window->set_resizable(false);
window->set_double_buffering_enabled(false);
window->set_title("Breakout");
auto app_icon = GUI::Icon::default_icon("app-breakout");
window->set_icon(app_icon.bitmap_for_size(16));
auto& game = window->set_main_widget<Breakout::Game>();
window->show();
auto menubar = GUI::MenuBar::construct();
auto& app_menu = menubar->add_menu("Breakout");
app_menu.add_action(GUI::Action::create_checkable("Pause", { {}, Key_P }, [&](auto& action) {
game.set_paused(action.is_checked());
return;
}));
app_menu.add_separator();
app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
GUI::Application::the()->quit();
return;
}));
auto& help_menu = menubar->add_menu("Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Breakout", app_icon, window));
app->set_menubar(move(menubar));
return app->exec();
}