Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from scene import *
- import sound
- import random
- import math
- import time
- A = Action
- w, h = 0, 0
- main_touch = 0
- s = 10
- by = 140
- fs = 4
- min_space = 70
- y_space = 200
- q = 1
- in_game = True
- wms = 2
- obw_colors = {
- 'ball1': '#ffffff',
- 'ball2': '#000000',
- 'background': '#616666',
- 'window_1': '#ffffff',
- 'window_2': '#000000',
- 'labels': '#c2c2c2',
- 'walls': '#ff9d00'
- }
- fsg_colors = {
- 'ball1': '#ff0000',
- 'ball2': '#0000ff',
- 'background': '#000000',
- 'window_1': '#ff0000',
- 'window_2': '#0000ff',
- 'labels': '#c2c2c2',
- 'walls': '#ffffff'
- }
- colors = fsg_colors
- class Ball(SpriteNode):
- def __init__(self, shape, q='red', **kwargs):
- SpriteNode.__init__(self, shape, **kwargs)
- self.c = q
- class m_wall(SpriteNode):
- def __init__(self, **kwargs):
- SpriteNode.__init__(self, 'White.JPG', **kwargs)
- self.sign = random.randrange(-1, 2, 2)
- class window(SpriteNode):
- def __init__(self, c='red', **kwargs):
- SpriteNode.__init__(self, 'White.JPG', **kwargs)
- self.run_action(A.fade_to(0.5, 0))
- self.c = c
- def closest_point(rect, circle):
- return Point(max(rect.min_x, min(rect.max_x, circle.x)), max(rect.min_y, min(rect.max_y, circle.y)))
- def hit_test(rect, circle, radius, bbox=None):
- if bbox and not rect.intersects(bbox):
- return False
- return abs(closest_point(rect, circle) - circle) < radius
- class MyScene (Scene):
- def setup(self):
- global w, h, in_game, fs, min_space
- min_space = 70
- fs = 2
- in_game = True
- w, h = self.size
- #line = SpriteNode('shp:RoundRect', parent=self, position=self.size/2, size=(0.5, 2*h))
- self.background_color = colors['background']
- self.red = Ball('shp:Circle', q='red', parent=self, position=(w/4, by), color=colors['ball1'])
- self.blue = Ball('shp:Circle', q='blue', parent=self, position=(3*w/4, by), color=colors['ball2'])
- self._dist = w/2
- self.tchs = []
- self.walls = []
- self.new_wall(h+500)
- self._score = 0
- self.score_label = LabelNode(str(self._score), ('Futura', 32), parent=self, position=(w/2, h-48), color=colors['labels'])
- for ball in {self.red, self.blue}:
- ball.scale = 0
- ball.run_action(A.scale_to(1, 1))
- @property
- def score(self):
- return self._score
- @score.setter
- def score(self, value):
- self._score = value
- self.score_label.text = str(int(value))
- @property
- def dist(self):
- return self._dist
- @dist.setter
- def dist(self, value):
- k = self.red.size.w
- if abs(value) > w+k:
- value = -(w+k)*value/abs(value)
- x, y = self.blue.position
- x = w/2+value/2
- self.blue.position = (x, y)
- x, y = self.red.position
- x = w/2-value/2
- self.red.position = (x, y)
- self._dist = value
- def update(self):
- global fs
- fs += 0.001
- if not in_game:
- return
- self.score += fs/100
- self.ball_grav()
- self.ball_effect()
- self.wall_update()
- for ball in {self.red, self.blue}:
- for wall in self.walls:
- if hit_test(wall.frame, ball.position, ball.size.w/2, ball.bbox):
- if type(wall) == window:
- if wall.c != ball.c:
- self.game_over()
- else:
- self.game_over()
- def wall_update(self):
- for wall in set(self.walls):
- if type(wall) == m_wall:
- ww = wall.size.w/2
- x, y = wall.position
- sign = wall.sign
- x = max(min(x+sign*wms, w-ww), ww)
- wall.position = (x, y)
- if x == ww or x == w-ww:
- wall.sign *= -1
- if wall.position.y < by:
- wall.run_action(A.fade_to(0, (by/fs)/60))
- wall.run_action(A.move_by(0, -fs, 0))
- if wall.position.y < -32:
- self.walls.remove(wall)
- if h-self.walls[-1].position.y > y_space:
- self.new_wall(h+32)
- def new_game(self):
- for child in self.children:
- child.remove_from_parent()
- self.setup()
- def game_over(self):
- global in_game
- if not in_game:
- return
- in_game = False
- a = SpriteNode('White.JPG', parent=self, position=self.size/2, size=self.size, z_position=5)
- a.run_action(A.sequence(
- A.fade_to(0, 0.3),
- A.remove()
- ))
- for wall in self.walls:
- wall.run_action(A.sequence(
- A.fade_to(0, 0.3, TIMING_SINODIAL),
- A.remove()
- ))
- for ball in {self.red, self.blue}:
- for i in range(100):
- r = random.uniform(2, 8)
- a = SpriteNode('shp:Circle', parent=self, size=(r,r), position=ball.position, color=ball.color)
- x = random.uniform(-1, 1)
- y = random.randrange(-1, 2, 2)*(1-x**2)**0.5
- a.run_action(A.sequence(A.group(
- A.move_by(50*x*random.uniform(10, 2000), y*50*random.uniform(10, 2000), 100)), A.remove()))
- ball.remove_from_parent()
- sound.play_effect('digital:LowDown')
- #self.setup()
- self.g_label = LabelNode('restart', ('Futura', 48), parent=self, position=self.size/2)
- self.g_label.run_action(A.sequence(
- A.fade_to(0, 0),
- A.fade_to(1, 1)
- ))
- self.score_label.run_action(A.sequence(
- A.fade_to(0, 0),
- A.fade_to(1, 1)
- ))
- self.score_label.color = 'white'
- def ball_move(self):
- if len(self.tchs) > 0:
- tloc = self.tchs[-1].location
- if tloc.x > w/2:
- self.dist += s
- else:
- self.dist -= s
- def ball_grav(self):
- grav = gravity()
- if abs(grav.x) > 0.01:
- self.dist += grav.x*30
- def ball_effect(self):
- for ball in {self.red, self.blue}:
- fate = SpriteNode('shp:wavering', position=ball.position, size=ball.size*ball.scale, parent=self, color=ball.color, z_position=-1)
- t = ((by+32)/fs)/60-0.01
- fate.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()))
- def new_wall(self, y):
- t = random.uniform(0, 100)
- if t > 85:
- self.out_wall(y)
- elif t > 70:
- self.central_wall(y, windows=random.randrange(3))
- elif t > 60:
- self.rside_wall(y)
- elif t > 50:
- self.lside_wall(y)
- elif t > 35:
- self.lmix_wall(y, windows=random.randrange(3))
- elif t > 20:
- self.rmix_wall(y, random.randrange(3))
- else:
- self.m_wall(y)
- def add_wall(self, y, width, x):
- self.walls.append(SpriteNode(parent=self, position=(x, y), size=(width, 32), color=colors['walls']))
- def add_window(self, y, width, x, c):
- if c == 'red':
- color = colors['window_1']
- else:
- color = colors['window_2']
- self.walls.append(window(
- parent=self,
- position=(x, y),
- size=(width, 32),
- color=color,
- c = c
- ))
- def add_m_wall(self, y, width, x):
- self.walls.append(m_wall(parent=self, position=(x, y), size=(width, 32), color=colors['walls']))
- def m_wall(self, y):
- width = random.uniform(64, w/2-1.5*min_space)
- x = random.uniform(width/2, w-width/2)
- self.add_m_wall(y, width, x)
- def central_wall(self, y, windows = 0):
- width = random.uniform(32, w-2*min_space)
- x = random.uniform(min_space+width/2, w-(min_space+width/2))
- self.add_wall(y, width, x)
- if windows > 0:
- sw1 = x-width/2
- sx1 = sw1/2
- sw2 = w-sw1-width
- sx2 = w - sw2/2
- if windows == 1:
- c = random.choice(['red', 'blue'])
- if random.randrange(2) == 1:
- self.add_window(y, sw1, sx1, c)
- else:
- self.add_window(y, sw2, sx2, c)
- else:
- c1 = random.choice(['red', 'blue'])
- c2 = list({'red', 'blue'}-{c1})[0]
- self.add_window(y, sw1, sx1, c1)
- self.add_window(y, sw2, sx2, c2)
- def lside_wall(self, y):
- width = random.uniform(96, w/2-min_space/2)
- self.add_wall(y, width, width/2)
- def rside_wall(self, y):
- width = random.uniform(96, w/2-min_space/2)
- self.add_wall(y, width, w-width/2)
- def lmix_wall(self, y, windows = 0):
- w1 = random.uniform(32, w/2-min_space-32)
- wl = random.uniform(16, w/2-min_space-w1)
- wr = random.uniform(32, w/2-min_space-w1)
- w2 = wl+wr
- x1 = w1/2
- x2 = w/2+(wr-wl)/2
- self.add_wall(y, w1, x1)
- self.add_wall(y, w2, x2)
- if windows > 0:
- sw1 = w/2-wl-w1
- sx1 = w1 + sw1/2
- sw2 = w/2 - wr
- sx2 = w - sw2/2
- if windows == 1:
- c = random.choice(['red', 'blue'])
- if random.randrange(2) == 1:
- self.add_window(y, sw1, sx1, c)
- else:
- self.add_window(y, sw2, sx2, c)
- else:
- c1 = random.choice(['red', 'blue'])
- c2 = list({'red', 'blue'}-{c1})[0]
- self.add_window(y, sw1, sx1, c1)
- self.add_window(y, sw2, sx2, c2)
- def rmix_wall(self, y, windows = 0):
- w1 = random.uniform(32, w/2-min_space-32)
- wl = random.uniform(16, w/2-min_space-w1)
- wr = random.uniform(32, w/2-min_space-w1)
- w2 = wl+wr
- x1 = w-w1/2
- x2 = w-(w/2+(wr-wl)/2)
- self.add_wall(y, w1, x1)
- self.add_wall(y, w2, x2)
- if windows > 0:
- sw1 = w/2-wl-w1
- sx1 = w-(w1 + sw1/2)
- sw2 = w/2 - wr
- sx2 = sw2/2
- if windows == 1:
- c = random.choice(['red', 'blue'])
- if random.randrange(2) == 1:
- self.add_window(y, sw1, sx1, c)
- else:
- self.add_window(y, sw2, sx2, c)
- else:
- c1 = random.choice(['red', 'blue'])
- c2 = list({'red', 'blue'}-{c1})[0]
- self.add_window(y, sw1, sx1, c1)
- self.add_window(y, sw2, sx2, c2)
- def out_wall(self, y):
- self.rside_wall(y)
- self.lside_wall(y)
- def touch_began(self, touch):
- if not in_game and touch.location in self.g_label.frame:
- self.new_game()
- self.tchs.append(touch)
- def touch_ended(self, touch):
- if touch in self.tchs:
- self.tchs.remove(touch)
- if __name__ == '__main__':
- run(MyScene(), show_fps=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement