Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Last active December 15, 2020 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/b3ac3533c766c28fbf60147daff6d42d to your computer and use it in GitHub Desktop.
Save amirrajan/b3ac3533c766c28fbf60147daff6d42d to your computer and use it in GitHub Desktop.
Proud Lines of Code: DragonRuby DVD Game
def tick args
defaults args # initialize the defaults for the game
render args # render the game
input args # check for user input
calc args # run simulation
end
def defaults args
args.state.colors = [:red, :orange, :green, :blue, :indigo, :violet] # colors for random selection
args.state.box.size = 50 # size of the box
args.state.box.speed = 1 # movement speed
args.state.box.x ||= args.grid.w.half - args.state.box.size.half # start the box center x
args.state.box.y ||= args.grid.h.half - args.state.box.size.half # start the box center y
args.state.box.dx ||= [1, -1].sample # pick a random direction of movement for x
args.state.box.dy ||= [1, -1].sample # pick a random direction of movement for y
args.state.box.status ||= :moving # state variable that is consulted before moving the box
set_box_color args if !args.state.box.color # set the color of the box to a random one
end
def render args
args.outputs.sprites << [ # render the box sprite
args.state.box.x,
args.state.box.y,
args.state.box.size,
args.state.box.size,
"sprites/square-#{args.state.box.color}.png"
]
end
def input args
return unless args.inputs.keyboard.key_down.space # exit this function if space isn't pressed
if args.state.box.status == :moving # toggle the box state
args.state.box.status = :stopped
else
args.state.box.status = :moving
end
end
def calc args
return unless args.state.box.status == :moving # do not move the box if the box status isn't :moving
args.state.box.x += args.state.box.dx * args.state.box.speed # move the box in the x direction by the speed
args.state.box.y += args.state.box.dy * args.state.box.speed # move the box in the y direction by the speed
# collision logic: determine if the box is outside the
# bounds of the 1280x720 canvas if it is, then change
# the direction of the box and color
if (args.state.box.x + args.state.box.size) > args.grid.w # right side of the canvas check
args.state.box.x = args.grid.w - args.state.box.size
args.state.box.dx = -1
set_box_color args
elsif args.state.box.x < 0 # left side of the canvas check
args.state.box.x = 0
args.state.box.dx = 1
set_box_color args
end
if (args.state.box.y + args.state.box.size) > args.grid.h # top of the canvas check
args.state.box.y = args.grid.h - args.state.box.size
args.state.box.dy = -1
set_box_color args
elsif args.state.box.y < 0 # bottom of the canvas check
args.state.box.y = 0
args.state.box.dy = 1
set_box_color args
end
end
def set_box_color args # helper method for setting the box's color
args.state.box.color = (args.state.colors - [args.state.box.color]).sample # to something other than it's current color
end
@erikyuzwa
Copy link

thanks a lot @amirrajan! Works like a charm. Just a quick note that its seems the HTML gremlins got you. There's some &gt; and &lt; in this source. 🪲

@amirrajan
Copy link
Author

thanks a lot @amirrajan! Works like a charm. Just a quick note that its seems the HTML gremlins got you. There's some &gt; and &lt; in this source. 🐞

fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment