Andreas' practical language comparison

G

Georgy

|
| I found time to do "Red Ball" just now, hope no one else was faster. I
| think it looks pretty cool in ruby (dunno if it goes even
| better). Especially comparing it to the python-version...

Hi,
I didn't understand your sarcasm about Python. If you compare *equivalent* programs
written on Ruby and Python, they'll be *extremely* the same:

#!/bin/env python

# We use the Tk-Toolkit
from Tkinter import Tk, Canvas
from random import randint # G-: Okey, okey, I added these two lines
from sys import exit # and removed two 'end's. Agreed? :)


# Create a root-widget, a canvas and a hash to store the squares
root = Tk(); root.title( 'Red Ball' )

c = Canvas(root, width=320, height=200)
fig = {}

# Create the squares with the appropriate colors on random places
for colour in ('black', 'red'):
x = randint(0, 320 - 16)
y = randint(0, 200 - 16)
fig[colour] = c.create_rectangle( x, y, x+15, y+15, fill=colour )

c.pack()

# Move the square in the desired direction, exit if we have won
def move_player(x, y):
c.move( fig['red'], x, y )
print c.coords(fig['red']), c.coords(fig['black'])
if c.coords(fig['red']) == c.coords(fig['black']): exit(0)

# Bind the actions to keys
root.bind("s", lambda e: move_player(-1, 0) )
root.bind("d", lambda e: move_player( 1, 0) )
root.bind("e", lambda e: move_player( 0, -1) )
root.bind("x", lambda e: move_player( 0, 1) )

# Finally give up control to Tk
root.mainloop()

Regards,
Georgy
 
G

gabriele renzi

il Sun, 30 May 2004 17:25:03 +0200, Hynek Schlawack
I found time to do "Red Ball" just now, hope no one else was faster. I
think it looks pretty cool in ruby (dunno if it goes even
better). Especially comparing it to the python-version...

I believe some little tweaks may make this little more rubyish:
get rid of global variables using a constant, a local, and defining
move_player as a singleton method of the canvas object

#!/bin/env ruby

# We use the Tk-Toolkit
require 'tk'

# Create a root-widget, a canvas and a hash to store the squares
root = TkRoot.new { title "Red Ball" }
canvas = TkCanvas.new(root) { width 320; height 200 }
Fig = Hash.new

# Create the squares with the appropriate colors on random places
['black', 'red'].each do | colour |
x = (rand * (320 - 16)).to_i
y = (rand * (200 - 16)).to_i
Fig[colour] = TkcRectangle.new(canvas, x, y, x+15, y+15, 'fill' =>
colour)
end

canvas.pack

# Move the square in the desired direction, exit if we have won
def canvas.move_player(x, y)
move(Fig['red'], x, y)
exit if (coords(Fig['red']) == coords(Fig['black']))
end

# Bind the actions to keys
root.bind("s") { canvas.move_player(-1, 0) }
root.bind("d") { canvas.move_player( 1, 0) }
root.bind("e") { canvas.move_player( 0, -1) }
root.bind("x") { canvas.move_player( 0, 1) }

# Finally give up control to Tk
Tk.mainloop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top