moving box!

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7649
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

moving box!

Post by Isaac »

This is a text-art animated box, python program.

Code: Select all

import os
import math
class box:
	def clear(self):
		os.system(\"clear\")
	def count(self,a):
		self.a=\" \"*int(round(math.cos(a)*50)+50) #controls horizontal offset
		self.b=int(round(math.sin(a)*20)+20) #controls vertical offset
	def box(self):
		print self.a,\"____\\n\",self.a,\"[  ]\\n\",self.a,\"[__]\"#draws box & horizontal offset
	def voffset(self):
		b=0
		while b<=self.b:
			print
			b+=1
go=box()

a=0
while a<40:#the number after the < sign controls the duration.
	go.clear()
	go.count(a)
	go.voffset()
	go.box()
	a+=0.02 #lower the number the slower the speed. 
	
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6511
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

Post by Jeff250 »

Not bad for starting out but too many lines... this is Python, not Java or C#. ;) The following should be functionally equivalent except for some parameters I changed to reflect my CPU speed and terminal size:

Code: Select all

import os
import math

for a in [x * 0.002 for x in range(0, 10000)]:
    os.system('clear')
    print '\\n' * int(round(math.sin(a) * 10) + 10)
    space = ' ' * int(round(math.cos(a) * 37) + 37) 
    print '%s____\\n%s[  ]\\n%s[__]' % ((space,) * 3)
The following one is more CPU-friendly:

Code: Select all

import os
import math
import time

for a in [x * 0.02 for x in range(0, 10000)]:
    os.system('clear')
    print '\\n' * int(round(math.sin(a) * 10) + 10)
    space = ' ' * int(round(math.cos(a) * 37) + 37) 
    print '%s____\\n%s[  ]\\n%s[__]' % ((space,) * 3)
    time.sleep(0.01)
Also, since you're now sharing Python code, you should read this:
http://www.python.org/dev/peps/pep-0008/
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7649
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Post by Isaac »

That's amazing! I'm amazed at two things: First, I knew I was using too many lines, but you've shown me how much I was off by. I don't know if people write programs by writing a rough draft then final draft, which has a better use of space. Also, I've never used \"for\" in the way you do. I'm going to investigate that before moving forward. I'll also look into time.sleep(), which is also new to me.

I read the introduction in that link and I will read the rest this week, though it shouldn't take long; I don't want to rush through it; it looks important.

On a python forum all I got was, \"cute\" and \"fun but useless\". Your post is far more valuable. Thank you!
User avatar
Sirius
DBB Master
DBB Master
Posts: 5616
Joined: Fri May 28, 1999 2:01 am
Location: Bellevue, WA
Contact:

Post by Sirius »

\"Drafts\" in that sense aren't common as far as I've seen - not for use of space or anything like that - but there is a practice known as refactoring which sometimes serves similar purposes. It's usually intended to make code more maintainable though.
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6511
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

Re:

Post by Jeff250 »

Isaac wrote:Also, I've never used "for" in the way you do. I'm going to investigate that before moving forward.
The first "for" is a for loop. In Python, for loops are like the "for each" loops of other languages. They iterate over something iterable (like a list), assigning each value to the variable left of "in".

The second "for" is in a list comprehension. The documentation should get you started. Something that the documentation doesn't really cover well:

Code: Select all

xs = [1, 2, 3]
ys = [4, 5, 6]
pairwise_sums = [x + y for x, y in zip(xs, ys)]
cartesian_product = [(x, y) for x in xs for y in ys]
So to do something pairwise on two lists, use one "for" over the zip'd lists. Since zip is variadic, you can actually extend this pattern to n-tuple-wise on n lists. To do something on every possible combination of one thing from the first list and one thing from the second list, use two "for"'s. Again, you can extend this pattern to n lists and n "for"'s.
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7649
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Post by Isaac »

Thanks Jeff. I have used \"for\" loops, but only for the simplest things, like
>>> for letter in \"LETTERS\":
... print letter,\") \"

I thought that's all one could do with them. I will study that page you gave me, List Comprehension. Thank you!

Thank you too sirius!
Post Reply