Skip to content

Instantly share code, notes, and snippets.

@arishal

arishal/pong Secret

Last active August 29, 2015 14:04
Show Gist options
  • Save arishal/2b96b561b724bbad1c5d to your computer and use it in GitHub Desktop.
Save arishal/2b96b561b724bbad1c5d to your computer and use it in GitHub Desktop.
A pong game written in Ruby
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.05
ball["x"] -= cos(ball["dir"]) * scof * 0.05
a = Curses::getch
if a == ("w" || "W")
paddle += 1
elsif a == ("s" || "S")
paddle -= 1
end
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment