Ejemplo n.º 1
0
def main():
    # Initialise screen
    pygame.init()
    screen = pygame.display.set_mode((screen_x, screen_y))
    pygame.display.set_caption(
        'Pong Game Controlled by Artificial Neural Network')

    # Fill background
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((255, 255, 255))

    # Initialise players
    global player1
    global player2
    player1 = Player("left")
    player2 = Player("right")

    # Initialise ball
    speed = 13
    rand = ((0.1 * (random.randint(5, 8))))
    ball = Ball((0.47, speed))

    # Initialise sprites
    playersprites = pygame.sprite.RenderPlain((player1, player2))
    ballsprite = pygame.sprite.RenderPlain(ball)

    # Blit everything to the screen
    screen.blit(background, (0, 0))
    pygame.display.flip()

    # Initialise clock
    clock = pygame.time.Clock()

    # Initialise NeuralNetwork
    nn = NeuralNetwork()
    nn.set_random_w()
    input = [0, 0, 0]
    output = [0]
    t_output = [0]

    # Font Initialization
    pygame.font.init()
    font_title = pygame.font.SysFont('Comic Sans MS', 20)
    font_comment = pygame.font.SysFont('Comic Sans MS', 12)
    text_title = font_title.render("Neural Network Pong", False, (0, 0, 0))
    text_controls = font_comment.render(
        "Keyboard control: 'a' - Player Up, 'y' - Player Down", False,
        (100, 100, 100))

    # Event loop
    while True:
        clock.tick(60)

        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN:
                if event.key == K_a:
                    player1.moveup()
                if event.key == K_z:
                    player1.movedown()
                if event.key == K_UP:
                    player2.moveup()
                if event.key == K_DOWN:
                    player2.movedown()
            elif event.type == KEYUP:
                if event.key == K_a or event.key == K_z:
                    player1.movepos = [0, 0]
                    player1.state = "still"
                if event.key == K_UP or event.key == K_DOWN:
                    player2.movepos = [0, 0]
                    player2.state = "still"

        (x, y) = ball.get_position()
        angle = ball.get_angle()
        (xp, yp) = player2.get_position()

        input[0] = x
        input[1] = y
        input[2] = angle

        nn.set_input(input)
        nn.feed_forward()
        output = nn.get_output()

        t_output[0] = y

        nn.set_delta_output(t_output)

        nn.back_propagation()
        nn.adjust_w()

        if output[0] > y:
            player2.moveup()
        else:
            player2.movedown()
        print('Output = {} Position = {}'.format(output[0], x))

        screen.blit(text_title, (230, 0))
        screen.blit(text_controls, (20, 450))
        screen.blit(background, ball.rect, ball.rect)
        screen.blit(background, player1.rect, player1.rect)
        screen.blit(background, player2.rect, player2.rect)
        ballsprite.update()
        playersprites.update()
        ballsprite.draw(screen)
        playersprites.draw(screen)
        pygame.display.flip()