Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. from scene import *
  2. import sound
  3. import random
  4. import math
  5. import time
  6. A = Action
  7.  
  8. w, h = 0, 0
  9. main_touch = 0
  10. s = 10
  11. by = 140
  12. fs = 4
  13. min_space = 70
  14. y_space = 200
  15. q = 1
  16. in_game = True
  17. wms = 2
  18. obw_colors = {
  19. 'ball1': '#ffffff',
  20. 'ball2': '#000000',
  21. 'background': '#616666',
  22. 'window_1': '#ffffff',
  23. 'window_2': '#000000',
  24. 'labels': '#c2c2c2',
  25. 'walls': '#ff9d00'
  26. }
  27. fsg_colors = {
  28. 'ball1': '#ff0000',
  29. 'ball2': '#0000ff',
  30. 'background': '#000000',
  31. 'window_1': '#ff0000',
  32. 'window_2': '#0000ff',
  33. 'labels': '#c2c2c2',
  34. 'walls': '#ffffff'
  35. }
  36. colors = fsg_colors
  37.  
  38.  
  39.  
  40. class Ball(SpriteNode):
  41. def __init__(self, shape, q='red', **kwargs):
  42. SpriteNode.__init__(self, shape, **kwargs)
  43. self.c = q
  44.  
  45. class m_wall(SpriteNode):
  46. def __init__(self, **kwargs):
  47. SpriteNode.__init__(self, 'White.JPG', **kwargs)
  48. self.sign = random.randrange(-1, 2, 2)
  49.  
  50.  
  51. class window(SpriteNode):
  52. def __init__(self, c='red', **kwargs):
  53. SpriteNode.__init__(self, 'White.JPG', **kwargs)
  54. self.run_action(A.fade_to(0.5, 0))
  55. self.c = c
  56.  
  57. def closest_point(rect, circle):
  58. return Point(max(rect.min_x, min(rect.max_x, circle.x)), max(rect.min_y, min(rect.max_y, circle.y)))
  59.  
  60. def hit_test(rect, circle, radius, bbox=None):
  61. if bbox and not rect.intersects(bbox):
  62. return False
  63. return abs(closest_point(rect, circle) - circle) < radius
  64.  
  65. class MyScene (Scene):
  66. def setup(self):
  67. global w, h, in_game, fs, min_space
  68. min_space = 70
  69. fs = 2
  70. in_game = True
  71. w, h = self.size
  72. #line = SpriteNode('shp:RoundRect', parent=self, position=self.size/2, size=(0.5, 2*h))
  73. self.background_color = colors['background']
  74. self.red = Ball('shp:Circle', q='red', parent=self, position=(w/4, by), color=colors['ball1'])
  75. self.blue = Ball('shp:Circle', q='blue', parent=self, position=(3*w/4, by), color=colors['ball2'])
  76. self._dist = w/2
  77. self.tchs = []
  78. self.walls = []
  79. self.new_wall(h+500)
  80. self._score = 0
  81. self.score_label = LabelNode(str(self._score), ('Futura', 32), parent=self, position=(w/2, h-48), color=colors['labels'])
  82. for ball in {self.red, self.blue}:
  83. ball.scale = 0
  84. ball.run_action(A.scale_to(1, 1))
  85.  
  86. @property
  87. def score(self):
  88. return self._score
  89.  
  90. @score.setter
  91. def score(self, value):
  92. self._score = value
  93. self.score_label.text = str(int(value))
  94.  
  95. @property
  96. def dist(self):
  97. return self._dist
  98.  
  99. @dist.setter
  100. def dist(self, value):
  101. k = self.red.size.w
  102. if abs(value) > w+k:
  103. value = -(w+k)*value/abs(value)
  104. x, y = self.blue.position
  105. x = w/2+value/2
  106. self.blue.position = (x, y)
  107. x, y = self.red.position
  108. x = w/2-value/2
  109. self.red.position = (x, y)
  110. self._dist = value
  111.  
  112. def update(self):
  113. global fs
  114. fs += 0.001
  115. if not in_game:
  116. return
  117. self.score += fs/100
  118. self.ball_grav()
  119. self.ball_effect()
  120. self.wall_update()
  121. for ball in {self.red, self.blue}:
  122. for wall in self.walls:
  123. if hit_test(wall.frame, ball.position, ball.size.w/2, ball.bbox):
  124. if type(wall) == window:
  125. if wall.c != ball.c:
  126. self.game_over()
  127. else:
  128. self.game_over()
  129.  
  130. def wall_update(self):
  131. for wall in set(self.walls):
  132. if type(wall) == m_wall:
  133. ww = wall.size.w/2
  134. x, y = wall.position
  135. sign = wall.sign
  136. x = max(min(x+sign*wms, w-ww), ww)
  137. wall.position = (x, y)
  138. if x == ww or x == w-ww:
  139. wall.sign *= -1
  140.  
  141.  
  142. if wall.position.y < by:
  143. wall.run_action(A.fade_to(0, (by/fs)/60))
  144. wall.run_action(A.move_by(0, -fs, 0))
  145. if wall.position.y < -32:
  146. self.walls.remove(wall)
  147. if h-self.walls[-1].position.y > y_space:
  148. self.new_wall(h+32)
  149.  
  150. def new_game(self):
  151. for child in self.children:
  152. child.remove_from_parent()
  153. self.setup()
  154.  
  155. def game_over(self):
  156. global in_game
  157. if not in_game:
  158. return
  159. in_game = False
  160. a = SpriteNode('White.JPG', parent=self, position=self.size/2, size=self.size, z_position=5)
  161. a.run_action(A.sequence(
  162. A.fade_to(0, 0.3),
  163. A.remove()
  164. ))
  165. for wall in self.walls:
  166. wall.run_action(A.sequence(
  167. A.fade_to(0, 0.3, TIMING_SINODIAL),
  168. A.remove()
  169. ))
  170.  
  171. for ball in {self.red, self.blue}:
  172. for i in range(100):
  173. r = random.uniform(2, 8)
  174. a = SpriteNode('shp:Circle', parent=self, size=(r,r), position=ball.position, color=ball.color)
  175. x = random.uniform(-1, 1)
  176. y = random.randrange(-1, 2, 2)*(1-x**2)**0.5
  177.  
  178. a.run_action(A.sequence(A.group(
  179. A.move_by(50*x*random.uniform(10, 2000), y*50*random.uniform(10, 2000), 100)), A.remove()))
  180. ball.remove_from_parent()
  181.  
  182. sound.play_effect('digital:LowDown')
  183. #self.setup()
  184. self.g_label = LabelNode('restart', ('Futura', 48), parent=self, position=self.size/2)
  185. self.g_label.run_action(A.sequence(
  186. A.fade_to(0, 0),
  187. A.fade_to(1, 1)
  188. ))
  189. self.score_label.run_action(A.sequence(
  190. A.fade_to(0, 0),
  191. A.fade_to(1, 1)
  192. ))
  193. self.score_label.color = 'white'
  194.  
  195. def ball_move(self):
  196. if len(self.tchs) > 0:
  197. tloc = self.tchs[-1].location
  198. if tloc.x > w/2:
  199. self.dist += s
  200. else:
  201. self.dist -= s
  202.  
  203. def ball_grav(self):
  204. grav = gravity()
  205. if abs(grav.x) > 0.01:
  206. self.dist += grav.x*30
  207.  
  208. def ball_effect(self):
  209. for ball in {self.red, self.blue}:
  210. fate = SpriteNode('shp:wavering', position=ball.position, size=ball.size*ball.scale, parent=self, color=ball.color, z_position=-1)
  211. t = ((by+32)/fs)/60-0.01
  212.  
  213. fate.run_action(A.sequence(A.group(
  214. A.fade_to(0, t), A.scale_to(0, t), A.move_by(0, -by-32, t)
  215. ), A.remove()))
  216.  
  217. def new_wall(self, y):
  218. t = random.uniform(0, 100)
  219. if t > 85:
  220. self.out_wall(y)
  221. elif t > 70:
  222. self.central_wall(y, windows=random.randrange(3))
  223. elif t > 60:
  224. self.rside_wall(y)
  225. elif t > 50:
  226. self.lside_wall(y)
  227. elif t > 35:
  228. self.lmix_wall(y, windows=random.randrange(3))
  229. elif t > 20:
  230. self.rmix_wall(y, random.randrange(3))
  231. else:
  232. self.m_wall(y)
  233.  
  234. def add_wall(self, y, width, x):
  235. self.walls.append(SpriteNode(parent=self, position=(x, y), size=(width, 32), color=colors['walls']))
  236.  
  237. def add_window(self, y, width, x, c):
  238. if c == 'red':
  239. color = colors['window_1']
  240. else:
  241. color = colors['window_2']
  242.  
  243. self.walls.append(window(
  244. parent=self,
  245. position=(x, y),
  246. size=(width, 32),
  247. color=color,
  248. c = c
  249. ))
  250.  
  251. def add_m_wall(self, y, width, x):
  252. self.walls.append(m_wall(parent=self, position=(x, y), size=(width, 32), color=colors['walls']))
  253.  
  254.  
  255. def m_wall(self, y):
  256. width = random.uniform(64, w/2-1.5*min_space)
  257. x = random.uniform(width/2, w-width/2)
  258. self.add_m_wall(y, width, x)
  259.  
  260.  
  261. def central_wall(self, y, windows = 0):
  262. width = random.uniform(32, w-2*min_space)
  263. x = random.uniform(min_space+width/2, w-(min_space+width/2))
  264. self.add_wall(y, width, x)
  265.  
  266. if windows > 0:
  267. sw1 = x-width/2
  268. sx1 = sw1/2
  269. sw2 = w-sw1-width
  270. sx2 = w - sw2/2
  271. if windows == 1:
  272. c = random.choice(['red', 'blue'])
  273. if random.randrange(2) == 1:
  274. self.add_window(y, sw1, sx1, c)
  275. else:
  276. self.add_window(y, sw2, sx2, c)
  277. else:
  278. c1 = random.choice(['red', 'blue'])
  279. c2 = list({'red', 'blue'}-{c1})[0]
  280. self.add_window(y, sw1, sx1, c1)
  281. self.add_window(y, sw2, sx2, c2)
  282.  
  283. def lside_wall(self, y):
  284. width = random.uniform(96, w/2-min_space/2)
  285. self.add_wall(y, width, width/2)
  286.  
  287. def rside_wall(self, y):
  288. width = random.uniform(96, w/2-min_space/2)
  289. self.add_wall(y, width, w-width/2)
  290.  
  291. def lmix_wall(self, y, windows = 0):
  292. w1 = random.uniform(32, w/2-min_space-32)
  293. wl = random.uniform(16, w/2-min_space-w1)
  294. wr = random.uniform(32, w/2-min_space-w1)
  295. w2 = wl+wr
  296. x1 = w1/2
  297. x2 = w/2+(wr-wl)/2
  298. self.add_wall(y, w1, x1)
  299. self.add_wall(y, w2, x2)
  300.  
  301. if windows > 0:
  302. sw1 = w/2-wl-w1
  303. sx1 = w1 + sw1/2
  304. sw2 = w/2 - wr
  305. sx2 = w - sw2/2
  306. if windows == 1:
  307. c = random.choice(['red', 'blue'])
  308. if random.randrange(2) == 1:
  309. self.add_window(y, sw1, sx1, c)
  310. else:
  311. self.add_window(y, sw2, sx2, c)
  312. else:
  313. c1 = random.choice(['red', 'blue'])
  314. c2 = list({'red', 'blue'}-{c1})[0]
  315. self.add_window(y, sw1, sx1, c1)
  316. self.add_window(y, sw2, sx2, c2)
  317.  
  318.  
  319. def rmix_wall(self, y, windows = 0):
  320. w1 = random.uniform(32, w/2-min_space-32)
  321. wl = random.uniform(16, w/2-min_space-w1)
  322. wr = random.uniform(32, w/2-min_space-w1)
  323. w2 = wl+wr
  324. x1 = w-w1/2
  325. x2 = w-(w/2+(wr-wl)/2)
  326. self.add_wall(y, w1, x1)
  327. self.add_wall(y, w2, x2)
  328.  
  329. if windows > 0:
  330. sw1 = w/2-wl-w1
  331. sx1 = w-(w1 + sw1/2)
  332. sw2 = w/2 - wr
  333. sx2 = sw2/2
  334. if windows == 1:
  335. c = random.choice(['red', 'blue'])
  336. if random.randrange(2) == 1:
  337. self.add_window(y, sw1, sx1, c)
  338. else:
  339. self.add_window(y, sw2, sx2, c)
  340. else:
  341. c1 = random.choice(['red', 'blue'])
  342. c2 = list({'red', 'blue'}-{c1})[0]
  343. self.add_window(y, sw1, sx1, c1)
  344. self.add_window(y, sw2, sx2, c2)
  345.  
  346. def out_wall(self, y):
  347. self.rside_wall(y)
  348. self.lside_wall(y)
  349.  
  350. def touch_began(self, touch):
  351. if not in_game and touch.location in self.g_label.frame:
  352. self.new_game()
  353. self.tchs.append(touch)
  354.  
  355. def touch_ended(self, touch):
  356. if touch in self.tchs:
  357. self.tchs.remove(touch)
  358.  
  359. if __name__ == '__main__':
  360. run(MyScene(), show_fps=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement