From e31916e6508dedde60f6dc1e3f074c47ca1ff967 Mon Sep 17 00:00:00 2001 From: Logan Koester Date: Mon, 24 Jan 2022 11:24:22 +0700 Subject: [PATCH] Imports snakemoji sample --- app/main.rb | 166 ++++++++++++++++++++++++++++++++++++++++- license-for-sample.txt | 9 +++ 2 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 license-for-sample.txt diff --git a/app/main.rb b/app/main.rb index b185c05..3bffa22 100644 --- a/app/main.rb +++ b/app/main.rb @@ -1,7 +1,165 @@ -require 'smaug.rb' +# coding: utf-8 +################################ +# So I was working on a snake game while +# learning DragonRuby, and at some point I had a thought +# what if I use "😀" as a function name, surely it wont work right...? +# RIGHT....? +# BUT IT DID, IT WORKED +# it all went downhill from then +# Created by Anton K. (ai Doge) +# https://gist.github.com/scorp200 +#############LICENSE############ +# Feel free to use this anywhere and however you want +# You can sell this to EA for $1,000,000 if you want, its completely free. +# Just rememeber you are helping this... thing... to spread... +# ALSO! I am not liable for any mental, physical or financial damage caused. +#############LICENSE############ + + +class Array + #Helper function + def move! vector + self.x += vector.x + self.y += vector.y + return self + end + + #Helper function to draw snake body + def draw! 🎮, 📺, color + translate 📺.solids, 🎮.⛓, [self.x * 🎮.⚖️ + 🎮.🛶 / 2, self.y * 🎮.⚖️ + 🎮.🛶 / 2, 🎮.⚖️ - 🎮.🛶, 🎮.⚖️ - 🎮.🛶, color] + end + + #This is where it all started, I was trying to find good way to multiply a map by a number, * is already used so is ** + #I kept trying different combinations of symbols, when suddenly... + def 😀 value + self.map {|d| d * value} + end +end + +#Draw stuff with an offset +def translate output_collection, ⛓, what + what.x += ⛓.x + what.y += ⛓.y + output_collection << what +end + +BLUE = [33, 150, 243] +RED = [244, 67, 54] +GOLD = [255, 193, 7] +LAST = 0 def tick args - args.outputs.labels << [640, 500, 'Irate-Telephone', 5, 1] - args.outputs.labels << [640, 460, 'A new Smaug project', 5, 1] - args.outputs.sprites << [576, 280, 128, 101, 'dragonruby.png'] + defaults args.state + render args.state, args.outputs + input args.state, args.inputs + update args.state +end + +def update 🎮 + #Update every 10 frames + if 🎮.tick_count.mod_zero? 10 + #Add new snake body piece at head's location + 🎮.🐍 << [*🎮.🤖] + #Assign Next Direction to Direction + 🎮.🚗 = *🎮.🚦 + + #Trim the snake a bit if its longer than current size + if 🎮.🐍.length > 🎮.🛒 + 🎮.🐍 = 🎮.🐍[-🎮.🛒..-1] + end + + #Move the head in the Direction + 🎮.🤖.move! 🎮.🚗 + + #If Head is outside the playing field, or inside snake's body restart game + if 🎮.🤖.x < 0 || 🎮.🤖.x >= 🎮.🗺.x || 🎮.🤖.y < 0 || 🎮.🤖.y >= 🎮.🗺.y || 🎮.🚗 != [0, 0] && 🎮.🐍.any? {|s| s == 🎮.🤖} + LAST = 🎮.💰 + 🎮.as_hash.clear + return + end + + #If head lands on food add size and score + if 🎮.🤖 == 🎮.🍎 + 🎮.🛒 += 1 + 🎮.💰 += (🎮.🛒 * 0.8).floor.to_i + 5 + spawn_🍎 🎮 + puts 🎮.🍎 + end + end + + #Every second remove 1 point + if 🎮.💰 > 0 && 🎮.tick_count.mod_zero?(60) + 🎮.💰 -= 1 + end +end + +def spawn_🍎 🎮 + #Food + 🎮.🍎 ||= [*🎮.🤖] + #Randomly spawns food inside the playing field, keep doing this if the food keeps landing on the snake's body + while 🎮.🐍.any? {|s| s == 🎮.🍎} || 🎮.🍎 == 🎮.🤖 do + 🎮.🍎 = [rand(🎮.🗺.x), rand(🎮.🗺.y)] + end +end + +def render 🎮, 📺 + #Paint the background black + 📺.solids << [0, 0, 1280, 720, 0, 0, 0, 255] + #Draw a border for the playing field + translate 📺.borders, 🎮.⛓, [0, 0, 🎮.🗺.x * 🎮.⚖️, 🎮.🗺.y * 🎮.⚖️, 255, 255, 255] + + #Draw the snake's body + 🎮.🐍.map do |🐍| 🐍.draw! 🎮, 📺, BLUE end + #Draw the head + 🎮.🤖.draw! 🎮, 📺, BLUE + #Draw the food + 🎮.🍎.draw! 🎮, 📺, RED + + #Draw current score + translate 📺.labels, 🎮.⛓, [5, 715, "Score: #{🎮.💰}", GOLD] + #Draw your last score, if any + translate 📺.labels, 🎮.⛓, [[*🎮.🤖.😀(🎮.⚖️)].move!([0, 🎮.⚖️ * 2]), "Your Last score is #{LAST}", 0, 1, GOLD] unless LAST == 0 || 🎮.🚗 != [0, 0] + #Draw starting message, only if Direction is 0 + translate 📺.labels, 🎮.⛓, [🎮.🤖.😀(🎮.⚖️), "Press any Arrow key to start", 0, 1, GOLD] unless 🎮.🚗 != [0, 0] +end + +def input 🎮, 🕹 + #Left and Right keyboard input, only change if X direction is 0 + if 🕹.keyboard.key_held.left && 🎮.🚗.x == 0 + 🎮.🚦 = [-1, 0] + elsif 🕹.keyboard.key_held.right && 🎮.🚗.x == 0 + 🎮.🚦 = [1, 0] + end + + #Up and Down keyboard input, only change if Y direction is 0 + if 🕹.keyboard.key_held.up && 🎮.🚗.y == 0 + 🎮.🚦 = [0, 1] + elsif 🕹.keyboard.key_held.down && 🎮.🚗.y == 0 + 🎮.🚦 = [0, -1] + end +end + +def defaults 🎮 + #Playing field size + 🎮.🗺 ||= [20, 20] + #Scale for drawing, screen height / Field height + 🎮.⚖️ ||= 720 / 🎮.🗺.y + #Offset, offset all rendering to the center of the screen + 🎮.⛓ ||= [(1280 - 720).fdiv(2), 0] + #Padding, make the snake body slightly smaller than the scale + 🎮.🛶 ||= (🎮.⚖️ * 0.2).to_i + #Snake Size + 🎮.🛒 ||= 3 + #Snake head, the only part we are actually controlling + 🎮.🤖 ||= [🎮.🗺.x / 2, 🎮.🗺.y / 2] + #Snake body map, follows the head + 🎮.🐍 ||= [] + #Direction the head moves to + 🎮.🚗 ||= [0, 0] + #Next_Direction, during input check only change this variable and then when game updates asign this to Direction + 🎮.🚦 ||= [*🎮.🚗] + #Your score + 🎮.💰 ||= 0 + #Spawns Food randomly + spawn_🍎(🎮) unless 🎮.🍎? end diff --git a/license-for-sample.txt b/license-for-sample.txt new file mode 100644 index 0000000..376dd0e --- /dev/null +++ b/license-for-sample.txt @@ -0,0 +1,9 @@ +Copyright 2019 Anton K. (ai Doge) + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.