|
| 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