From a503afe87619ff82201c0a43818fa1c3f070a548 Mon Sep 17 00:00:00 2001 From: Amir Rajan Date: Sat, 7 Aug 2021 00:13:33 -0500 Subject: Samples folder synced. --- .../farming_game_starting_point/app/main.rb | 84 ++++++ .../farming_game_starting_point/app/repl.rb | 307 +++++++++++++++++++++ .../farming_game_starting_point/app/tests.rb | 29 ++ 3 files changed, 420 insertions(+) create mode 100644 samples/99_genre_crafting/farming_game_starting_point/app/main.rb create mode 100644 samples/99_genre_crafting/farming_game_starting_point/app/repl.rb create mode 100644 samples/99_genre_crafting/farming_game_starting_point/app/tests.rb (limited to 'samples/99_genre_crafting/farming_game_starting_point/app') diff --git a/samples/99_genre_crafting/farming_game_starting_point/app/main.rb b/samples/99_genre_crafting/farming_game_starting_point/app/main.rb new file mode 100644 index 0000000..4c7dd61 --- /dev/null +++ b/samples/99_genre_crafting/farming_game_starting_point/app/main.rb @@ -0,0 +1,84 @@ +def tick args + args.state.tile_size = 80 + args.state.player_speed = 4 + args.state.player ||= tile(args, 7, 3, 0, 128, 180) + generate_map args + #press j to plant a green onion + if args.inputs.keyboard.j + #change this part you can change what you want to plant + args.state.walls << tile(args, ((args.state.player.x+80)/args.state.tile_size), ((args.state.player.y)/args.state.tile_size), 255, 255, 255) + args.state.plants << tile(args, ((args.state.player.x+80)/args.state.tile_size), ((args.state.player.y+80)/args.state.tile_size), 0, 160, 0) + end + # Adds walls, background, and player to args.outputs.solids so they appear on screen + args.outputs.solids << [0,0,1280,720, 237,189,101] + args.outputs.sprites << [0, 0, 1280, 720, 'sprites/background.png'] + args.outputs.solids << args.state.walls + args.outputs.solids << args.state.player + args.outputs.solids << args.state.plants + args.outputs.labels << [320, 640, "press J to plant", 3, 1, 255, 0, 0, 200] + + move_player args, -1, 0 if args.inputs.keyboard.left # x position decreases by 1 if left key is pressed + move_player args, 1, 0 if args.inputs.keyboard.right # x position increases by 1 if right key is pressed + move_player args, 0, 1 if args.inputs.keyboard.up # y position increases by 1 if up is pressed + move_player args, 0, -1 if args.inputs.keyboard.down # y position decreases by 1 if down is pressed +end + +# Sets position, size, and color of the tile +def tile args, x, y, *color + [x * args.state.tile_size, # sets definition for array using method parameters + y * args.state.tile_size, # multiplying by tile_size sets x and y to correct position using pixel values + args.state.tile_size, + args.state.tile_size, + *color] +end + +# Creates map by adding tiles to the wall, as well as a goal (that the player needs to reach) +def generate_map args + return if args.state.area + + # Creates the area of the map. There are 9 rows running horizontally across the screen + # and 16 columns running vertically on the screen. Any spot with a "1" is not + # open for the player to move into (and is green), and any spot with a "0" is available + # for the player to move in. + args.state.area = [ + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,], + [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,], + [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,], + [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,], + [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,], + [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,], + [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ], + ].reverse # reverses the order of the area collection + + # By reversing the order, the way that the area appears above is how it appears + # on the screen in the game. If we did not reverse, the map would appear inverted. + + #The wall starts off with no tiles. + args.state.walls = [] + args.state.plants = [] + + # If v is 1, a green tile is added to args.state.walls. + # If v is 2, a black tile is created as the goal. + args.state.area.map_2d do |y, x, v| + if v == 1 + args.state.walls << tile(args, x, y, 255, 160, 156) # green tile + end + end +end + +# Allows the player to move their box around the screen +def move_player args, *vector + box = args.state.player.shift_rect(vector) # box is able to move at an angle + + # If the player's box hits a wall, it is not able to move further in that direction + return if args.state.walls + .any_intersect_rect?(box) + + # Player's box is able to move at angles (not just the four general directions) fast + args.state.player = + args.state.player + .shift_rect(vector.x * args.state.player_speed, # if we don't multiply by speed, then + vector.y * args.state.player_speed) # the box will move extremely slow +end diff --git a/samples/99_genre_crafting/farming_game_starting_point/app/repl.rb b/samples/99_genre_crafting/farming_game_starting_point/app/repl.rb new file mode 100644 index 0000000..f4c8292 --- /dev/null +++ b/samples/99_genre_crafting/farming_game_starting_point/app/repl.rb @@ -0,0 +1,307 @@ +# =============================================================== +# Welcome to repl.rb +# =============================================================== +# You can experiement with code within this file. Code in this +# file is only executed when you save (and only excecuted ONCE). +# =============================================================== + +# =============================================================== +# REMOVE the "x" from the word "xrepl" and save the file to RUN +# the code in between the do/end block delimiters. +# =============================================================== + +# =============================================================== +# ADD the "x" to the word "repl" (make it xrepl) and save the +# file to IGNORE the code in between the do/end block delimiters. +# =============================================================== + +# Remove the x from xrepl to run the code. Add the x back to ignore to code. +xrepl do + puts "The result of 1 + 2 is: #{1 + 2}" +end + +# ==================================================================================== +# Ruby Crash Course: +# Strings, Numeric, Booleans, Conditionals, Looping, Enumerables, Arrays +# ==================================================================================== + +# ==================================================================================== +# Strings +# ==================================================================================== +# Remove the x from xrepl to run the code. Add the x back to ignore to code. +xrepl do + message = "Hello World" + puts "The value of message is: " + message + puts "Any value can be interpolated within a string using \#{}." + puts "Interpolated message: #{message}." + puts 'This #{message} is not interpolated because the string uses single quotes.' +end + +# ==================================================================================== +# Numerics +# ==================================================================================== +# Remove the x from xrepl to run the code. Add the x back to ignore to code. +xrepl do + a = 10 + puts "The value of a is: #{a}" + puts "a + 1 is: #{a + 1}" + puts "a / 3 is: #{a / 3}" +end + +# Remove the x from xrepl to run the code. Add the x back to ignore to code. +xrepl do + b = 10.12 + puts "The value of b is: #{b}" + puts "b + 1 is: #{b + 1}" + puts "b as an integer is: #{b.to_i}" + puts '' +end + +# ==================================================================================== +# Booleans +# ==================================================================================== +# Remove the x from xrepl to run the code. Add the x back to ignore to code. +xrepl do + c = 30 + puts "The value of c is #{c}." + + if c + puts "This if statement ran because c is truthy." + end +end + +# Remove the x from xrepl to run the code. Add the x back to ignore to code. +xrepl do + d = false + puts "The value of d is #{d}." + + if !d + puts "This if statement ran because d is falsey, using the not operator (!) makes d evaluate to true." + end + + e = nil + puts "Nil is also considered falsey. The value of e is: #{e}." + + if !e + puts "This if statement ran because e is nil (a falsey value)." + end +end + +# ==================================================================================== +# Conditionals +# ==================================================================================== +# Remove the x from xrepl to run the code. Add the x back to ignore to code. +xrepl do + i_am_true = true + i_am_nil = nil + i_am_false = false + i_am_hi = "hi" + + puts "======== if statement" + i_am_one = 1 + if i_am_one + puts "This was printed because i_am_one is truthy." + end + + puts "======== if/else statement" + if i_am_false + puts "This will NOT get printed because i_am_false is false." + else + puts "This was printed because i_am_false is false." + end + + puts "======== if/elsif/else statement" + if i_am_false + puts "This will NOT get printed because i_am_false is false." + elsif i_am_true + puts "This was printed because i_am_true is true." + else + puts "This will NOT get printed i_am_true was true." + end + + puts "======== case statement " + i_am_one = 1 + case i_am_one + when 10 + puts "case equaled: 10" + when 9 + puts "case equaled: 9" + when 5 + puts "case equaled: 5" + when 1 + puts "case equaled: 1" + else + puts "Value wasn't cased." + end + + puts "======== different types of comparisons" + if 4 == 4 + puts "equal (4 == 4)" + end + + if 4 != 3 + puts "not equal (4 != 3)" + end + + if 3 < 4 + puts "less than (3 < 4)" + end + + if 4 > 3 + puts "greater than (4 > 3)" + end + + if ((4 > 3) || (3 < 4) || false) + puts "or statement ((4 > 3) || (3 < 4) || false)" + end + + if ((4 > 3) && (3 < 4)) + puts "and statement ((4 > 3) && (3 < 4))" + end +end + +# ==================================================================================== +# Looping +# ==================================================================================== +# Remove the x from xrepl to run the code. Add the x back to ignore to code. +xrepl do + puts "======== times block" + 3.times do |i| + puts i + end + puts "======== range block exclusive" + (0...3).each do |i| + puts i + end + puts "======== range block inclusive" + (0..3).each do |i| + puts i + end +end + +# ==================================================================================== +# Enumerables +# ==================================================================================== +# Remove the x from xrepl to run the code. Add the x back to ignore to code. +xrepl do + puts "======== array each" + colors = ["red", "blue", "yellow"] + colors.each do |color| + puts color + end + + puts '======== array each_with_index' + colors = ["red", "blue", "yellow"] + colors.each_with_index do |color, i| + puts "#{color} at index #{i}" + end +end + +# Remove the x from xrepl to run the code. Add the x back to ignore to code. +xrepl do + puts "======== single parameter function" + def add_one_to n + n + 5 + end + + puts add_one_to(3) + + puts "======== function with default value" + def function_with_default_value v = 10 + v * 10 + end + + puts "passing three: #{function_with_default_value(3)}" + puts "passing nil: #{function_with_default_value}" + + puts "======== Or Equal (||=) operator for nil values" + def function_with_nil_default_with_local a = nil + result = a + result ||= "or equal operator was exected and set a default value" + end + + puts "passing 'hi': #{function_with_nil_default_with_local 'hi'}" + puts "passing nil: #{function_with_nil_default_with_local}" +end + +# ==================================================================================== +# Arrays +# ==================================================================================== +# Remove the x from xrepl to run the code. Add the x back to ignore to code. +xrepl do + puts "======== Create an array with the numbers 1 to 10." + one_to_ten = (1..10).to_a + puts one_to_ten + + puts "======== Create a new array that only contains even numbers from the previous array." + one_to_ten = (1..10).to_a + evens = one_to_ten.find_all do |number| + number % 2 == 0 + end + puts evens + + puts "======== Create a new array that rejects odd numbers." + one_to_ten = (1..10).to_a + also_even = one_to_ten.reject do |number| + number % 2 != 0 + end + puts also_even + + puts "======== Create an array that doubles every number." + one_to_ten = (1..10).to_a + doubled = one_to_ten.map do |number| + number * 2 + end + puts doubled + + puts "======== Create an array that selects only odd numbers and then multiply those by 10." + one_to_ten = (1..10).to_a + odd_doubled = one_to_ten.find_all do |number| + number % 2 != 0 + end.map do |odd_number| + odd_number * 10 + end + puts odd_doubled + + puts "======== All combination of numbers 1 to 10." + one_to_ten = (1..10).to_a + all_combinations = one_to_ten.product(one_to_ten) + puts all_combinations + + puts "======== All uniq combinations of numbers. For example: [1, 2] is the same as [2, 1]." + one_to_ten = (1..10).to_a + uniq_combinations = + one_to_ten.product(one_to_ten) + .map do |unsorted_number| + unsorted_number.sort + end.uniq + puts uniq_combinations +end + +# ==================================================================================== +# Advanced Arrays +# ==================================================================================== +# Remove the x from xrepl to run the code. Add the x back to ignore to code. +xrepl do + puts "======== All unique Pythagorean Triples between 1 and 40 sorted by area of the triangle." + + one_to_hundred = (1..40).to_a + triples = + one_to_hundred.product(one_to_hundred).map do |width, height| + [width, height, Math.sqrt(width ** 2 + height ** 2)] + end.find_all do |_, _, hypotenuse| + hypotenuse.to_i == hypotenuse + end.map do |triangle| + triangle.map(&:to_i) + end.uniq do |triangle| + triangle.sort + end.map do |width, height, hypotenuse| + [width, height, hypotenuse, (width * height) / 2] + end.sort_by do |_, _, _, area| + area + end + + triples.each do |width, height, hypotenuse, area| + puts "(#{width}, #{height}, #{hypotenuse}) = #{area}" + end +end diff --git a/samples/99_genre_crafting/farming_game_starting_point/app/tests.rb b/samples/99_genre_crafting/farming_game_starting_point/app/tests.rb new file mode 100644 index 0000000..458bc18 --- /dev/null +++ b/samples/99_genre_crafting/farming_game_starting_point/app/tests.rb @@ -0,0 +1,29 @@ +# For advanced users: +# You can put some quick verification tests here, any method +# that starts with the `test_` will be run when you save this file. + +# Here is an example test and game + +# To run the test: ./dragonruby mygame --eval app/tests.rb --no-tick + +class MySuperHappyFunGame + attr_gtk + + def tick + outputs.solids << [100, 100, 300, 300] + end +end + +def test_universe args, assert + game = MySuperHappyFunGame.new + game.args = args + game.tick + assert.true! args.outputs.solids.length == 1, "failure: a solid was not added after tick" + assert.false! 1 == 2, "failure: some how, 1 equals 2, the world is ending" + puts "test_universe completed successfully" +end + +puts "running tests" +$gtk.reset 100 +$gtk.log_level = :off +$gtk.tests.start -- cgit v1.2.3