Skip to content

Instantly share code, notes, and snippets.

View neslinesli93's full-sized avatar
🏠
Working from home

Tom neslinesli93

🏠
Working from home
View GitHub Profile
@neslinesli93
neslinesli93 / router.ex
Created January 2, 2020 20:34
Router addition
scope "/", BreakoutexWeb do
pipe_through :browser
get "/", GameController, :index
end
@neslinesli93
neslinesli93 / game_controller.ex
Created January 2, 2020 20:35
Main game controller
defmodule BreakoutexWeb.GameController do
use BreakoutexWeb, :controller
alias Phoenix.LiveView
alias BreakoutexWeb.Live.Game
def index(conn, _) do
opts = [
session: %{cookies: conn.cookies}
]
@neslinesli93
neslinesli93 / game.ex
Created January 2, 2020 20:36
First implementation of the game loop
defmodule BreakoutexWeb.Live.Game do
@moduledoc """
Main module, contains the entry point for the live view socket and
all the game logic
"""
use Phoenix.LiveView
alias Phoenix.LiveView.Socket
alias BreakoutexWeb.Live.Blocks
@neslinesli93
neslinesli93 / game_view.ex
Created January 2, 2020 20:39
Main game view
defmodule BreakoutexWeb.GameView do
use BreakoutexWeb, :view
end
@neslinesli93
neslinesli93 / game.css
Created January 2, 2020 20:40
CSS specific for the game
.game-container {
position: relative;
margin-top: 4rem;
/* Subtract half of the board width*/
left: calc(50% - 260px);
}
.game-container .block {
display: block;
position: absolute;
@neslinesli93
neslinesli93 / app.css
Last active January 6, 2020 13:51
Main CSS
<!-- ...other styles above -->
@import "../../deps/phoenix_live_view/assets/css/live_view.css";
@import "./game.css";
html {
background: #292a2d;
color: #a9a9b3;
}
body {
@neslinesli93
neslinesli93 / index.html.eex
Last active January 2, 2020 21:29
Expand game template
<div phx-keydown="keydown" phx-keyup="keyup" phx-target="window">
<%# Here should be the div with .game-container class %>
</div>
@neslinesli93
neslinesli93 / game.ex
Created January 2, 2020 20:55
Add basic event handlers to game module
@spec handle_event(String.t(), map(), Socket.t()) :: {:noreply, Socket.t()} | {:stop, Socket.t()}
def handle_event("keydown", payload, socket) do
IO.puts("Received keydown event")
IO.inspect(payload, label: "Payload for keydown")
{:noreply, socket}
end
def handle_event("keyup", payload, socket) do
IO.puts("Received keyup event")
@neslinesli93
neslinesli93 / output.txt
Created January 2, 2020 20:57
Terminal output
Received keyup event
Payload for keyup: %{
"altKey" => false,
"charCode" => 0,
"code" => "ArrowDown",
"ctrlKey" => false,
"key" => "ArrowDown",
"keyCode" => 40,
"location" => 0,
"metaKey" => false,
@neslinesli93
neslinesli93 / game.ex
Created January 2, 2020 20:59
Module attributes for the paddle
# Coordinates of the top-left vertex of the paddle. They are relative to the board matrix
@paddle_left 11
@paddle_top 18
# Paddle length/height expressed in basic units
@paddle_length 5
@paddle_height 1
# Misc
@paddle_speed 5