Skip to content

Instantly share code, notes, and snippets.

@ohAitch
Forked from arishal/pong
Last active August 29, 2015 14:04
Show Gist options
  • Save ohAitch/be72b6eb1c008b124bb7 to your computer and use it in GitHub Desktop.
Save ohAitch/be72b6eb1c008b124bb7 to your computer and use it in GitHub Desktop.
puts "1"
require "curses"
include Curses
include Math
def pong
# ALL THIS NEEDS TO BE DISPLAYED SOMEHOW
# show “CONTROL THE PADDLE WITH THE W AND S KEYS” in the middle of the board for a few seconds
score = 0
scof = 1.0
paddle = 50.0 # max y-coordinate/2, making the board 100x100 for now
#paddle is 10 long
ball = {"x" => (rand(50.0) + 50.0), "y" => rand(100.0), "dir" => rand * 2.0 * PI}
# remember to make everything floats or 1/2 will equal 0 and the world will end
Curses::timeout = 50
while ball["x"] != 0
#i don’t actually need this for anything but it took a long time to type so i’m saving it just in case
#(ball[“x”] > 0) && (ball[“x”] < 100) && (ball[“y”] > 0) && (ball[“y”] < 100)
ball["y"] -= sin(ball["dir"]) * scof * 0.5
ball["x"] -= cos(ball["dir"]) * scof * 0.5
setpos 0, 10
addstr ball.to_s
setpos 1, 10
addstr "#{sin(ball["dir"])} #{cos(ball["dir"])}"
a = Curses::getch
if a == ("w" || "W")
paddle += 1
elsif a == ("s" || "S")
paddle -= 1
end
addstr "#{paddle}"
newdir = lambda { |x| ball["dir"] + (2 * (x - ball["dir"])) + (rand * 0.3) }
if ball["y"] <= 0
ball ["dir"] = newdir[PI * 0.5]
elsif ball["y"] >= 100
ball["dir"] = newdir[PI * 1.5]
elsif ball["x"] >= 100
ball["dir"] = newdir[PI * 1.0]
elsif ball["x"] <= 1
if (ball["y"] < (paddle + 5.0)) && (ball["y"] > (paddle - 5.0))
ball["dir"] = newdir[0]
scof += 0.1
score += 1
#alternately, score and speed coefficient could increment with time
else
ball["x"] = 0
end
end
setpos ball["x"] / 5 , ball["y"] / 5
addch '.'
end
# replace board with “YOU LOSE”, pause for a second, then add “YOUR SCORE WAS #{score}”, pause again, then “DO YOU WANT TO PLAY AGAIN (Y/N)?”
b = getch
if b == ("y" || "Y")
pong
end
end
puts "2"
pong
puts "3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment