def test_BallAndBarObj( self ): screen = PygScreen() screen.setSize(10,10) ball = BallObj() ball.setObj("img/ball.bmp") bar = BarObj() bar.setObj( "img/bar.bmp" ) mediator = Mediator() # 衝突した際の動作 self.setPosition( ball, 150, 120, bar, 150, 120 ) #center #ObjA, X, Y, ObjB, X, Y self.assertTrue( True == mediator.isBallAndBarConflict( ball.getBallrect(), bar.getRect() ) ) self.setPosition( ball, 120, 10, bar, 120, 40,) #left self.assertTrue( True == mediator.isBallAndBarConflict( ball.getBallrect(), bar.getRect() ) ) self.setPosition( ball, 120, 70, bar, 120, 40,) #right self.assertTrue( True == mediator.isBallAndBarConflict( ball.getBallrect(), bar.getRect() ) ) self.setPosition( ball, 120, 80, bar, 120, 40,) #none self.assertTrue( False == mediator.isBallAndBarConflict( ball.getBallrect(), bar.getRect() ) ) ball.setState( self.sysData.stateIdle ) #状態が変化するか idel->L2R self.setPosition( ball, 80, 120, bar, 80, 120 ) mediator.judgeConflictBallAndBar( ball, bar ) self.assertTrue( self.sysData.stateLeft2Right == ball.getState() ) ball.setState( self.sysData.stateLeft2Right ) #状態が変化するか L2R->R2L self.setPosition( ball, 80, 120, bar, 80, 120 ) mediator.judgeConflictBallAndBar( ball, bar ) self.assertTrue( self.sysData.stateRight2Left == ball.getState() )
def test_moveAs2Key(self): pygame.init() screen = PygScreen() screen.setSize(10,10) ballObj = BallObj() ballObj.setObj("img/ball.bmp") ballObj.setRect() # Leftキーが押された時speedXが1減算 ballObj.setSpeed( 1,1 ) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) pressed_keys_cp[K_LEFT] = 1 ballObj.moveAs2Key( pressed_keys_cp ) self.assertTrue( [0,1] == ballObj.getSpeed() ) # Rightキーが押された時speedXが1加算 ballObj.setSpeed( 1,1 ) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) pressed_keys_cp[K_RIGHT] = 1 ballObj.moveAs2Key( pressed_keys_cp ) self.assertTrue( [2,1] == ballObj.getSpeed() ) # Upキーが押された時initSpeedが1加算 ballObj.setInitSpeed( 5 ) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) pressed_keys_cp[K_UP] = 1 ballObj.moveAs2Key( pressed_keys_cp ) self.assertTrue( 6 == ballObj.getInitSpeed() ) # Upキーが押された時initSpeedが1加算 ballObj.setInitSpeed( 20 ) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) pressed_keys_cp[K_UP] = 1 ballObj.moveAs2Key( pressed_keys_cp ) self.assertTrue( 20 == ballObj.getInitSpeed() ) # Downキーが押された時initSpeedが1加算 ballObj.setInitSpeed( 1 ) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) pressed_keys_cp[K_DOWN] = 1 ballObj.moveAs2Key( pressed_keys_cp ) print "test initspeed "+str(ballObj.getInitSpeed()) self.assertTrue( 0 == ballObj.getInitSpeed() ) # Downキーが押された時initSpeedが1加算 # initSpeedは0以下にならない ballObj.setInitSpeed( 0 ) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) pressed_keys_cp[K_DOWN] = 1 ballObj.moveAs2Key( pressed_keys_cp ) print "test initspeed "+str(ballObj.getInitSpeed()) self.assertTrue( 0 == ballObj.getInitSpeed() )
def test_moveAs2Key(self): pygame.init() screen = PygScreen() screen.setSize(10, 10) ballObj = BallObj() ballObj.setObj("img/ball.bmp") ballObj.setRect() # Leftキーが押された時speedXが1減算 ballObj.setSpeed(1, 1) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) pressed_keys_cp[K_LEFT] = 1 ballObj.moveAs2Key(pressed_keys_cp) self.assertTrue([0, 1] == ballObj.getSpeed()) # Rightキーが押された時speedXが1加算 ballObj.setSpeed(1, 1) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) pressed_keys_cp[K_RIGHT] = 1 ballObj.moveAs2Key(pressed_keys_cp) self.assertTrue([2, 1] == ballObj.getSpeed()) # Upキーが押された時initSpeedが1加算 ballObj.setInitSpeed(5) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) pressed_keys_cp[K_UP] = 1 ballObj.moveAs2Key(pressed_keys_cp) self.assertTrue(6 == ballObj.getInitSpeed()) # Upキーが押された時initSpeedが1加算 ballObj.setInitSpeed(20) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) pressed_keys_cp[K_UP] = 1 ballObj.moveAs2Key(pressed_keys_cp) self.assertTrue(20 == ballObj.getInitSpeed()) # Downキーが押された時initSpeedが1加算 ballObj.setInitSpeed(1) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) pressed_keys_cp[K_DOWN] = 1 ballObj.moveAs2Key(pressed_keys_cp) self.assertTrue(0 == ballObj.getInitSpeed()) # Downキーが押された時initSpeedが1加算 # initSpeedは0以下にならない ballObj.setInitSpeed(0) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) pressed_keys_cp[K_DOWN] = 1 ballObj.moveAs2Key(pressed_keys_cp) self.assertTrue(0 == ballObj.getInitSpeed())
def test_BallAndBarObj(self): screen = PygScreen() screen.setSize(10, 10) ball = BallObj() ball.setObj("img/ball.bmp") bar = BarObj() bar.setObj("img/bar.bmp") mediator = Mediator() # 衝突した際の動作 self.setPosition(ball, 150, 120, bar, 150, 120) #center #ObjA, X, Y, ObjB, X, Y self.assertTrue(True == mediator.isBallAndBarConflict( ball.getBallrect(), bar.getRect())) self.setPosition( ball, 120, 10, bar, 120, 40, ) #left self.assertTrue(True == mediator.isBallAndBarConflict( ball.getBallrect(), bar.getRect())) self.setPosition( ball, 120, 70, bar, 120, 40, ) #right self.assertTrue(True == mediator.isBallAndBarConflict( ball.getBallrect(), bar.getRect())) self.setPosition( ball, 120, 80, bar, 120, 40, ) #none self.assertTrue(False == mediator.isBallAndBarConflict( ball.getBallrect(), bar.getRect())) ball.setState(self.sysData.stateIdle) #状態が変化するか idel->L2R self.setPosition(ball, 80, 120, bar, 80, 120) mediator.judgeConflictBallAndBar(ball, bar) self.assertTrue(self.sysData.stateLeft2Right == ball.getState()) ball.setState(self.sysData.stateLeft2Right) #状態が変化するか L2R->R2L self.setPosition(ball, 80, 120, bar, 80, 120) mediator.judgeConflictBallAndBar(ball, bar) self.assertTrue(self.sysData.stateRight2Left == ball.getState())
def test_BallAndBarObj( self ): screen = PygScreen() screen.setSize(10,10) ball = BallObj() ball.setObj("img/ball.bmp") bar = BarObj() bar.setObj( "img/bar.bmp" ) mediator = Mediator() # 衝突した際の動作 self.setPosition( ball, 150, 120, #ObjA, X , Y bar, 150, 120 ) #ObjB, X , Y self.assertTrue( True == mediator.isBallAndBarConflict( ball.getBallrect(), bar.getRect() ) ) self.setPosition( ball, 120, 10, bar, 120, 40,) self.assertTrue( True == mediator.isBallAndBarConflict( ball.getBallrect(), bar.getRect() ) ) self.setPosition( ball, 120, 70, bar, 120, 40,) self.assertTrue( True == mediator.isBallAndBarConflict( ball.getBallrect(), bar.getRect() ) ) self.setPosition( ball, 120, 80, bar, 120, 40,) self.assertTrue( False == mediator.isBallAndBarConflict( ball.getBallrect(), bar.getRect() ) ) ball.setSpeed( 1,1 ) self.setPosition( ball, 80, 120, bar, 80, 120 ) mediator.judgeConflictBallAndBar( ball, bar ) self.assertTrue( [1, -5] == ball.getSpeed() )
def isCorrectSpeedChange(self, position, speed_before, key, speed_after): # init object. pygame.init() screen = PygScreen() screen.setSize(100, 100) ball = BallObj() ball.setObj(self.sysData.ballBmp) ball.setRect() # 判定処理 ball.setBallrectLeft(position[0]) ball.setBallrectBottom(position[1]) ball.setSpeed(speed_before[0], speed_before[1]) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) if key != None: pressed_keys_cp[key] = 1 ball.decideBallPosition(pressed_keys_cp, screen.getSize()) return speed_after == ball.getSpeed()
def isCorrectSpeedChange( self, position, speed_before, key, speed_after ): # init object. pygame.init() screen = PygScreen() screen.setSize(100,100) ball = BallObj() ball.setObj( self.sysData.ballBmp ) ball.setRect() # 判定処理 ball.setBallrectLeft( position[0] ) ball.setBallrectBottom( position[1] ) ball.setSpeed( speed_before[0], speed_before[1] ) pressed_keys_cp = \ self.makeListFromKeyTuple( pygame.key.get_pressed() ) if key != None: pressed_keys_cp[key] = 1 ball.decideBallPosition( pressed_keys_cp, screen.getSize() ) return speed_after == ball.getSpeed()
def test_BallAndWarpObj( self ): screen = PygScreen() screen.setSize(10,10) ball = BallObj() ball.setObj("img/ball.bmp") warp = WarpObj() warp.setObj( "img/warp.bmp" ) mediator = Mediator() self.setPosition( ball, 150, 120, #ObjA, X , Y warp, 150, 120 ) #ObjB, X , Y self.assertTrue( True == mediator.isBallAndBarConflict( ball.getBallrect(), warp.getRect() ) ) ball.setSpeed( 1,1 ) self.setPosition( ball, 80, 120, warp, 80, 120 ) mediator.judgeConflictBallAndWarp( ball, warp, 100 ) self.assertTrue( (100,120) == ball.getBallrect().center) self.assertTrue( [0,0] == ball.getSpeed() )
clock = pygame.time.Clock() while True: #clock.tick(sysData.waitTime) print clock.tick_busy_loop(sysData.waitTime) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # ボール生成部分 # TODO: ボール生成個数とボール射出タイミングはsysDataに移動 if len(balls) < 3: ballMakeTiming = ballMakeTiming + 1 if ballMakeTiming > 25: ball = BallObj() ball.setSpeed( 0, 0 ) ball.setObj( sysData.ballBmp ) ball.setRect_pos( 60,-100 ) balls.append( ball ) ballMakeTiming = 0 # ボールの次のフレームの動作 for ball in balls: ball.ballMove() ball.decideBallPosition(pygame.key.get_pressed(), screen.getSize() ) #print balls[0].getBallrect() # ボールとバーが衝突した際の動作 for ball in balls: mediator.judgeConflictBallAndBar( ball, barRight ) mediator.judgeConflictBallAndBar( ball, barLeft )