generated from ereborstudios/smaug_project_template
Logan Koester
3 years ago
2 changed files with 171 additions and 4 deletions
@ -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 |
|||
|
@ -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. |
Loadingโฆ
Reference in new issue