pawlack

Untitled

Feb 22nd, 2020
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. import math
  2. from math import sqrt
  3. import curses
  4. import random
  5. from curses import wrapper
  6. from random import randint
  7.  
  8. all_leafs = []
  9.  
  10.  
  11. if __name__ == "__main__":
  12.    
  13.  
  14.     def main(screen):
  15.        
  16.         #screen.timeout(1000)
  17.         #screen function calls
  18.         num_rows, num_cols = screen.getmaxyx()
  19.         min_x = 1
  20.         min_y = 1
  21.         max_x = num_cols-2
  22.         max_y = num_rows-2
  23.         screen.keypad(1)
  24.         curses.noecho()
  25.         curses.curs_set(0)
  26.         screen.border(0)
  27.         screen.addstr(0, 2, "".join(["Screen size: ", str(num_rows), " x ", str(num_cols)]))
  28.  
  29.         #classes
  30.  
  31.  
  32.  
  33.         class Leaf():
  34.             dire = [-1, 1]
  35.             randomdir = dire[randint(0, 1)]
  36.             y_position = 0
  37.             x_position = 0
  38.             velocity = 1
  39.             speed_counter = 0
  40.             global all_leafs
  41.  
  42.             def __init__(self, y, x):
  43.                 self.x_position = x
  44.                 self.y_position = y
  45.                 all_leafs.append(self)
  46.  
  47.  
  48.             def set_new_position(self):
  49.                
  50.                 if find_collision(self.y_position + 1, self.x_position) == False:
  51.                     self.y_position += 1
  52.                     if self.velocity < 20:
  53.                         self.velocity += 1+2/(self.velocity+0.1)
  54.                 elif find_collision(self.y_position + 1, self.x_position + self.randomdir) == False:
  55.                     self.x_position += self.randomdir
  56.                     self.y_position += 1
  57.                     if self.velocity < 20:
  58.                         self.velocity += 1+2/(self.velocity+0.1)
  59.                 elif find_collision(self.y_position, self.x_position + self.randomdir) == False and self.velocity > 0 :
  60.                     self.x_position += self.randomdir
  61.                     if self.velocity > 0:
  62.                         self.velocity -= 1
  63.                 else:
  64.                     self.randomdir = -self.randomdir
  65.                     if self.velocity > 0:
  66.                         self.velocity -= 1
  67.                
  68.                
  69.  
  70.             def renderself(self):
  71.                 screen.addstr(self.y_position, self.x_position, "O")
  72.  
  73.             def tick(self):
  74.                 self.speed_counter += 1
  75.                 if self.speed_counter + self.velocity >= 30:
  76.                
  77.                     self.set_new_position()
  78.                    
  79.                     self.speed_counter = 0
  80.                 self.renderself()
  81.  
  82.         def find_collision(y_pos, x_pos):
  83.             for ob in all_leafs:
  84.                 if ob.y_position == y_pos and ob.x_position == x_pos:
  85.                     return True
  86.             if y_pos >= max_y or y_pos <= min_y:
  87.                 return True
  88.             if x_pos >= max_x or x_pos <= min_x:
  89.                 return True
  90.             return False
  91.  
  92.  
  93.         tiktak = 1
  94.        
  95.         while True:
  96.            
  97.            
  98.             screen.refresh()
  99.             curses.napms(5)
  100.             screen.erase()
  101.             if tiktak <= 400:
  102.                 Leaf(1, randint(int(min_x+max_x/4), int(max_x/2)))
  103.  
  104.             #Leaf(2+tiktak, 2)
  105.  
  106.             for obj in all_leafs:
  107.                 obj.tick()
  108.  
  109.             tiktak += 1
  110.             screen.border(0)
  111.             screen.addstr(0, 40, str(tiktak))
  112.             screen.addstr(0, 10, str(all_leafs[0].velocity))
  113.             if tiktak >= 3000:
  114.                 break
  115.  
  116.            
  117.  
  118.  
  119.  
  120.     wrapper(main)
  121.  
  122.     print("bye")
  123.     #print(all_leafs)
  124.     #input("")
Advertisement
Add Comment
Please, Sign In to add comment