* FAQ    * Search  * Register * Login 
Active topics
Unanswered topics

All times are UTC-06:00



Post new topic  Reply to topic  [ 6 posts ] 
Author Message
 Post subject: moving box!
PostPosted: Sun Sep 26, 2010 6:52 am 
Offline
DBB Artist
DBB Artist
User avatar

Joined: Mon Aug 01, 2005 8:47 am
Posts: 7124
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝
This is a text-art animated box, python program.
Code:
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.
   


Top
   
 Post subject:
PostPosted: Mon Sep 27, 2010 3:51 am 
Offline
DBB Master
DBB Master
User avatar

Joined: Sun Sep 05, 1999 2:01 am
Posts: 6377
Location: ☃☃☃
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:
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:
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/


Top
   
 Post subject:
PostPosted: Mon Sep 27, 2010 9:18 am 
Offline
DBB Artist
DBB Artist
User avatar

Joined: Mon Aug 01, 2005 8:47 am
Posts: 7124
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝
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!


Top
   
 Post subject:
PostPosted: Mon Sep 27, 2010 9:39 am 
Offline
DBB Master
DBB Master
User avatar

Joined: Fri May 28, 1999 2:01 am
Posts: 5428
ICQ: 28874658
Website: http://www.povterminal.net/
Location: Bellevue, WA
\"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.


Top
   
 Post subject: Re:
PostPosted: Mon Sep 27, 2010 2:34 pm 
Offline
DBB Master
DBB Master
User avatar

Joined: Sun Sep 05, 1999 2:01 am
Posts: 6377
Location: ☃☃☃
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:
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.


Top
   
 Post subject:
PostPosted: Tue Sep 28, 2010 10:55 am 
Offline
DBB Artist
DBB Artist
User avatar

Joined: Mon Aug 01, 2005 8:47 am
Posts: 7124
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝
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!


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 6 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Descent'rs have piloted these pages
 
The layout and contents contained within this site are © DescentBB.net 1997-2006.
Descent, Descent II are © Parallax Software Corporation.
Descent III is Outrage Entertainment.
Descent is a Trademark of Interplay Productions.

Miner Wars™ is trademark of Keen Software House s. r. o.
.


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group