def __init__(self, limits, name="Ball Manager"): super().__init__(name) self._limits = limits self._balls = cdkk.SpriteGroup() self._timer = cdkk.Timer() self._next_test = 1 self._timer = cdkk.Timer() tt_style={"fillcolour":None, "align_horiz":"L", "textformat":"Test: {0} Time Left: {1:0.1f}"} self._test_time = cdkk.Sprite_DynamicText("Test+Time", rect=cdkk.cdkkRect(10, 10, 300, 40), style=tt_style) self._test_time.set_text(self._next_test, 0) self.add(self._test_time) cdkk.EventManager.post_game_control("NextTest")
def __init__(self, limits, game_time, player_num): super().__init__("Scoreboard Manager", player=player_num) self._game_time = game_time text_style = {"fillcolour": None, "outlinecolour": None, "align_horiz": "L", "width": 200, "height": 35} tb_score = cdkk.Sprite_TextBox("Score", style=text_style) tb_score.set_text_format("Score: {0}", 0) tb_score.rect.topleft = (limits.left+10, limits.top+5) self.add(tb_score) self.score = 0 self._timer = cdkk.Timer( self._game_time, cdkk.EVENT_GAME_TIMER_1, auto_start=self.game_is_active) tb_time_left = cdkk.Sprite_TextBox("Time Left", style=text_style) tb_time_left.set_text_format("Time Left: {0:0.1f}", 0) tb_time_left.rect.midtop = ( limits.left + limits.width*0.5, limits.top+5) self.add(tb_time_left) tb_balls_left = cdkk.Sprite_TextBox("Balls Left", style=text_style) tb_balls_left.set_text_format("Balls Left: {0}", 0) tb_balls_left.rect.topright = (limits.right-10, limits.top+5) self.add(tb_balls_left) self.balls_left = 0 self._game_over = cdkk.Sprite_GameOver(limits) self._game_over.rect.center = ( limits.left + limits.width*0.5, limits.top + limits.height*0.5)
def __init__(self, limits, game_time): super().__init__("Scoreboard Manager") self._game_time = game_time text_style = { "fillcolour": None, "outlinecolour": None, "align_horiz": "L", "width": 200, "height": 35 } tb_score = cdkk.Sprite_TextBox("Score", style=text_style) tb_score.set_text_format("Score: {0}", 0) tb_score.rect.midleft = (limits.width * 0.1, limits.height * 0.05) self.add(tb_score) self.score = 0 self._timer = cdkk.Timer(self._game_time, cdkk.EVENT_GAME_TIMER_1) tb_time_left = cdkk.Sprite_TextBox("Time Left", style=text_style) tb_time_left.set_text_format("Time Left: {0:0.1f}", 0) tb_time_left.rect.center = (limits.width * 0.45, limits.height * 0.05) self.add(tb_time_left) tb_balls_left = cdkk.Sprite_TextBox("Balls Left", style=text_style) tb_balls_left.set_text_format("Balls Left: {0}", 0) tb_balls_left.rect.midright = (limits.width * 0.85, limits.height * 0.05) self.add(tb_balls_left) self.balls_left = 0 self._game_over = cdkk.Sprite_GameOver(limits) self._game_over.rect.center = (limits.width * 0.5, limits.height * 0.5)
def __init__(self, limits, name = "Spaceship Manager"): super().__init__(name) self._limits = limits self._spaceship = Sprite_Spaceship(self._limits) self.add(self._spaceship, layer=9) # Layer 9: Above everything else self._bullets = cdkk.SpriteGroup("Bullets") self._bullet_time_limit = 250 # Minimum time between bullets (msecs) self._bullet_timer = cdkk.Timer(self._bullet_time_limit/1000.0)
def run_test(self): if self._next_test == 1: obstacle = Sprite_Obstacle(cdkk.cdkkRect(700,100,50,250)) ball = Sprite_Ball(100, 500, 5, -13, cdkk.Physics_Limit(self._limits, cdkk.LIMIT_KEEP_INSIDE, cdkk.AT_LIMIT_BOUNCE), cdkk.Physics_Limit(obstacle.rect, cdkk.LIMIT_KEEP_OUTSIDE, cdkk.AT_LIMIT_BOUNCE)) self._timer = cdkk.Timer(10, cdkk.EVENT_GAME_TIMER_1) self.add(obstacle, ball) self._balls.add(ball) elif self._next_test == 2: obstacle = Sprite_Obstacle(cdkk.cdkkRect(100,100,50,250)) ball = Sprite_Ball(700, 500, -5, -13, cdkk.Physics_Limit(self._limits, cdkk.LIMIT_KEEP_INSIDE, cdkk.AT_LIMIT_BOUNCE), cdkk.Physics_Limit(obstacle.rect, cdkk.LIMIT_KEEP_OUTSIDE, cdkk.AT_LIMIT_BOUNCE)) self._timer = cdkk.Timer(5, cdkk.EVENT_GAME_TIMER_1) self.add(obstacle, ball) self._balls.add(ball) elif self._next_test == 3: obstacle = Sprite_Obstacle(cdkk.cdkkRect(320,160,300,50)) ball = Sprite_Ball(100, 500, 10, -20, cdkk.Physics_Limit(self._limits, cdkk.LIMIT_KEEP_INSIDE, cdkk.AT_LIMIT_BOUNCE), cdkk.Physics_Limit(obstacle.rect, cdkk.LIMIT_KEEP_OUTSIDE, cdkk.AT_LIMIT_BOUNCE)) self._timer = cdkk.Timer(10, cdkk.EVENT_GAME_TIMER_1) self.add(obstacle, ball) self._balls.add(ball) elif self._next_test == 4: obstacle1 = Sprite_Obstacle(cdkk.cdkkRect(320,160,300,50)) obstacle2 = Sprite_Obstacle(cdkk.cdkkRect(500,350,250,50)) ball = Sprite_Ball(100, 500, 6, -20, cdkk.Physics_Limit(self._limits, cdkk.LIMIT_KEEP_INSIDE, cdkk.AT_LIMIT_BOUNCE), cdkk.Physics_Limit(obstacle1.rect, cdkk.LIMIT_KEEP_OUTSIDE, cdkk.AT_LIMIT_BOUNCE), cdkk.Physics_Limit(obstacle2.rect, cdkk.LIMIT_KEEP_OUTSIDE, cdkk.AT_LIMIT_BOUNCE)) self._timer = cdkk.Timer(10, cdkk.EVENT_GAME_TIMER_1) self.add(ball, obstacle1, obstacle2) self._balls.add(ball) elif self._next_test == 5: obstacle = Sprite_Obstacle(cdkk.cdkkRect(700,100,50,250)) ball = Sprite_Ball(100, 500, 5, -13, cdkk.Physics_Limit(self._limits, cdkk.LIMIT_KEEP_INSIDE, cdkk.AT_LIMIT_BOUNCE), cdkk.Physics_Limit(obstacle.rect, cdkk.LIMIT_KEEP_OUTSIDE, cdkk.AT_LIMIT_X_CLEAR_VEL_X)) self._timer = cdkk.Timer(5, cdkk.EVENT_GAME_TIMER_1) self.add(obstacle, ball) self._balls.add(ball) elif self._next_test == 6: obstacle = Sprite_Obstacle(cdkk.cdkkRect(100,100,50,250)) ball = Sprite_Ball(700, 500, -5, -14, cdkk.Physics_Limit(self._limits, cdkk.LIMIT_KEEP_INSIDE, cdkk.AT_LIMIT_BOUNCE), cdkk.Physics_Limit(obstacle.rect, cdkk.LIMIT_KEEP_OUTSIDE, cdkk.AT_LIMIT_XY_CLEAR_VEL_XY)) self._timer = cdkk.Timer(5, cdkk.EVENT_GAME_TIMER_1) self.add(obstacle, ball) self._balls.add(ball) elif self._next_test == 7: obstacle = Sprite_Obstacle(cdkk.cdkkRect(700,100,50,250)) ball = Sprite_Ball(100, 500, 20, -12, cdkk.Physics_Limit(self._limits, cdkk.LIMIT_KEEP_INSIDE, cdkk.AT_LIMIT_BOUNCE), cdkk.Physics_Limit(obstacle.rect, cdkk.LIMIT_KEEP_OUTSIDE, cdkk.AT_LIMIT_BOUNCE)) ball.rect.bounce_cor = 0.5 self._timer = cdkk.Timer(6, cdkk.EVENT_GAME_TIMER_1) self.add(obstacle, ball) self._balls.add(ball)
def fire_bullet(self, bullet_type): if self._bullet_timer.time_left == 0: posx = self.spaceship_rect.centerx + 15 posy = self.spaceship_rect.centery - 5 bullet = Sprite_Ammunition(bullet_type, posx, posy, self._limits) self.add(bullet, layer=1) self._bullets.add(bullet) cdkk.EventManager.post_game_control("UpdateScore", score=-1) self._bullet_timer = cdkk.Timer(self._bullet_time_limit/1000.0)
def __init__(self, limits, bird_speed, pipe_freq, difficulty): super().__init__("FlapyBird Manager") self._limits = limits self._bird_speed = bird_speed # Bird speed (gravity) self._difficulty = difficulty # Pipe gap (1..9) self._pipe_timer = cdkk.Timer( pipe_freq, cdkk.EVENT_GAME_TIMER_2, auto_start=False) # Pipe frequency in seconds self._bird = None self._pipe_gap = 0 self.add(cdkk.Sprite_Background("FlappyBackground.png", self._limits), layer=0)
def event(self, e): dealt_with = super().event(e) if not dealt_with and e.type == cdkk.EVENT_GAME_CONTROL: dealt_with = True if e.action == "Board": x, y = e.pos col, row = self.sprite("Board").find_cell((x, y)) self.play_piece(col, row) elif e.action == "Pass": self._reversi.next_player() elif e.action == "Hint": moves = self._reversi.next_moves() self.sprite("Board").highlight_cells(moves) self.timer = cdkk.Timer(1, cdkk.EVENT_GAME_TIMER_1) elif e.action == "ClearHint": self.timer.stop_event() moves = self._reversi.next_moves() self.sprite("Board").highlight_cells(moves, False) elif e.action == "Print": self._reversi.print_board() else: dealt_with = False return dealt_with