def __init__(self, limits, name="Board Manager"): super().__init__(name) board = cdkk.Sprite_BoardGame_Board("Board", cdkk.stylesheet.style("BoardGame_Board")) cell_size = int(min((limits.height * 0.75) / MNK_ROWS, (limits.width * 0.75) / MNK_COLS)) board.setup_board_grid( cell_size, MNK_COLS, cdkk.EventManager.gc_event("Board"), MNK_ROWS, None) board.rect.center = limits.center self.add(board, layer=9) Sprite_mnkGame_Piece.mnkGame_board = board self._mnk_game = BoardGame_mnkGame(MNK_COLS, MNK_ROWS, MNK_INAROW) self._mnk_game.start_game() self._current_piece = None self._next_player = cdkk.Sprite_DynamicText("Next", cdkk.cdkkRect( limits.width * 0.3, limits.height * 0.9, 150, 35), style=cdkk.stylesheet.style("app_NextPlayer")) self._next_player.set_text_format("Next: {0}", "") self.add(self._next_player) self._winner = cdkk.Sprite_DynamicText("Winner", rect=cdkk.cdkkRect( limits.width/2-200, 25, 400, 70), style=cdkk.stylesheet.style("GameOver")) self._winner.set_text_format("Winner: {0}", "") ev_Restart = cdkk.EventManager.gc_event("StartGame") ev_Quit = cdkk.EventManager.gc_event("Quit") rect = cdkk.cdkkRect(limits.width * 0.5, limits.height * 0.9, 120, 35) self.add(cdkk.Sprite_Button("Restart", rect, ev_Restart)) rect.left = limits.width * 0.65 self.add(cdkk.Sprite_Button("Quit", rect, ev_Quit)) self.start_game()
def code_peg_rect(self, turn, peg=None): if peg is None: return cdkk.cdkkRect( self._board_rect.left + 20, self._board_rect.top + self.cp_hole_size * turn + 10, self.cp_hole_size * MM_HOLES, self.cp_hole_size - 10) else: return cdkk.cdkkRect( self._board_rect.left + self.cp_hole_size * peg + 20, self._board_rect.top + self.cp_hole_size * turn + 10, self.cp_hole_size - 10, self.cp_hole_size - 10)
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 __init__(self, posx, posy, velx, vely, *limits): super().__init__(name="Ball", rect=cdkk.cdkkRect(posx, posy, 50, 50), style=Sprite_Ball.default_style) self.rect.set_velocity(velx, vely) self.rect.set_acceleration(0, self.rect.gravity) for l in limits: self.rect.add_limit(l) self.rect.go()
def score_pegs_rect(self, turn): rect = cdkk.cdkkRect( self._board_rect.left + self.cp_hole_size * MM_HOLES + 30, self._board_rect.top + self.cp_hole_size * turn + 10 + self.cp_hole_size / 2 - self.sp_hole_size, self.sp_hole_size * 2 - 2, self.sp_hole_size * 2 - 2) return rect
def __init__(self, game_time): score_style = cdkk.stylesheet.style("Scoreboard") timer_style = cdkk.stylesheet.style("Scoreboard") super().__init__(game_time, score_style=score_style, timer_style=timer_style) sb_rect = cdkk.cdkkRect(self.app_boundary.right - 200, 10, 200, 40) self.timer_text.rect = sb_rect self.score_text.rect = sb_rect.move(0, 40) self.game_over.text = "You hit a pipe!"
def init(self): super().init() court1 = cdkk.cdkkRect( 10, 10, (self.boundary.width/2-30), self.boundary.height-20) court2 = cdkk.cdkkRect( self.boundary.width/2+5, 10, (self.boundary.width/2-30), self.boundary.height-20) self._court_mgr1 = Manager_Court(court1) self._court_mgr2 = Manager_Court(court2) self.add_sprite_mgr(self._court_mgr1) self.add_sprite_mgr(self._court_mgr2) self._ball_mgr1 = Manager_Ball(court1, 10, 3, 1) self._ball_mgr2 = Manager_Ball(court2, 10, 3, 2) self.add_sprite_mgr(self._ball_mgr1) self.add_sprite_mgr(self._ball_mgr2) self._bat_mgr1 = Manager_Bat(court1, cdkk.CONTROL_JOYSTICK) self._bat_mgr2 = Manager_Bat(court2, cdkk.CONTROL_MOUSE) self._bat_mgr1.set_config("player", 1) self._bat_mgr2.set_config("player", 2) self.add_sprite_mgr(self._bat_mgr1) self.add_sprite_mgr(self._bat_mgr2) self._scoreboard_mgr1 = Manager_Scoreboard(court1, 15, 1) self._scoreboard_mgr2 = Manager_Scoreboard(court2, 15, 2) self.add_sprite_mgr(self._scoreboard_mgr1) self.add_sprite_mgr(self._scoreboard_mgr2) self.add_sprite_mgr(Manager_Splash(self.boundary, 3)) key_map = { pygame.K_q: "Quit", pygame.K_s: "StartGame", pygame.K_LEFT: "BatLeft", pygame.K_RIGHT: "BatRight" } self.event_mgr.event_map(key_event_map=key_map) self.event_mgr.user_event(cdkk.EVENT_GAME_TIMER_1, "GameOver") self.event_mgr.user_event(cdkk.EVENT_GAME_TIMER_2, "ClearSplash") self.config_joystick(court1)
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): super().init() court = cdkk.cdkkRect(20, 20, (self.boundary.width - 40), self.boundary.height - 40) self._court_mgr = Manager_Court(court) self.add_sprite_mgr(self._court_mgr) self._ball_mgr = Manager_Ball(court, 10, 3) self.add_sprite_mgr(self._ball_mgr) self._bat_mgr = Manager_Bat(court) self.add_sprite_mgr(self._bat_mgr) self._scoreboard_mgr = Manager_Scoreboard(court, 15) self.add_sprite_mgr(self._scoreboard_mgr) key_map = {pygame.K_q: "Quit", pygame.K_s: "StartGame"} self.event_mgr.event_map(key_event_map=key_map) self.event_mgr.user_event(cdkk.EVENT_GAME_TIMER_1, "GameOver")
def __init__(self, limits): super().__init__("Spaceship") self._limits = limits self.load_animation("Spaceship", "Spaceship{0:02d}.png", 7, crop=self.crop_image) self.load_spritesheet("Explosion", "Explosion.png", 4, 4) self.show_spaceship() self.rect.centerx = limits.width/4 self.rect.centery = limits.height/2 self.rect.add_limit(cdkk.Physics_Limit(limits, cdkk.LIMIT_KEEP_INSIDE, cdkk.AT_LIMIT_X_HOLD_POS_X+cdkk.AT_LIMIT_Y_HOLD_POS_Y)) return_speed = 3 limit_rect = cdkk.cdkkRect(0,0,return_speed*2,0) limit_rect.center = self.rect.center xlimit = cdkk.Physics_Limit(limit_rect, cdkk.LIMIT_MOVE_TO, cdkk.AT_LIMIT_X_MOVE_TO_X) xlimit.motion = cdkk.Physics_Motion() xlimit.motion.velocity_x = return_speed self.rect.add_limit(xlimit) self.rect.go() self.set_config("auto_move_physics", True)
def __init__(self, limits, obstacle): super().__init__("") rect = cdkk.cdkkRect(100, limits.bottom-100, 50, 50) self.setup_shape(rect, shape_colours=["red3"], shape="Ellipse") # Test 1: Keep Inside # self.rect.set_velocity(5,-10) # self.rect.set_velocity(5,-5) # Hit bottom limit # self.rect.set_velocity(5,-15) # Hit top limit # self.rect.set_velocity(10,-10) # Hit right limit # self.rect.set_velocity(-5,-10) # Hit left limit # self.rect.add_limit(cdkk.Physics_Limit(limits, cdkk.LIMIT_KEEP_INSIDE, AT_LIMIT_X_HOLD_POS_X + AT_LIMIT_Y_HOLD_POS_Y)) # self.rect.add_limit(cdkk.Physics_Limit(limits, cdkk.LIMIT_KEEP_INSIDE, AT_LIMIT_X_CLEAR_VEL_X + AT_LIMIT_Y_HOLD_POS_Y)) # self.rect.add_limit(cdkk.Physics_Limit(limits, cdkk.LIMIT_KEEP_INSIDE, AT_LIMIT_XY_CLEAR_VEL_XY)) self.rect.add_limit(cdkk.Physics_Limit(limits, cdkk.LIMIT_KEEP_INSIDE, cdkk.AT_LIMIT_BOUNCE)) # Test 2: Keep Outside self.rect.set_velocity(5,-15) # self.rect.add_limit(cdkk.Physics_Limit(obstacle, cdkk.LIMIT_KEEP_OUTSIDE, AT_LIMIT_Y_HOLD_POS_Y+AT_LIMIT_Y_CLEAR_VEL_Y)) self.rect.add_limit(cdkk.Physics_Limit(obstacle, cdkk.LIMIT_KEEP_OUTSIDE, cdkk.AT_LIMIT_BOUNCE))
def __init__(self, speed, limits, gap_top, gap_size): super().__init__("Pipe") self._pipe_t = cdkk.Sprite("PipeTop") self._pipe_b = cdkk.Sprite("PipeBottom") pipe_h = self._pipe_t.rect.height pipe_w = self._pipe_t.rect.width xpos = limits.right + pipe_w stretch_t = gap_top - pipe_h stretch_b = limits.height - gap_top - gap_size - pipe_h self._pipe_t.load_image_from_spritesheet( "FlappyBird.png", 4, 4, 6, img_process=("stretch", [0, 0, stretch_t, 0])) self._pipe_b.load_image_from_spritesheet( "FlappyBird.png", 4, 4, 5, img_process=("stretch", [0, 0, 0, stretch_b])) self._pipe_t.rect.bottomleft = (xpos, gap_top) self._pipe_b.rect.topleft = (xpos, gap_top + gap_size) self.add(self._pipe_t) self.add(self._pipe_b) pipe_limit = cdkk.cdkkRect(limits.left - pipe_w, limits.top, 1, limits.height) for s in self.sprites(): s.set_config("Type", "Pipe") s.rect.set_velocity(-speed, 0) ev = cdkk.EventManager.gc_event("KillSpriteUUID", uuid=s.uuid) limit = cdkk.Physics_Limit(pipe_limit, cdkk.LIMIT_KEEP_OUTSIDE, cdkk.AT_LIMIT_XY_DO_NOTHING, event=ev) s.rect.add_limit(limit) s.rect.go() s.set_config("auto_move_physics", True)
def __init__(self, limits, name="Test PyGame App Manager"): super().__init__(name) image_sprite = cdkk.Sprite() image_sprite.load_image_from_file("beachball.png") image_sprite.rect.topleft = (10, 10) self.add(image_sprite) anim_sprite = cdkk.Sprite_Animation() anim_sprite.load_spritesheet("Explode", "ExplosionCount.png", 4, 4, set_anim=True) anim_sprite.rect.topleft = (200, 10) self.add(anim_sprite) rect_sprite = cdkk.Sprite_Shape("Shape:Rectangle", cdkk.cdkkRect(300, 10, 60, 30), {"fillcolour": "blue"}) self.add(rect_sprite) ellipse_sprite = cdkk.Sprite_Shape("Shape:Ellipse", cdkk.cdkkRect(400, 10, 60, 50), { "fillcolour": "green3", "outlinecolour": "red4", "shape": "Ellipse" }) self.add(ellipse_sprite) polygon_sprite = cdkk.Sprite_Shape("Shape:Polygon", cdkk.cdkkRect(500, 10, 101, 101), { "fillcolour": None, "shape": "Polygon" }) polygon_sprite.setup_polygon([(0, 50), (50, 0), (100, 50), (50, 100)]) self.add(polygon_sprite) image_from_ss = cdkk.Sprite() image_from_ss._image.set_spritesheet("ExplosionCount.png", 4, 4) image_from_ss._image.spritesheet_image(6) image_from_ss._image_size_to_rect() image_from_ss.rect.topleft = (650, 10) self.add(image_from_ss) tb_default = cdkk.Sprite_TextBox("TextBox: Default", cdkk.cdkkRect(10, 150, 300, 60)) self.add(tb_default) tb_style = { "textcolour": "blue", "fillcolour": None, "outlinecolour": "blue" } tb_style1 = cdkk.Sprite_TextBox("Blue/None/Blue", cdkk.cdkkRect(320, 150, 300, 60), style=tb_style) self.add(tb_style1) tb_style["fillcolour"] = "yellow1" tb_style["textsize"] = 20 tb_style["shape"] = "Ellipse" tb_style2 = cdkk.Sprite_TextBox("Blue/Yellow/Blue, O, 20", cdkk.cdkkRect(640, 150, 300, 60), style=tb_style) self.add(tb_style2) tb_topleft = cdkk.Sprite_TextBox("TextBox: Top Left", cdkk.cdkkRect(10, 220, 300, 60), style={ "align_horiz": "L", "align_vert": "T" }) self.add(tb_topleft) tb_bottomright = cdkk.Sprite_TextBox("TextBox: Bottom Right", cdkk.cdkkRect(320, 220, 300, 60), style={ "align_horiz": "R", "align_vert": "B" }) self.add(tb_bottomright) tb_click_me = cdkk.Sprite_TextBox("Click Me", cdkk.cdkkRect(640, 220, 300, 60)) ev_Clicked = cdkk.EventManager.gc_event("Clicked") ev_Unclicked = cdkk.EventManager.gc_event("Unclicked") tb_click_me.setup_mouse_events(ev_Clicked, ev_Unclicked) self.add(tb_click_me) self.game_over = cdkk.Sprite_GameOver(cdkk.cdkkRect(320, 300, 300, 60)) ev_Clicked = cdkk.EventManager.gc_event("ToggleGameOver") self._click_game_over = cdkk.Sprite_Button( "End Game", cdkk.cdkkRect(10, 300, 300, 60), ev_Clicked) self.add(self._click_game_over) self.start_game() self._fps = cdkk.Sprite_DynamicText("FPS", cdkk.cdkkRect(640, 300, 120, 40), style={ "fillcolour": None, "align_horiz": "L", "textformat": "FPS: {0:4.1f}" }) self.add(self._fps) self._fps_units = cdkk.Sprite_Label("frames/sec", cdkk.cdkkRect(770, 300, 150, 40)) self._fps_units.rect.centery = 320 self.add(self._fps_units) tb_auto_size = cdkk.Sprite_TextBox("AutoSize", cdkk.cdkkRect(10, 400, 0, 0), auto_size=True) ev_Auto_Clicked = cdkk.EventManager.gc_event("AutoSize") tb_auto_size.setup_mouse_events(ev_Auto_Clicked, None) self.add(tb_auto_size)
def __init__(self, posx, posy): super().__init__(name="Ball", rect=cdkk.cdkkRect( posx, posy, 50, 50), style=Sprite_Ball.default_style) self.rect.go()
def __init__(self, limits, name="Board Manager"): super().__init__(name) self._mm_game = BoardGame_Mastermind(MM_HOLES, MM_TURNS, MM_OPTIONS) # --- Set up the board self.cp_hole_size = int( min((limits.height * 0.9) / MM_TURNS, (limits.width * 0.9) / MM_HOLES)) self._board_rect = cdkk.cdkkRect( 0, 0, (self.cp_hole_size + self.sp_hole_size) * MM_HOLES + 10, self.cp_hole_size * MM_TURNS + 10) self._board_rect.center = limits.center self.add( cdkk.Sprite_Shape("Board Background", self._board_rect, { "fillcolour": "tan4", "outlinecolour": None })) # --- Set up each turn for i in range(MM_TURNS): evl_click = [] for j in range(MM_HOLES): evl_click.append( cdkk.EventManager.gc_event("PlaceCodePeg", guess=(i, j))) self.add( Sprite_CodePeg_Set(i, self.code_peg_rect(i), MM_HOLES, evl_click=evl_click)) self.add( Sprite_ScorePeg_Set(i, self.score_pegs_rect(i), int((MM_HOLES + 1) / 2), 2)) # --- Code peg options and picker options = [i for i in range(MM_OPTIONS)] evl_click = [ cdkk.EventManager.gc_event("SelectCodePeg", guess=i) for i in range(MM_OPTIONS) ] options_rect = cdkk.cdkkRect( limits.left + limits.width * 0.2, limits.top + limits.height / 2 - self.cp_hole_size * MM_OPTIONS / 2, self.cp_hole_size - 10, self.cp_hole_size * MM_OPTIONS) cp = Sprite_CodePeg_Set(999, options_rect, 1, MM_OPTIONS, evl_click, "CodePegOptions") cp.update(options) self.add(cp) self._peg_picker = Sprite_CodePeg(self.code_peg_rect(0, 0)) self.add(self._peg_picker) # --- Set up controls button_height = 35 rect = cdkk.cdkkRect(self._board_rect.right + 100, limits.height / 2, 120, button_height) ev_Guess = cdkk.EventManager.gc_event("GuessCode") rect.bottom = limits.height / 2 - button_height * 2 self.add(cdkk.Sprite_Button("Guess", rect, ev_Guess)) ev_Restart = cdkk.EventManager.gc_event("StartGame") rect.centery = limits.height / 2 self.add(cdkk.Sprite_Button("Restart", rect, ev_Restart)) ev_Quit = cdkk.EventManager.gc_event("Quit") rect.top = limits.height / 2 + button_height * 2 self.add(cdkk.Sprite_Button("Quit", rect, ev_Quit)) self._winner = cdkk.Sprite_GameOver(limits) self.start_game()