Esempio n. 1
0
    def update( self ):
        # find the ball
        ball_entity = Entity.find_entity( "ball" )
        if ball == None:
            raise ValueError( "No ball entity" )

        # see if it's out of bounds
        ball_position = ball_entity.data[ 'position' ]

        if \
            ball_position >= -self.half_size and \
            ball_position <= self.half_size:
            # ball still in play
            return


        if ball_position < -self.half_size:
            # out of bounds
            print "Ball out of bounds on left"
            self.entity.data[ 'right_score' ] += 1

        if ball_position > self.half_size:
            # out of bounds
            print "Ball out of bounds on right"
            self.entity.data[ 'left_score' ] += 1

        self.entity.data[ 'games' ] += 1

        # serve the ball
        ball_contact = ball_entity.find_component(
            ball.BallContactComponent.type
            )
        ball_contact.serve()
Esempio n. 2
0
def create():
    entity = Entity( "ball" )
    entity.add_component(
        BallMovementComponent( 'movement' )
        )
    entity.add_component(
        BallContactComponent( 'contact' )
        )
    entity.data[ 'position' ] = 0.0
    entity.data[ 'velocity' ] = 0.0
    return entity
Esempio n. 3
0
def create( half_size ):
    entity = Entity( "table" )
    entity.add_component(
        BallCheckComponent( 'ball_check', half_size )
        )

    entity.data[ 'left_score' ] = 0
    entity.data[ 'right_score' ] = 0
    entity.data[ 'games' ] = 0

    return entity
Esempio n. 4
0
    def update( self ):
        # find the ball
        ball_entity = Entity.find_entity( "ball" )
        if ball == None:
            raise ValueError( "No ball entity" )

        # see if it's within our hit distance
        ball_position = ball_entity.data[ 'position' ]

        bat_position = self.entity.data[ 'position' ]
        hit_distance = self.entity.data[ 'hit_distance' ]
        max_distance = bat_position + hit_distance
        min_distance = bat_position - hit_distance

        if \
            ball_position > max_distance or \
            ball_position < min_distance:
            # ball is too far away
            # do nothing
            return

        # see if it's coming in our direction
        # simply compare velocity direction with bat
        # position and assume only 2 bats
        # not the best component but its just an example
        ball_velocity = ball_entity.data[ 'velocity' ]
        if \
            ball_velocity < 0.0 and \
            bat_position > 0.0:
            # heading away
            return
        if \
            ball_velocity > 0.0 and \
            bat_position < 0.0:
            # heading away
            return

        # hit it!
        bat_hit = self.entity.find_component(
            BatHitComponent.type
            )
        if bat_hit == None:
            raise ValueError( "No bat hit component" )

        # hit the ball
        bat_hit.hit_ball()
Esempio n. 5
0
def create( name, position, hit_distance, hit_chance, sleep_time ):
    entity = Entity( name )
    entity.add_component(
        BatPositionComponent( 'position' )
        )
    entity.add_component(
        BatHitComponent(
            'hit',
            sleep_time,
            hit_chance
            )
        )

    entity.data[ 'position' ] = position
    entity.data[ 'hit_distance' ] = hit_distance

    return entity
Esempio n. 6
0
    def hit_ball( self ):
        # see if we can hit yet
        if self.last_hit > 0:
            # we already tried to hit it
            # don't continue
            return

        # set our last hit to our sleep time
        # this stops us taking unlimited hits
        self.last_hit = self.sleep_time

        # roll a dice and see if we hit it or not
        chance = random.random()

        if chance > self.hit_chance:
            # we've missed
            print "%s missed the ball!" % self.entity.name
            return

        # find the ball
        ball_entity = Entity.find_entity( "ball" )
        if ball == None:
            raise ValueError( "No ball entity" )

        ball_contact = ball_entity.find_component(
            ball.BallContactComponent.type
            )
        if ball_contact == None:
            raise ValueError( "No ball contact component" )

        velocity = random.random()
        position = self.entity.data[ 'position' ]

        if position > 0.0:
            velocity *= -1.0

        ball_contact.hit( velocity )

        print "%s hit the ball!" %self.entity.name
Esempio n. 7
0
# begin playing
print "Playing to %i" % games_to_play

# serve the ball
ball_contact = ball_entity.find_component(ball.BallContactComponent.type)

if ball_contact == None:
    raise ValueError("No ball contact component")


ball_contact.serve()

# keep playing until the game is over
while True:
    Entity.update_entities()

    if table_entity.data["games"] >= games_to_play:
        break

# print final score
print "Final score"
player1_score = table_entity.data["left_score"]
player2_score = table_entity.data["right_score"]
print "Player 1: %i" % player1_score
print "Player 2: %i" % player2_score

if player1_score > player2_score:
    print "Player 1 Wins!"
elif player1_score < player2_score:
    print "Player 2 Wins!"