Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Daniel added a 2D snowman example

  • Loading branch information...
commit 9e5b1c3767d578a7a6748f5845ab86c4699f5c48 1 parent 801eca2
@holtzermann17 holtzermann17 authored
Showing with 58 additions and 24 deletions.
  1. +58 −24 snowman2d.py
View
82 snowman2d.py
@@ -1,24 +1,58 @@
-# Initial snowman example by Daniel Chiquito
-
-height = 10.0
-time = 0.0
-
-data = {time:height}
-
-# This is the function. To be changed.
-def update(h,t):
- h -= 3.0/(t+1.0)
- data[t] = h
- return h
-
-while time < 15:
- time += 1.0
- height = update(height, time)
-
-print "time | height"
-for t in data:
- ts = str(t)
- hs = str(data[t])
- print " "+ts+(" "*(5-len(ts)))+"| "+hs
-
-# (don't bother decrypting the print line, just for display.)
+class HeightArray:
+ """
+ Basic array for storing the height of the snowman.
+ Each position in the array is the snowmans height at that point on the x axis.
+ """
+ def __init__(self, indx, args):
+ """ Initialize the array. """
+ if int(indx/2.0) == indx/2.0:
+ indx += 1
+ self.indx = indx
+ while len(args) > indx:
+ args = args[1:-1]
+ while len(args) < indx:
+ args.append(0.0)
+ args.insert(0, 0.0)
+ self.args = args
+
+ def update(self):
+ """ Makes a new array. """
+ n_height = HeightArray(self.indx, [0])
+ for i in range(0,self.indx):
+ n_height[i] += self.args[i] * 0.8
+ if i > 0:
+ n_height[i-1] += self.args[i] * 0.1
+ if i < self.indx-1:
+ n_height[i+1] += self.args[i] * 0.1
+ return n_height
+
+ """ Extra functions used for my convenience. ;) """
+ def __str__(self):
+ ret = " "
+ for i in self.args:
+ i = str(int(i*10)/10.0)
+ ret += i + (5 - len(i)) * " "
+ return ret
+ def __repr__(self):
+ return self.args
+ def __setitem__(self, i, assig):
+ self.args[i] = assig
+ def __getitem__(self, i):
+ return self.args[i]
+
+time = 0
+data = {time:HeightArray(7, [0,0,0,10,0,0,0])}
+height = data[0]
+
+""" Runs the loop 15 times. """
+while time < 15:
+ time += 1.0
+ height = height.update()
+ data[time] = height
+
+""" Display the data. """
+print "time | height"
+for t in data:
+ ts = str(t)
+ hs = str(data[t])
+ print " "+ts+(" "*(5-len(ts)))+"| "+hs
Please sign in to comment.
Something went wrong with that request. Please try again.