Advertisement
Guest User

Untitled

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